Close the form

  • Thread starter Thread starter Alan
  • Start date Start date
A

Alan

The following is the code when I pressed the Cancel button:

Private Sub cmdCancelAdd_Click()
On Error GoTo Err_cmdCancelAdd_Click
DoCmd.DoMenuItem acFormBar, acEditMenu, acUndo, , acMenuVer70
Exit_cmdCancelAdd_Click:
Exit Sub
Err_cmdCancelAdd_Click:
MsgBox Err.Description
Resume Exit_cmdCancelAdd_Click
End Sub

What code and where should I put in order to close the form ?
 
After your current DoCmd, which undoes the changes add

DoCmd.Close acForm, Me.Name

You can probably get by with just DoCmd.Close.
 
I added the that command and now works.
BUT, I have another button as 'Save':

Private Sub cmdSaveAdd_Click()
On Error GoTo Err_cmdSaveAdd_Click
DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70
DoCmd.Close
Exit_cmdSaveAdd_Click:
Exit Sub

Err_cmdSaveAdd_Click:
MsgBox Err.Description
Resume Exit_cmdSaveAdd_Click

End Sub

Taht's similar to the Cancel, I then added the command into this section but
I got an error:
"The command of action 'SaveRecord' isn't available now"
 
I added the that command and now works.
BUT, I have another button as 'Save':
[...]

DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70
DoCmd.Close
[...]


Taht's similar to the Cancel, I then added the command into this section but
I got an error:
"The command of action 'SaveRecord' isn't available now"

You can replace the first "DoCmd" line with:

If Me.Dirty Then
Me.Dirty = False
End If

This will cause the record to save only if it needs to be saved and avoids that
error message.
 
Back
Top