Access Form. New code for new record interfering with OK and Cancel button

Hi

I have introduced a "New Supplier" button on my Access form, which has an "OK" and "Cancel" button designed to save changes
if the user clicks "OK". The introduction of the "New Supplier" button has caused errors. If I click this button, fill in y information
and click OK things seem to work fine. I get the expected "Changes saved" message. If I then go and click the "New Supplier" button
again I get the error shown in the image further down

Option Compare Database

Private Sub btnCancel_Click()
   On Error GoTo EH
    '-----------------------------
     DoCmd.Close
     MsgBox "Changes discarded!"
    '-----------------------------
    Exit Sub
EH:
    MsgBox Err.Description
End Sub

Private Sub btnNewSupplier_Click()

On Error GoTo EH
    '-----------------------------
    'First go to the last record...
      With Me.RecordsetClone
          .MoveLast
           Me.Bookmark = .Bookmark
      End With
     'Now mimic right click on arrow to create (New) record
      DoCmd.GoToRecord , , acNext 'This acts like the right arrow right on the bottom of the form
    '-----------------------------
      Exit Sub
EH:
      MsgBox Err.Description
End Sub

Private Sub btnOK_Click()

On Error GoTo EH
    '-----------------------------
      blnSaved = True
      MsgBox "Changes saved!"
    '-----------------------------
      Exit Sub
EH:
      MsgBox Err.Description
      
End Sub

Private Sub Form_BeforeUpdate(Cancel As Integer)
   On Error GoTo EH
   '-----------------------------
    If blnSaved = False Then
        Cancel = True
    End If
   '-----------------------------
    Exit Sub
EH:
    MsgBox Err.Description
End Sub

Private Sub Form_Current()
    On Error GoTo EH
    '-----------------------------
      blnSaved = False
    '-----------------------------
    Exit Sub
EH:
    MsgBox Err.Description
End Sub

Open in new window


1
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

MacroShadowCommented:
When you click the OK button, the code in Form_BeforeUpdate is executed. That is what's causing the record to not be saved.

    If blnSaved = False Then
        Cancel = True
    End If

Open in new window


blnSaved was set to false in Form_Current.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
Thanks
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.