I am currently trying to create a form that will update the text in a series of text boxes based upon the selection of a value in a combobox. All of the data updated is tied to either the same table as the combo box or to other tables in the database tied by primary keys.
I'm trying to get one text box to update and it doesn't want to work.
Here is my code thus far:
Option Compare Database
Dim localConnection As New ADODB.Connection
Dim rs As New ADODB.Recordset
Private Sub cboID_AfterUpdate()
Dim sql As String
'On Error GoTo DbError
sql = "SELECT * " & _
"FROM SignMainGeneral " & _
"WHERE ID = " & Me.cboID.Value
rs.Open sql, localConnection, , , adCmdText
'Me.txtMUTCDCode.SetFocus
If rs.BOF = True Then
'populate with MUTCD code
Me.txtMUTCDCode = rs!MUTCDCode
Else
MsgBox "Error."
End If
rs.Close
Set rs = Nothing
Exit Sub
'DbError:
'MsgBox "There was an error retrieving information " & _
"from the database. " & Err.Number & ", " & Err.Description
rs.Close
Set rs = Nothing
End Sub
Private Sub Form_Load()
On Error GoTo ConnectionError
Set localConnection = CurrentProject.Connection
MsgBox "Local connection established."
Exit Sub
ConnectionError:
MsgBox "Error Connecting"
End Sub
Public Sub Form_Unload(Cancel As Integer)
'localConnection.Close
'rs = Nothing
End Sub
It seems my recordset doesn't return values when it should. The MUTCDCode value returned by the recordset appears to be NULL. I will then get an error saying "the value you have entered isn't valid for this field."
I am very new to creating forms in Access. I'm not even sure if I need to use VBA to accomplish my goal. Any help would be much appreciated. Thanks.
I'm trying to get one text box to update and it doesn't want to work.
Here is my code thus far:
Option Compare Database
Dim localConnection As New ADODB.Connection
Dim rs As New ADODB.Recordset
Private Sub cboID_AfterUpdate()
Dim sql As String
'On Error GoTo DbError
sql = "SELECT * " & _
"FROM SignMainGeneral " & _
"WHERE ID = " & Me.cboID.Value
rs.Open sql, localConnection, , , adCmdText
'Me.txtMUTCDCode.SetFocus
If rs.BOF = True Then
'populate with MUTCD code
Me.txtMUTCDCode = rs!MUTCDCode
Else
MsgBox "Error."
End If
rs.Close
Set rs = Nothing
Exit Sub
'DbError:
'MsgBox "There was an error retrieving information " & _
"from the database. " & Err.Number & ", " & Err.Description
rs.Close
Set rs = Nothing
End Sub
Private Sub Form_Load()
On Error GoTo ConnectionError
Set localConnection = CurrentProject.Connection
MsgBox "Local connection established."
Exit Sub
ConnectionError:
MsgBox "Error Connecting"
End Sub
Public Sub Form_Unload(Cancel As Integer)
'localConnection.Close
'rs = Nothing
End Sub
It seems my recordset doesn't return values when it should. The MUTCDCode value returned by the recordset appears to be NULL. I will then get an error saying "the value you have entered isn't valid for this field."
I am very new to creating forms in Access. I'm not even sure if I need to use VBA to accomplish my goal. Any help would be much appreciated. Thanks.