vb.ne textbox数字保存excel_第80讲 工作表数据与UserForm窗口的交互:记录的编辑和保存...

2107bcc3c28415ebd564e0b9390d778f.png

大家好,我们今天继续讲解VBA数据库解决方案,今日讲解的是第80讲:工作表数据与UserForm窗口的交互过程中:如何对显示的记录进行编辑和保存。在前几讲中,我们实现了将工作表的数据传给UserForm窗口,实现的开始记录、下一条记录最后记录的显示,我们今日继续讲解如何实现编辑和保存记录。前几讲是查找与显示,查找的目的是为了编辑。

思路:①在UserForm窗口上,设置显示编辑和保存按钮,用于指令的下达。

②在弹出UserForm窗口后,EXCEL文件要隐藏。

③要考虑到按钮之间的作用,此按钮要在开始按钮按过之后才可以响应动作。同时窗口上可以显示的按钮还有“显示下一条记录”和“显示最后记录”按钮

下面我们首先实现UserForm窗体:在上一讲的基础上我这次增加的是“编辑”和“保存”按钮:

a3feeca15129e9796b9c5366ecf2c198.png

下面看代码的实现:

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才能响应动作。

下面看代码的截图:

76facdac33bd59aa3815c21ce9ae68f3.png

9be593713516021ea36ff976963f0bc1.png

e19f746f79f0e6d0cecd0e91acc80d60.png

代码的运行:

30d083d929a0dfef6f60f6b756d459d7.png

01072b2be741e458feaa10eab6170fdc.png

ea485effc663e6e50a85866bab2a3f84.png

4ead8868a69b2227faa02ebf65730e75.png

2bb8ae622238edd224b2dda8e9bad569.png

工作表数据与UserForm窗口的交互中,如何实现记录的编辑和保存

今日内容回向:

1 如何实现记录的编辑和保存?

2 连接ADO连接EXCEL中,为了实现编辑和保存要进行什么设置?

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值