刘金玉的零基础VB教程084期:
mshflexgrid联动数据库更新(编辑)
窗体模态 Form2.Show vbModal
vbModal使得弹出的界面与第一个界面绑定的关系,只能编辑弹出的界面,而无法使用第一个界面。
如何在新窗体中获取原窗体中的内容?
Label2.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)’获取密码
如何保证mshflexgrid实时更新?
Form1.MSHFlexGrid1.TextMatrix(Form1.MSHFlexGrid1.Row, 1) = Text1.Text
如果要在form2中更新form1的mshflexgrid,那么必须要使用TextMatrix方法进行赋值的形式,而不能采用数据库重新查找刷新的形式
课堂总结
1、掌握mshflexgrid的增加、删除、修改、查询
2、掌握mshflexgrid与数据库的绑定与联动
3、学会这个控件的综合应用,比如说图书管理系统等
项目列表
界面:
form1窗体
form2窗体
源代码:
form1窗体源码
Option Explicit Dim sql As String Dim conn As New ADODB.Connection Dim rs As ADODB.Recordset Private Sub Command1_Click() If conn.State = 0 Then conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\Database3.mdb;Persist Security Info=False" conn.Open End If sql = "insert into [users]([username],[password]) values('" & Text1.Text & "','" & Text2.Text & "')" Set rs = New ADODB.Recordset rs.Open sql, conn, adOpenKeyset, adLockBatchOptimistic 'MsgBox "录入成功!" 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) = "密码" End Sub Public Sub Command2_Click() If conn.State = 0 Then conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\Database3.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) = "密码" Command4.Enabled = True End Sub Private Sub Command3_Click() If conn.State = 0 Then conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\Database3.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 'Call Command2_Click '方法1 '方法2 MSHFlexGrid1.RemoveItem MSHFlexGrid1.Row End Sub Private Sub Command4_Click() Form2.Show vbModal End Sub
form2窗体源码
Option Explicit Dim conn As New ADODB.Connection Dim rs As ADODB.Recordset Dim sql As String Private Sub Adodc1_WillMove(ByVal adReason As ADODB.EventReasonEnum, adStatus As ADODB.EventStatusEnum, ByVal pRecordset As ADODB.Recordset) End Sub Private Sub Command1_Click() If conn.State = 0 Then conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\Database3.mdb;Persist Security Info=False" conn.Open End If '更新数据库 Set rs = New ADODB.Recordset sql = "update [users] set [username]='" & Text1.Text & "',[password]='" & Text2.Text & "' where id=" & Label2.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 Unload Form2 End Sub Private Sub Form_Load() Label2.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) End Sub