OnClose DoCmd: If Form Open In Read Only vs. If Not

G

Guest

Hi there,
I'm working on a form that will open in Read Only mode by default.
A user can change it to Edit mode by clicking a button.
On Close, I want the user to be prompted to save their changes, but only if
it was open in Edit mode.
I've tried the following If statement (based on a post that I found with the
basic structure), but am having difficulty getting it to work. (I'm getting
Error 2501 - Not sure why.)

Thank you in advance for your help. I'm new to VBA and greatly appreciate
your guidance!

CODE:
Private Sub Form_Close()
If Forms("frmEmployees").AllowEdits Then
DoCmd.Close acForm, "frmEmployees", acSaveYes
Else
DoCmd.Close acForm, "frmEmployees", acSaveNo
End If


End Sub
 
A

Allen Browne

Jenna, I think you have a misunderstanding here.

The acSaveYes does not apply to saving the record. That's already been saved
before the Close event occurs. Instead, it refers to saving changes to the
form itself (such as saving its Filter or OrderBy properties, or if you have
made design changes to the form.)

If you want to prompt the user whether to save the record, use the
BeforeUpdate event of the form like this:
Private Sub Form_BeforeUpdate(Cancel As Integer)
If MsgBox("Save?", vbYesNo) = vbNo Then
Cancel = True
'Me.Undo
End If
End Sub
 
G

Guest

Allen,

Thank you for pointing that out. You're right. I didn't understand correctly
acSaveYes. I had that scattered all around in my code for my db - so I really
appreciate your correction!

Thanks for the solution. Works perfectly. Thanks for taking the time to
respond in detail to such a simple question. Also, thanks for your
informative webpage. I refer to it often.
 

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