excel学习库

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

第80讲:工作表数据与UserForm窗口的交互,记录的编辑和保存

大家好,我们今天继续讲解VBA数据库解决方案,今日讲解的是第80讲:工作表数据与UserForm窗口的交互过程中:如何对显示的记录进行编辑和保存。在前几讲中,我们实现了将工作表的数据传给UserForm窗口,实现的开始记录、下一条记录最后记录的显示,我们今日继续讲解如何实现编辑和保存记录。前几讲是查找与显示,查找的目的是为了编辑。 思路:①在UserForm窗口上,设置显示编辑和保存按钮,用于指令的下达。 ②在弹出UserForm窗口后,EXCEL文件要隐藏。 ③要考虑到按钮之间的作用,此按钮要在开始按钮按过之后才可以响应动作。同时窗口上可以显示的按钮还有“显示下一条记录”和“显示最后记录”按钮 下面我们首先实现UserForm窗体:在上一讲的基础上我这次增加的是“编辑”和“保存”按钮:下面看代码的实现: 1 从EXCEL窗口进入人机交互窗口: Sub mynzRecords_80() '将工作表数据变成记录集,并实现编辑和保存 Application.Visible = False UserForm1.Show End Sub 代码解释:上述代码完成从EXCEL界面到人机交互UserForm窗体,这时的Application.Caller是5. 2 窗体加载时设置相关的属性代码: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 代码解释:上述代码设置了各个按钮的必要属性,大家要注意,由于涉及到保存记录,这里的TextBox 的Enabled属性设置为False. 3 “编辑”按钮响应代码: Private Sub CommandButton5_Click() '编辑 MsgBox ("请修改记录!") UserForm1.TextBox2.Enabled = True UserForm1.TextBox3.Enabled = True UserForm1.CommandButton6.Enabled = True '保存记录 End Sub 代码解释: 点击“编辑”按钮后弹出对话框,要求和用户确认,得到认可后把TextBox2.Enabled,TextBox3.Enabled, CommandButton6.Enabled的属性修改为True,这时就用户可以编辑了与保存了。 4 “保存”按钮响应代码: Private Sub CommandButton6_Click() '保存 If UserForm1.TextBox1.Value = "" Or UserForm1.TextBox2.Value = "" Or UserForm1.TextBox3.Value = "" Then MsgBox "信息有空值,请确认!": Exit Sub If MsgBox("是否要保存记录?", vbOKCancel, "提示") = vbCancel Then Exit Sub Dim cnADO, rsADO As Object Dim strPath, strSQL As String Dim myData() As Variant 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=0';" _ & "data source=" & strPath strSQL = "SELECT * FROM [数据7$]" rsADO.Open strSQL, cnADO, 1, 3 If UserForm1.TextBox1.Enabled = False Then '编辑的保存 If rsADO.RecordCount > 0 Then rsADO.MoveFirst Do While Not rsADO.EOF If Trim(rsADO.Fields(0)) = UserForm1.TextBox1.Value Then rsADO.Fields(1) = UserForm1.TextBox2.Value rsADO.Fields(2) = UserForm1.TextBox3.Value rsADO.Update GoTo 100 End If rsADO.MoveNext Loop 100: UserForm1.TextBox1.Enabled = False UserForm1.TextBox2.Enabled = False UserForm1.TextBox3.Enabled = False UserForm1.CommandButton6.Enabled = False MsgBox ("保存OK!") Else '录入的保存 If rsADO.RecordCount > 0 Then Do While Not rsADO.EOF If Trim(rsADO.Fields(0)) = UserForm1.TextBox1.Value Then MsgBox "员工编号重复,请确认!": GoTo 110 rsADO.MoveNext Loop End If rsADO.AddNew rsADO.Fields(0) = UserForm1.TextBox1.Value rsADO.Fields(1) = UserForm1.TextBox2.Value rsADO.Fields(2) = UserForm1.TextBox3.Value rsADO.Update 110: UserForm1.TextBox1.Value = "" UserForm1.TextBox2.Value = "" UserForm1.TextBox3.Value = "" UserForm1.TextBox1.SetFocus MsgBox ("保存OK!") End If rsADO.Close cnADO.Close Set rsADO = Nothing Set cnADO = Nothing End Sub 代码解释:保存的时候有两种情况:一个是后面讲到的的录入数据后的保存;一个是修改记录的保存,两者的有所区别,这里是用TextBox1.Enabled的属性作为判断的依据,保存的代码是rsADO.Update .这里要特别注意的是数据库在连接时的设置:imex=0。 4 在“开始”按钮中相关代码: If Right(Application.Caller, 1) = 5 Then '编辑记录 UserForm1.CommandButton1.Enabled = True UserForm1.CommandButton4.Enabled = True UserForm1.CommandButton5.Enabled = True End If 代码解释: 当按下“开始”按钮后的按钮1、4、5才能响应动作。 下面看代码的截图: 代码的运行: 今日内容回向: 1 如何实现记录的编辑和保存? 2 连接ADO连接EXCEL中,为了实现编辑和保存要进行什么设置?

发表评论:

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

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