今天學習了劉金玉老師零基礎VB教程的第85期,也是我學習VB基礎知識的最後一期,學習的主要内容是數據庫mshflexgrid數據庫聯動更新。
1.思路:今天學習的内容,主要在昨天數據删除聯動的基礎上,增加數據聯動更新的功能,通過鼠标選擇确定需要更新的行号,然後将選中行的數據庫内容鍊接到新的窗體(form2)的text控件中,然後修改form2的text控件内容,最後将更新後的内容,分别輸入數據庫和mshflexgrid表格,最終達到更新數據的目的。
2.補充知識:Form2.Show vbModal表示的是form窗體彈出時候,原關聯的窗體隻能在後面。
3.案例:數據庫和mshflexgrid聯動錄入删除查詢和更新
(1)控件
窗體一:
窗體二:
(2)代碼:
窗體一代碼:
Option Explicit
Dim sql As String
Dim conn As New ADODB.Connection
Dim rs As ADODB.Recordset
Private Sub Command1_Click()
'連接數據庫,并通過text控件連接數據庫表格,實現數據輸入
If conn.State = 0 Then
conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\作業成果\學習VB編程第68天\Database1.mdb;Persist Security Info=False" '連接語句填寫在此處
conn.Open
End If
sql = "insert into [users]([username],[sex],[age]) values('" & Text1.Text & "','" & Text2.Text & "','" & Text3.Text & "')"
Set rs = New ADODB.Recordset
rs.Open sql, conn, adOpenKeyset, adLockBatchOptimistic
'同步顯示數據到MSHFlexGrid表格
sql = "select * from users"
rs.Open sql, conn, adOpenKeyset, adLockBatchOptimistic
Set MSHFlexGrid1.DataSource = rs
MSHFlexGrid1.TextMatrix(0, 0) = "序号"
MSHFlexGrid1.TextMatrix(0, 1) = "姓名"
MSHFlexGrid1.TextMatrix(0, 2) = "性别"
MSHFlexGrid1.TextMatrix(0, 3) = "年齡"
End Sub
Public Sub Command2_Click()
'連接數據庫,并通過text控件連接數據庫表格
If conn.State = 0 Then
conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\作業成果\學習VB編程第68天\Database1.mdb;Persist Security Info=False"
conn.Open
End If
Set rs = New ADODB.Recordset
sql = "select * from users"
rs.Open sql, conn, adOpenKeyset, adLockBatchOptimistic
Set MSHFlexGrid1.DataSource = rs
MSHFlexGrid1.TextMatrix(0, 0) = "序号"
MSHFlexGrid1.TextMatrix(0, 1) = "姓名"
MSHFlexGrid1.TextMatrix(0, 2) = "性别"
MSHFlexGrid1.TextMatrix(0, 3) = "年齡"
Command4.Enabled = True
End Sub
Private Sub Command3_Click()
If conn.State = 0 Then
conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\作業成果\學習VB編程第68天\Database1.mdb;Persist Security Info=False"
conn.Open
End If
Set rs = New ADODB.Recordset
sql = "delete from [users] where [id]=" & MSHFlexGrid1.TextMatrix(MSHFlexGrid1.Row, 0)
rs.Open sql, conn, adOpenKeyset, adLockBatchOptimistic
'方法一删除MSHFlexGrid數據
'Call Command2_Click
'方法二更新MSHFlexGrid數據
MSHFlexGrid1.RemoveItem MSHFlexGrid1.Row
End Sub
Private Sub Command4_Click()
Form2.Show vbModal
End Sub
Private Sub Form_Load()
End Sub
窗體二代碼:
Option Explicit
Dim conn As New ADODB.Connection
Dim rs As ADODB.Recordset
Dim sql As String
Private Sub Command1_Click()
'打開數據庫
If conn.State = 0 Then
conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\作業成果\學習VB編程第68天\Database1.mdb;Persist Security Info=False"
conn.Open
End If
'更新數據庫
Set rs = New ADODB.Recordset
sql = "update [users] set [username]='" & Text1.Text & "',[sex]='" & Text2.Text & "',[age]='" & Text3.Text & "' where id=" & Label5.Caption
rs.Open sql, conn, adOpenKeyset, adLockBatchOptimistic
'更新MSHFlexGrid表格
Form1.MSHFlexGrid1.TextMatrix(Form1.MSHFlexGrid1.Row, 1) = Text1.Text
Form1.MSHFlexGrid1.TextMatrix(Form1.MSHFlexGrid1.Row, 2) = Text2.Text
Form1.MSHFlexGrid1.TextMatrix(Form1.MSHFlexGrid1.Row, 3) = Text3.Text
Unload Form2
End Sub
Private Sub Form_Load()
Label5.Caption = Form1.MSHFlexGrid1.TextMatrix(Form1.MSHFlexGrid1.Row, 0)
Text1.Text = Form1.MSHFlexGrid1.TextMatrix(Form1.MSHFlexGrid1.Row, 1)
Text2.Text = Form1.MSHFlexGrid1.TextMatrix(Form1.MSHFlexGrid1.Row, 2)
Text3.Text = Form1.MSHFlexGrid1.TextMatrix(Form1.MSHFlexGrid1.Row, 3)
End Sub
(3)運行效果詳見視頻
,更多精彩资讯请关注tft每日頭條,我们将持续为您更新最新资讯!