Question

Is it possible to bypass the BeforeUpdate event? I've got some code in my BeforeUpdate event that runs when an attempt to save the record is made, however on the same form I have a subform that requires the user to select a record in that subform. As this changes the focus of the current record it triggers my BeforeUpdate code, which I don't want it to do.

Is there a way of bypassing the form's BeforeUpdate event when the user clicks on a record in a form's subform?

Was it helpful?

Solution

"Is there a way of bypassing the form's BeforeUpdate event when the user clicks on a record in a form's subform?"

When you switch focus to a subform with an unsaved record in the main form, Access will automatically attempt to save the "dirty" main form record.

That behavior is built into Access and you can't prevent it.

I think you should use a different approach. Currently (based on the comments discussion) you have a command button to save the main form record. However if certain controls contain Null, you abort the save from the form's BeforeUpdate event.

From my point of view, the user should not even be able to click the command button when any of those controls contain Null. (Why let them click a button and then inform them the action is invalid?) So I suggest you only enable the command button when all those controls contain non-Null values.

Consider a command button named cmdSave and 3 text boxes: txtOne, txtTwo, and txtThree. Create a procedure, SetButtonAvailability, and call that procedure in the AfterUpdate events of all 3 text boxes.

Private Sub SetButtonAvailability
    If IsNull(Me.txtOne) Or IsNull(Me.txtTwo) Or IsNull(Me.txtThree) Then
        Me.cmdSave.Enabled = False
    Else
        Me.cmdSave.Enabled = True
    End If
End Sub

Also in the form's OnCurrent event, you would do Me.cmdSave.Enabled = False because there are not yet any changes for the current record to save.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top