Private Sub AutoWriteRecord() '自适应 On Error GoTo Err_AutoWriteRecord Dim D '创建一个变量 Set D = CreateObject("Scripting.Dictionary")
Dim i As Long For i = 0 To Me.Controls.Count - 1 If Me.Controls(i).ControlType = acTextBox _ Or Me.Controls(i).ControlType = acComboBox _ Or Me.Controls(i).ControlType = acListBox _ Or Me.Controls(i).ControlType = acCheckBox _ Or Me.Controls(i).ControlType = acOptionButton Then If Me.Controls(i).Name <> "编号" Then '自动编号字段不可写,所以除外,可以用on error resume next 忽略 D.Add Me.Controls(i).Name, Me.Controls(i).Value End If End If Next i '原理很简单,先将控件值赋值给变量 '然后在添加记录后再将该变量值赋值给控件 DoCmd.GoToRecord , , acNewRec
Dim K K = D.keys
For i = 0 To D.Count - 1 Me.Controls(K(i)).Value = D(K(i)) Next i
Private Sub AutoWriteRecord_1(strControlName As String) '人工定义控件名 On Error GoTo Err_AutoWriteRecord Dim D '创建一个变量 Set D = CreateObject("Scripting.Dictionary")
Dim strSName() As String strSName = Split(strControlName, ";")
Dim i As Long For i = 0 To UBound(strSName) - 1 D.Add strSName(i), Me.Controls(strSName(i)).Value Next i '原理很简单,先将控件值赋值给变量 '然后在添加记录后再将该变量值赋值给控件 DoCmd.GoToRecord , , acNewRec
For i = 0 To UBound(strSName) - 1 Me.Controls(strSName(i)).Value = D(strSName(i)) Next i Set D = Nothing