excel学习库

excel表格_excel函数公式大全_execl从入门到精通

如何实现工作表数据与自定义窗口的交互

大家好,我们今天继续讲解VBA数据库解决方案,今日讲解的是第77讲:工作表数据与UserForm窗口的交互:第一条记录的显示。从这讲开始,我将利用7讲的时间,给大家详细讲解一个工作表数据库的准实例,虽然是小型的工程,但麻雀虽小,五脏俱全。一些必要的操作都有,其中还会涉及到窗体的设计,按钮的设计等等内容,可以说,如果你需要管理的数据字段和我的例子相同的话,基本上可以不做修改就可以直接使用。由于此工程内容较多,代码也较多,还有窗体的设计,我只能一步一步的讲解,最后再展示给大家最后总的工程内容。 好了,我们今日的内容是实现工作表内容如何体现在UserForm窗口,先实现第一条记录的体现。 思路:①首先建立一个UserForm窗口,然后在上面设计文本框用于装载数据,按钮用于指令的下达。 ②在弹出UserForm窗口后,EXCEL文件要隐藏。 ③由于我的窗体中会陆续增加很多按钮,要根据实际的要求,每次起作用的按钮不一样。 下面是工作表的数据:下面我们来实现我们的目的: 首先建立一个窗口,由于我的设计时一次调试完成的,本讲只讲解本讲涉及到的内容,窗口如下:下面看我的代码: 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 代码截图:代码重点讲解: 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显示不同的字段。 下面看代码的运行: 今日内容回向: 1 如何控制窗体的显示出不同的可用按钮? 2 为了显示记录集,要把记录的各个字段赋值给窗口的哪个控件?

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

«    2024年12月    »
1
2345678
9101112131415
16171819202122
23242526272829
3031
控制面板
您好,欢迎到访网站!
  查看权限
网站分类
搜索
最新留言
    文章归档
      友情链接