Close the form

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 ?
 
W

Wayne Morgan

After your current DoCmd, which undoes the changes add

DoCmd.Close acForm, Me.Name

You can probably get by with just DoCmd.Close.
 
A

Alan

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"
 
B

Bruce M. Thompson

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top