vbOkCancel

T

Tara

I have a form that uses some code when it's about to be closed that confirms
the user either A - wants to close the form without saving data changes or B
- wants to cancel the Close operation and save the data first. It works fine
except that it also appears even if the form has no data or no data has been
edited. I would like to code it so that if the user made no changes or the
record is empty, the MsgBox doesn't appear. For instance, if the user opened
the form by mistake and simply wants to close out before any data has even
been entered. Here's the code currently:

Private Sub cmdAddFam_Click()
On Error GoTo Err_cmdAddFam_Click

Select Case MsgBox("WARNING! You are about to close the form without
saving the data. " & _
"Are you sure you want to continue?", vbOKCancel)
Case vbOK
Me.Undo
DoCmd.Close acForm, "FrmAddClient", acSaveNo
Case vbCancel
Exit Sub
End Select

Exit_cmdAddFam_Click:
Exit Sub

Err_cmdAddFam_Click:
MsgBox Err.Description
Resume Exit_cmdAddFam_Click

End Sub

Thanks!
 
J

John W. Vinson

I would like to code it so that if the user made no changes or the
record is empty, the MsgBox doesn't appear.

Check the value of Me.Dirty. It will be true if the user has changed a value
of a bound control. If it's false, just exit.
 
D

Dirk Goldgar

Tara said:
I have a form that uses some code when it's about to be closed that
confirms
the user either A - wants to close the form without saving data changes or
B
- wants to cancel the Close operation and save the data first. It works
fine
except that it also appears even if the form has no data or no data has
been
edited. I would like to code it so that if the user made no changes or
the
record is empty, the MsgBox doesn't appear. For instance, if the user
opened
the form by mistake and simply wants to close out before any data has even
been entered. Here's the code currently:

Private Sub cmdAddFam_Click()
On Error GoTo Err_cmdAddFam_Click

Select Case MsgBox("WARNING! You are about to close the form without
saving the data. " & _
"Are you sure you want to continue?", vbOKCancel)
Case vbOK
Me.Undo
DoCmd.Close acForm, "FrmAddClient", acSaveNo
Case vbCancel
Exit Sub
End Select

Exit_cmdAddFam_Click:
Exit Sub

Err_cmdAddFam_Click:
MsgBox Err.Description
Resume Exit_cmdAddFam_Click

End Sub


It seems odd to me that you have a button named "cmdAddFam" that cancels the
save, rather than allows or forces it. However, to answer your question,
you can modify your code to only display the prompt if the form is dirty;
like this:

'----- start of revised code -----
Private Sub cmdAddFam_Click()
On Error GoTo Err_cmdAddFam_Click

Dim intResponse As Integer

If Me.Dirty Then

intResponse = MsgBox( _
"WARNING! You are about to close the form without " & _
"saving the data. Are you sure you want to continue?", _
vbOKCancel)

If intResponse = vbOK Then
Me.Undo
End If

Else

intResponse = vbOK

End If

If intResponse = vbOK Then
DoCmd.Close acForm, Me.Name, acSaveNo
End If

Exit_cmdAddFam_Click:
Exit Sub

Err_cmdAddFam_Click:
MsgBox Err.Description
Resume Exit_cmdAddFam_Click

End Sub
'----- end of revised code -----
 
T

Tara

Thanks Dirk! As for the odd Name of the Cmd button, I actually had a bit of
a brain lapse when I created the command button and thought I was in the
middle of something else. I should change it suppose, but I just haven't
 
J

Jeff Boyce

Tara

Dirk's question and your response point to an issue of maintainability...

If something happens to you, whoever tries to understand your application
will surely stumble on that one.

And if YOU happen to walk away for six months, having forgotten to fix that,
YOU will likely stumble over that same issue!

Kind of a "pay now or pay later" situation <g>


Regards

Jeff Boyce
Microsoft Office/Access MVP
 

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