Problem with executing code for automatic field filling

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

|

Hi Milena Mitova-Borisova!

The VBA code seems to be correct, and it should execute as expected.

However, please check the following:

1. Ensure that the Item_AfterUpdate() the event handler is properly connected to the dropdown list. If the event isn’t triggered, the code won’t execute.

2. Ensure that all necessary references including the library references are enabled in your VBA project.

In the VBA editor, go to Tools > References and check if "Microsoft ActiveX Data Objects x.x Library" is checked.

3. Ensure that there are no conflicting names or reserved words used in your code or form controls.

Let me know how it goes.

Best Regards

Give a helping hand....

Please, remember to give back to the community. Help the next person with this problem by indicating whether this answer solved your problem. Click Yes or No at the bottom.

1 person found this reply helpful

·

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

Can you not do this with minimal code by returning the cutValue and unitValue columns in the combo box's RowSource, and assigning the values to the relevant controls in the form in the control's AfterUpdate event procedure?  The following is a simple example:

 

Private Sub ProductID_AfterUpdate()

 

    Dim ctrl As Control

   

    Set ctrl = Me.ActiveControl

   

    Me.UnitPrice = ctrl.Column(1)

    Me.TaxRate = ctrl.Column(2)

   

End Sub

 

The RowSource property of the ProductID combo box control, which is bound to a ProductID foreign key column, is:

 

SELECT ProductID, UnitPrice, TaxRate, Product

FROM Products

ORDER BY Product;

 

And its ColumnWidths property is:

 

0cm;0cm;0cm;8cm

 

_____________________
Ken Sheridan,
Newport, Shropshire, England

"Don't write it down until you understand it!" - Richard Feynman

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

Have you stepped through the code to see what is happening?

But I agree with Ken. This can be done much more simply

Hope this helps,
Scott<>
Blog: http://scottgem.wordpress.com
Microsoft Access MVP since 2007

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

Hello, Thank you for your quick response. I did the setup you describe: Microsoft ActiveX Data Objects 2.0 Library", but the code still didn't work unfortunately. Thank you anyway.

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

Hello, Thanks for the quick reply and the example. It's not exactly the same for me, but after a modification on my part it worked. Thank you was very helpful

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

 
 

Question Info


Last updated May 6, 2024 Views 40 Applies to: