I have a userform in Access 2013 and I'd like to bind the controls to a ADO recordset when it is opened.
So far, I have the following code:
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
I get an error at this line txtSerialNumber.ControlSource = rs.Fields("SerialNumber").Name
My aim is to bind the data retrieved from the recordset to the controls on the userform and when finished, I'll be updating the data table with any changes.
How am I best to fix this?
Thanks