Before Update VBA code not working

Robert Walberg 5 Reputation points
2025-11-12T11:03:59.4733333+00:00

I have a combo box (PublicationCombo) in a subform that requires an entry before the record can be saved. I have the "Required" property in the corresponding table set to "Yes". The following VBA code is in the subform's property:

Private Sub Form_BeforeUpdate(Cancel As Integer)

If IsNull(PublicationCombo) Then

    Cancel = True

    MsgBox "Select a value from the dropdown menu."

    Me.PublicationCombo.SetFocus

End If

End Sub

From everything I've read and viewed, this should work; but it doesn't. I'm stumped as to why it doesn't work. Any ideas?

BTW, without the above code Access displays an error message so the record can't be saved, but the message is not in plain language. That's why I want to insert the message box and keep the focus on the field in question.

<PII Removed>

Microsoft 365 and Office | Access | For home | Windows
{count} votes

Answer recommended by moderator
  1. Robert Walberg 5 Reputation points
    2025-11-17T18:54:46.65+00:00

    Ken & everyone else who contributed

    Happy to say I have solved the problem. The key was me studying and understanding what Ken said about Access setting a default value of zero in the bound table. Once I deleted the Default Value of "0" for all combo box fields (there are 3) and saved the changes, I was able to use the Before Update event as intended in the first place without using the On Error event. Not only that, but I was able to use IF/ElseIF to handle all three combo boxes in the subform. Now the user cannot leave the record without filling in the required fields. Setting the focus and displaying the dropdown menu should make the data entry idiot proof.

    BTW, deleting the Before Update event and inserting Ken's code in the On Error event didn't make a difference. Probably because the bound table default value = 0.

    Thank you to all of you. It wasn't a straight line, but I got there and learned a few things in the process.

    1 person found this answer helpful.
    0 comments No comments

6 additional answers

Sort by: Most helpful
  1. Q&A Assist
    2025-11-12T11:04:14.59+00:00
    AI generated content. This question contains an answer created with AI from Q&A Assist. Learn more

    It looks like your VBA code for the BeforeUpdate event should work as intended, but there are a few things you might want to check:

    1. Combo Box Name: Ensure that the name of the combo box in your form is exactly PublicationCombo. If there's a typo or if the name is different, the code won't recognize the control.
    2. Event Trigger: Make sure that the BeforeUpdate event is properly linked to the form. Sometimes, if the event is not set correctly, the code won't execute.
    3. Control Bound to a Field: Verify that the PublicationCombo is bound to a field in your table. If it's unbound, the IsNull check may not work as expected.
    4. Required Property: While you have set the Required property to Yes, ensure that this property is set correctly in the table design and that it is not being overridden by any other settings.
    5. Error Handling: If there are any other error handling routines in your form that might be interfering with the BeforeUpdate event, they could prevent your message box from displaying.
    6. Debugging: You can add a simple Debug.Print statement before the If condition to see if the code is being executed when you attempt to save the record. This can help you determine if the issue lies within the event firing.

    If you check these aspects and the code still does not work, consider testing in a new form to isolate the issue. Sometimes, forms can have hidden properties or settings that affect behavior.


  2. Duane Hookom 26,545 Reputation points Volunteer Moderator
    2025-11-12T11:44:57.69+00:00

    It might be easier to change the Form’s On Error message from the default to plain language. Sorry I’m not able to test this at the moment.


  3. George Hepworth 22,215 Reputation points Volunteer Moderator
    2025-11-12T12:04:21.41+00:00

    What, exactly is that cryptic error? Show us a screenshot of the actual error message, please.


  4. Karl Donaubauer 2,691 Reputation points MVP
    2025-11-12T13:33:45.0433333+00:00

    Hi,

    Your code doesn't look wrong.

    this should work; but it doesn't.

    Can you explain that in more detail? What exactly are you doing to test this and what happens?

    Servus
    Karl


    Access Forever News DevCon
    Access-Entwickler-Konferenz AEK


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.