应某粉丝,要求弄得一个记账本示例程序
'-------------------------------------------------------------------------------
'主页源码'
Dim UI As New cls_UI
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
UI.ShowForm(浏览, Me)
End Sub
Private Sub Form1_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Closed
Login.Close()
浏览.Close()
添加.Close()
End Sub
Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click
UI.ShowForm(浏览, Me)
End Sub
Private Sub into_data_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles into_data.Click
UI.ShowForm(添加, Me)
End Sub
'----------------------------------------------------------------------
'登录页源码'
Dim CN As New cls_accdb
Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click
Dim dt, dt1 As DataTable
Dim TF, TF1 As Boolean
dt = CN.get_data("Select 用户名 From users Where 用户名='" & UsernameTextBox.Text & "'")
dt1 = CN.get_data("Select 密码 From users Where 用户名='" & UsernameTextBox.Text & "' AND 密码='" & PasswordTextBox.Text & "'")
If dt.Rows.Count > 0 Then TF = True
If dt1.Rows.Count > 0 Then TF1 = True
If UsernameTextBox.Text <> "" And PasswordTextBox.Text <> "" Then
If TF = True And TF1 = True Then
主页.Show()
Me.Visible = False
Else
UsernameTextBox.Text = ""
PasswordTextBox.Text = ""
MsgBox("密码错误!", MsgBoxStyle.Critical, "VB小源码")
End If
End If
End Sub
Private Sub Cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel.Click
Me.Close()
End Sub
Private Sub Login_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
UsernameTextBox.Focus()
End Sub
Private Sub UsernameTextBox_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles UsernameTextBox.KeyUp
If e.KeyCode = Keys.Enter Then
PasswordTextBox.Focus()
End If
End Sub
End Class
'--------------------------------------------------------------------------------------------'
'access数据库操作源码类'
Imports System.Data
Imports System.IO
Imports System.Data.OleDb
Public Class cls_accdb
Public cn As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Application.StartupPath & "\mdb\DataSource.mdb") '定义连接---这里请更改为实际数据库路径及名称
Public DataBaseRST As Integer
Public Function DataModify(ByVal str As String) As Boolean
Dim cmdinsert As New OleDbCommand
Try
cmdinsert.CommandText = str
cmdinsert.Connection = cn
If cn.State = ConnectionState.Closed Then cn.Open()
DataBaseRST = cmdinsert.ExecuteNonQuery()
cn.Close()
Return True
Catch ex As Exception
MessageBox.Show(Err.Description, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return False
End Try
End Function
Public Function get_data(ByVal str As String) As DataTable
Dim tb As New DataTable
Try
Dim ap As New OleDb.OleDbDataAdapter(str, cn)
ap.Fill(tb)
Return tb
Catch ex As Exception
MessageBox.Show(Err.Description, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return tb
End Try
End Function
End Class
'--------------------------------------------------------------------------------------------------'
''查询也源码
Public Class 浏览
Dim cn As New cls_accdb
Private Sub 浏览_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
dgv.DataSource = cn.get_data("Select * From outdata")
End Sub
Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles out_menoy.Click
If OUT_TXT.Text = "" Or OUT_TXT.Text = "输入日期" Then
dgv.DataSource = cn.get_data("Select * From outdata")
Else
dgv.DataSource = cn.get_data("Select * From outdata where 日期 like '%" & OUT_TXT.Text & "%'")
End If
End Sub
Private Sub in_menoy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles in_menoy.Click
If OUT_TXT.Text = "" Or OUT_TXT.Text = "输入日期" Then
dgv.DataSource = cn.get_data("Select * From indata")
Else
dgv.DataSource = cn.get_data("Select * From indata where 日期 like '%" & OUT_TXT.Text & "%'")
End If
End Sub
Private Sub ToolStrip1_ItemClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ToolStripItemClickedEventArgs) Handles ToolStrip1.ItemClicked
End Sub
Private Sub DGv_CellFormatting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles dgv.CellFormatting
If dgv.Columns(e.ColumnIndex).Name = "是否超额" Then
If Not (e.Value Is Nothing) Then
If Not e.Value Is DBNull.Value Then
If e.Value = "是" Then
e.CellStyle.BackColor = Color.Red
End If
End If
End If
End If
End Sub
Private Sub ToolStripButton1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click
On Error Resume Next
Dim dt As DataTable
dt = cn.get_data("Select SUM(合计) From indata")
MsgBox("总收入 " & dt.Rows(0)(0) & " 大洋", MsgBoxStyle.Information, "VB小源码")
End Sub
Private Sub ToolStripButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton2.Click
On Error Resume Next
Dim dt As DataTable
dt = cn.get_data("Select SUM(合计) From outdata")
MsgBox("总支出 " & dt.Rows(0)(0) & " 大洋", MsgBoxStyle.Information, "VB小源码")
End Sub
End Class