0
votes

So i have a form that's updating a table in Microsoft Access. I'm using a BeforeUpdate event call to stop the form from updating if the 'first name' hasn't been filled in.

I basically only want the form to be saved once everything has been filled in, and then switch to see the updated table (the bit under If not cancel).

Currently the popup box to say 'You must enter a First Name' correctly fires, the next pop up box asking if the user wants to undo any changes fires. But then a pop up box saying 'No current record' appears and then the view changes to the table. I'm not sure why this is all happening.

Option Compare Database

Private Sub Form_BeforeUpdate(Cancel As Integer)

   Cancel = False

' perform data validation
If IsNull(Me.customer_f_name) Then

   MsgBox "You must enter a First Name.", vbCritical, "Data entry error..."
   Me.customer_f_name.BorderColor = vbRed
   DoCmd.GoToControl "customer_f_name"

   Cancel = True

End If

If Not Cancel Then

    DoCmd.SelectObject acTable, "ticket_tracker_table"
    DoCmd.Requery
    DoCmd.GoToRecord acDataTable, "ticket_tracker_table", acLast

End If

' if the save has been canceled or did not pass the validation , then ask to Undo changes
If Cancel Then

    If MsgBox("Do you want to undo any changes?", vbYesNo, "Confirm") = vbYes Then
        Me.Undo
    End If

End If

End Sub
2
Which statement causes the message that you do not like? Its not clear whether Yes or No was responded to the prior question. You should be able to set debug on the statement Cancel = False and then step thru until you find more info. - donPablo
I am not by a windows pc now, but I think your error is that you are trying to access the new record before you saved it, i,e, this code is still running in the BeforeUpdate time - YHoffman
Why is a table open? Are you the only user of this db? - June7

2 Answers

0
votes

Review https://docs.microsoft.com/en-us/office/vba/api/Access.Form.BeforeUpdate(even)

Instead of GoToControl, use SetFocus. I did a very simple test with the Undo question and it does work:

Private Sub Form_BeforeUpdate(Cancel As Integer)
If IsNull(Me.customer_f_name) Then
    MsgBox "Must enter customer name."
    Cancel = True
    If MsgBox("Do you want to undo edits?", vbYesNo) = vbYes Then Me.Undo
    Me.customer_f_name.SetFocus
End If
End Sub

Suggest you put table code in AfterUpdate event.

0
votes

so to supplement J7, with some big picture view: "basically only want the form to be saved once everything has been filled in" …. that is swimming against the tide; which is why you are having to write a lot of code. The product enters data as you go fundamentally. that is it's core design.

it is very normal to do field checks as you go - typically in the AfterUpdate event, the check code is applied and if invalid - generate a message box and invoke an Undo plus reset the focus back to the field they were attempting to leave.

A totally different design, in some cases it may justify a temp local duplicate table just for the purposes of holding form input data - - this approach allows a very elaborate check(s) against data in the production database; where upon if valid an Append Query then writes the data into the permanent table.