Link to home
Start Free TrialLog in
Avatar of Murray Brown
Murray BrownFlag for United Kingdom of Great Britain and Northern Ireland

asked on

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


User generated image
ASKER CERTIFIED SOLUTION
Avatar of Joe Howard
Joe Howard
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Murray Brown

ASKER

Thanks