I wrote a VBA code to automate the filling of two fields in a form, but it doesn’t execute. It neither gives an error nor executes. I tested the code through a query - it works. I checked the names for errors - there are none. It just doesn’t work. Can anyone tell me why? The Item field is a dropdown list and values are selected. It is based on a query that contains these fields / the query is for informational purposes to orient the user and to filter only the active ones /.
Private Sub Item_AfterUpdate()
On Error GoTo ErrorHandler
Dim cmd As ADODB.Command
Dim rs As ADODB.Recordset
Dim cutValue As String
Dim unitValue As String
Set cmd = New ADODB.Command
cmd.ActiveConnection = CurrentProject.Connection
cmd.CommandText = "SELECT Cut, Unit FROM Item WHERE Item = ?"
cmd.Parameters.Append cmd.CreateParameter("Item", adVarChar, adParamInput, 255, Me.Item.Value)
Set rs = cmd.Execute
If Not rs.EOF Then
cutValue = rs!Cut
unitValue = rs!Unit
Me.Cut.Value = cutValue
Me.Unit.Value = unitValue
End If
rs.Close
Set rs = Nothing
Set cmd = Nothing
Exit Sub
ErrorHandler:
MsgBox "Error " & Err.Number & ": " & Err.Description, vbCritical, "Error"
Exit Sub
End Sub