大家好,我们今天继续讲解VBA数据库解决方案,今日讲解的是第77讲:工作表数据与UserForm窗口的交互:第一条记录的显示。从这讲开始,我将利用7讲的时间,给大家详细讲解一个工作表数据库的准实例,虽然是小型的工程,但麻雀虽小,五脏俱全。一些必要的操作都有,其中还会涉及到窗体的设计,按钮的设计等等内容,可以说,如果你需要管理的数据字段和我的例子相同的话,基本上可以不做修改就可以直接使用。由于此工程内容较多,代码也较多,还有窗体的设计,我只能一步一步的讲解,最后再展示给大家最后总的工程内容。
好了,我们今日的内容是实现工作表内容如何体现在UserForm窗口,先实现第一条记录的体现。
思路:①首先建立一个UserForm窗口,然后在上面设计文本框用于装载数据,按钮用于指令的下达。
②在弹出UserForm窗口后,EXCEL文件要隐藏。
③由于我的窗体中会陆续增加很多按钮,要根据实际的要求,每次起作用的按钮不一样。
下面是工作表的数据:
![103ce1d4038086fea735fdab180ca9bc.png](https://i-blog.csdnimg.cn/blog_migrate/9509aa88c0678bffe5962afe891891c5.jpeg)
下面我们来实现我们的目的:
首先建立一个窗口,由于我的设计时一次调试完成的,本讲只讲解本讲涉及到的内容,窗口如下:
![e8f07be3d74d8756e2792f7ec7571bf9.png](https://i-blog.csdnimg.cn/blog_migrate/b5786ca6c4023f9235c4d30bbade7896.jpeg)
下面看我的代码:
1 进入交互窗体界面:
Sub mynzRecords_77() '将工作表数据变成记录集,并将第一条数据显示到UserForm窗口
Application.Visible = False
UserForm1.Show
End Sub
2 推出按钮的代码:
Private Sub CommandButton2_Click() '退出
Unload Me
Application.Visible = True
End Sub
3 开始按钮的代码:
Private Sub CommandButton3_Click() '开始按钮
Dim cnADO, rsADO As Object
Dim strPath, strSQL As String
Set cnADO = CreateObject("ADODB.Connection")
Set rsADO = CreateObject("ADODB.Recordset")
strPath = ThisWorkbook.FullName
cnADO.Open "provider=Microsoft.ACE.OLEDB.12.0;extended properties='excel 12.0;hdr=yes;imex=1';" _
& "data source=" & strPath
strSQL = "SELECT * FROM [数据7$]"
rsADO.Open strSQL, cnADO, 1, 3
If rsADO.RecordCount > 0 Then
rsADO.MoveFirst
UserForm1.TextBox1.Value = rsADO.Fields(0)
UserForm1.TextBox2.Value = rsADO.Fields(1)
UserForm1.TextBox3.Value = rsADO.Fields(2)
End If
rsADO.Close
cnADO.Close
Set rsADO = Nothing
Set cnADO = Nothing
If Right(Application.Caller, 1) = 2 Then '下一条
UserForm1.CommandButton1.Enabled = True
End If
If Right(Application.Caller, 1) = 3 Then '最后一条
UserForm1.CommandButton4.Enabled = True
End If
If Right(Application.Caller, 1) = 5 Then '编辑记录
UserForm1.CommandButton1.Enabled = True
UserForm1.CommandButton4.Enabled = True
UserForm1.CommandButton5.Enabled = True
End If
If Right(Application.Caller, 1) = 8 Then '删除
UserForm1.CommandButton1.Enabled = True
UserForm1.CommandButton4.Enabled = True
UserForm1.CommandButton8.Enabled = True '删除记录
End If
End Sub
Private Sub UserForm_Activate()
UserForm1.CommandButton2.SetFocus
If Right(Application.Caller, 1) = 1 Then '第一条记录显示
UserForm1.CommandButton1.Enabled = False '下一条记录
UserForm1.CommandButton4.Enabled = False '最后一条记录
UserForm1.CommandButton5.Enabled = False '编辑记录
UserForm1.CommandButton7.Enabled = False '查找记录
UserForm1.CommandButton8.Enabled = False '删除记录
UserForm1.CommandButton6.Enabled = False '保存记录
UserForm1.CommandButton9.Enabled = False '录入记录
End If
If Right(Application.Caller, 1) = 2 Then '下一条记录
UserForm1.CommandButton1.Enabled = False '下一条记录
UserForm1.CommandButton4.Enabled = False '最后一条记录
UserForm1.CommandButton5.Enabled = False '编辑记录
UserForm1.CommandButton7.Enabled = False '查找记录
UserForm1.CommandButton8.Enabled = False '删除记录
UserForm1.CommandButton6.Enabled = False '保存记录
UserForm1.CommandButton9.Enabled = False '录入记录
End If
If Right(Application.Caller, 1) = 3 Then '显示最后记录
UserForm1.CommandButton1.Enabled = False '下一条记录
UserForm1.CommandButton4.Enabled = False '最后一条记录
UserForm1.CommandButton5.Enabled = False '编辑记录
UserForm1.CommandButton7.Enabled = False '查找记录
UserForm1.CommandButton8.Enabled = False '删除记录
UserForm1.CommandButton6.Enabled = False '保存记录
UserForm1.CommandButton9.Enabled = False '录入记录
End If
If Right(Application.Caller, 1) = 5 Then '显示编辑记录
UserForm1.CommandButton1.Enabled = False '下一条记录
UserForm1.CommandButton4.Enabled = False '最后一条记录
UserForm1.CommandButton5.Enabled = False '编辑记录
UserForm1.CommandButton7.Enabled = False '查找记录
UserForm1.CommandButton8.Enabled = False '删除记录
UserForm1.CommandButton6.Enabled = False '保存记录
UserForm1.CommandButton9.Enabled = False '录入记录
UserForm1.TextBox1.Enabled = False
UserForm1.TextBox2.Enabled = False
UserForm1.TextBox3.Enabled = False
End If
If Right(Application.Caller, 1) = 6 Then '录入
UserForm1.CommandButton3.Enabled = False '开始
UserForm1.CommandButton1.Enabled = False '下一条记录
UserForm1.CommandButton4.Enabled = False '最后一条记录
UserForm1.CommandButton5.Enabled = False '编辑记录
UserForm1.CommandButton7.Enabled = False '查找记录
UserForm1.CommandButton8.Enabled = False '删除记录
UserForm1.CommandButton6.Enabled = False '保存记录
End If
If Right(Application.Caller, 1) = 7 Then '查找
UserForm1.CommandButton3.Enabled = False '开始
UserForm1.CommandButton1.Enabled = False '下一条记录
UserForm1.CommandButton4.Enabled = False '最后一条记录
UserForm1.CommandButton5.Enabled = False '编辑记录
'UserForm1.CommandButton7.Enabled = False '查找记录
UserForm1.CommandButton8.Enabled = False '删除记录
UserForm1.CommandButton6.Enabled = False '保存记录
UserForm1.CommandButton9.Enabled = False '录入记录
UserForm1.TextBox2.Enabled = False
UserForm1.TextBox3.Enabled = False
End If
If Right(Application.Caller, 1) = 8 Then '删除
' UserForm1.CommandButton3.Enabled = False '开始
UserForm1.CommandButton1.Enabled = False '下一条记录
UserForm1.CommandButton4.Enabled = False '最后一条记录
UserForm1.CommandButton5.Enabled = False '编辑记录
UserForm1.CommandButton7.Enabled = False '查找记录
UserForm1.CommandButton8.Enabled = False '删除记录
UserForm1.CommandButton6.Enabled = False '保存记录
UserForm1.CommandButton9.Enabled = False '录入记录
UserForm1.TextBox1.Enabled = False
UserForm1.TextBox2.Enabled = False
UserForm1.TextBox3.Enabled = False
End If
End Sub
代码截图:
![3bcaa2a1a77774ac768118d2347cdeaf.png](https://i-blog.csdnimg.cn/blog_migrate/344bfe78557e69aa373fb6a7542b2df5.jpeg)
![f6d6ba523d9b21ef104b667ce5f7479b.png](https://i-blog.csdnimg.cn/blog_migrate/a1977221cf6d2819c9119697f81ac0a8.jpeg)
![220fdefe61e1f99eb7db9af7b1677852.png](https://i-blog.csdnimg.cn/blog_migrate/c9a8c6326045f6226db5eacb962e710f.jpeg)
![47259a7c1ac11ad077f10ca73f8d6a73.png](https://i-blog.csdnimg.cn/blog_migrate/dd474857bbdc40a37184db0a6f0b3fb2.jpeg)
代码重点讲解:
1 上述代码实现了窗体和工作表数据的交互,将工作表数据显示在窗体上。
2 Application.Visible = False
UserForm1.Show
点击进入窗体时,EXCEL文件隐藏不可用,窗体出现。
3 Unload Me
Application.Visible = True
当窗体退出时,EXCEL文件可用。
4 rsADO.Open strSQL, cnADO, 1, 3
If rsADO.RecordCount > 0 Then
rsADO.MoveFirst
UserForm1.TextBox1.Value = rsADO.Fields(0)
UserForm1.TextBox2.Value = rsADO.Fields(1)
UserForm1.TextBox3.Value = rsADO.Fields(2)
End If
当点击开始时,会建立工作表ADO的连接,同时记录集打开工作表,记录移动到第一条,TextBox1显示不同的字段。
下面看代码的运行:
![a19814c57473493e09a1a4b27663660f.png](https://i-blog.csdnimg.cn/blog_migrate/af14cf97ff7e6491f25065bbeacbbd4e.jpeg)
![0ee45172b46e7fed1476cc8c64cbb2a1.png](https://i-blog.csdnimg.cn/blog_migrate/f59fa411c271c406fc9d00291971b7bd.jpeg)
![7cae0da8fb55774c0a4370e0085e327f.png](https://i-blog.csdnimg.cn/blog_migrate/76e929d9b59da182ee2d8b834316f0ac.jpeg)
今日内容回向:
1 如何控制窗体的显示出不同的可用按钮?
2 为了显示记录集,要把记录的各个字段赋值给窗口的哪个控件?