如何在运行时将用户窗体控件绑定到记录集?

时间:2019-06-27 20:48:42

标签: vba ms-access userform

我在Access 2013中有一个用户窗体,我想在打开控件时将控件绑定到ADO记录集。

到目前为止,我有以下代码:

Private Sub UserForm_Activate()

    Dim cn As ADODB.Connection
    Dim rs As ADODB.Recordset

    Set cn = New ADODB.Connection

    With cn
        .CursorLocation = adUseClient
        .Provider = "Microsoft.ACE.OLEDB.12.0"
        .Properties("Data Source").Value = CurrentProject.FullName
        .Properties("Mode").Value = adModeReadWrite
        .Properties("Persist Security Info").Value = False
        .Open
    End With

       Set rs = New ADODB.Recordset

   With rs
      Set .ActiveConnection = cn
      .Source = "SELECT SerialNumber FROM SerialPlateData"
      .LockType = adLockOptimistic
      .CursorType = adOpenKeyset
      .Open
   End With

    'bind textbox to ado recordset at runtime
    txtSerialNumber.ControlSource = rs.Fields("SerialNumber").Name

    Set rs = Nothing
    Set cn = Nothing

End Sub

在此行txtSerialNumber.ControlSource = rs.Fields("SerialNumber").Name上出现错误

我的目标是将从记录集检索的数据绑定到用户窗体上的控件,完成后,我将使用任何更改来更新数据表。

我如何最好地解决此问题?

谢谢

0 个答案:

没有答案