Close Button Problem.

  • Thread starter malik via AccessMonster.com
  • Start date
M

malik via AccessMonster.com

Hi
I have a close Button name BtnClose

I have the following code for BtnClose :

Private Sub BtnClose_Click()
DoCmd.Close
End Sub

While I have the following Code in Form's Unload Event :

Private Sub Form_Unload(Cancel As Integer)
If Me.Dirty = True Then
Me.Undo
DoCmd.Close acForm, "FrmDrVoucher", acSaveNo
Else

Dim Ans As Integer
Ans = MsgBox("Do you want to exit without saving data?", vbYesNo +
vbInformation, "Confirm")
If Ans = vbYes Then
DoCmd.SetWarnings False
DoCmd.RunCommand acCmdDeleteRecord
DoCmd.SetWarnings True

Else
Cancel = True


End If

End If
End Sub

Now Problem Is that If I press Close button, a YesNo Msg box appears. If I
press Yes, It goes all ok.
But when I press No, It shows me the error

Run time error '2501'
The Close Action Was Cancelled

Wat to do????

Thank you.
 
J

Jeff Boyce

I may be reading that procedure incorrectly, but it looks to me like you
check during unload to see if there's been any data added/edited (If
Me.Dirty = Yes), then undo the changes and close the form. Are you trying
to make sure that no one uses the form to make changes?

The messagebox portion of the code would seem to fire only if the form ISN'T
dirty ... so how could you ever have data to save?

What am I missing?

--

Regards

Jeff Boyce
Microsoft Access MVP

Disclaimer: This author may have received products and services mentioned in
this post. Mention and/or description of a product or service herein does
not constitute endorsement thereof.

Any code or pseudocode included in this post is offered "as is", with no
guarantee as to suitability.

You can thank the FTC of the USA for making this disclaimer
possible/necessary.
 
M

malik via AccessMonster.com

I also have Save Button in my form.


Jeff said:
I may be reading that procedure incorrectly, but it looks to me like you
check during unload to see if there's been any data added/edited (If
Me.Dirty = Yes), then undo the changes and close the form. Are you trying
to make sure that no one uses the form to make changes?

The messagebox portion of the code would seem to fire only if the form ISN'T
dirty ... so how could you ever have data to save?

What am I missing?
Hi
I have a close Button name BtnClose
[quoted text clipped - 39 lines]
Thank you.
 
D

Dirk Goldgar

malik via AccessMonster.com said:
Hi
I have a close Button name BtnClose

I have the following code for BtnClose :

Private Sub BtnClose_Click()
DoCmd.Close
End Sub

While I have the following Code in Form's Unload Event :

Private Sub Form_Unload(Cancel As Integer)
If Me.Dirty = True Then
Me.Undo
DoCmd.Close acForm, "FrmDrVoucher", acSaveNo
Else

Dim Ans As Integer
Ans = MsgBox("Do you want to exit without saving data?", vbYesNo +
vbInformation, "Confirm")
If Ans = vbYes Then
DoCmd.SetWarnings False
DoCmd.RunCommand acCmdDeleteRecord
DoCmd.SetWarnings True

Else
Cancel = True


End If

End If
End Sub

Now Problem Is that If I press Close button, a YesNo Msg box appears. If I
press Yes, It goes all ok.
But when I press No, It shows me the error

Run time error '2501'
The Close Action Was Cancelled

Wat to do????


You have several problems. If your code in Form_Unload cancels the closing,
error 2501 will be raised for the DoCmd.Close statement in BtnClose_Click,
because the requested action was cancelled. You can easily get around that
by adding error-handling to that procedure:

'------ start of revised code ------
Private Sub BtnClose_Click()

On Error GoTo Err_Handler

DoCmd.Close acForm, Me.Name, acSaveNo

Exit_Point:
Exit Sub

Err_Handler:
If Err.Number = 2501 Then
Resume Next
Else
MsgBox Err.Description, vbExclamation, "Error " & Err.Number
Resume Exit_Point
End If

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

HOWEVER, your code in Form_Unload doesn't make sense, because

(a) it says to always undo and close the form if it is dirty, and only ask
the question about saving data if the form is *not* dirty, and

(b) by the time the form's Unload event fires, any modified record will
already have been saved, so the form will *never* be dirty in the form's
Unload event. Period.

You can keep a record from being saved by cancelling the form's BeforeUpdate
event, or by attempting to control all the user actions that might result in
a record being saved. Those include pressing Shift+Enter, moving to a new
record, closing the form from the "X" button, closing he form using a menu
command, closing the form by clicking your BtnClose button, or even closing
the whole Access application. How many of those things do you want to try
to control?

If you only care about your own command button, then you can move your code
to the command button's Click event procedure, along these lines:

'------ start of revised code #2 ------
Private Sub BtnClose_Click()

On Error GoTo Err_Handler

If Me.Dirty Then

If MsgBox( _
"Do you want to exit without saving data?", _
vbYesNo + vbInformation, _
"Confirm") _
= vbYes
Then
' Undo modifications and close form.
Me.Undo
DoCmd.Close acForm, Me.Name, acSaveNo
Else
' Leave form open, with modified data.
End If

Else
' Form is unmodified, so just close it.
DoCmd.Close acForm, Me.Name, acSaveNo
End If


Exit_Point:
Exit Sub

Err_Handler:
If Err.Number = 2501 Then
Resume Next
Else
MsgBox Err.Description, vbExclamation, "Error " & Err.Number
Resume Exit_Point
End If

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

malik via AccessMonster.com

Thanks for ur help.


Dirk said:
Hi
I have a close Button name BtnClose
[quoted text clipped - 37 lines]
Wat to do????

You have several problems. If your code in Form_Unload cancels the closing,
error 2501 will be raised for the DoCmd.Close statement in BtnClose_Click,
because the requested action was cancelled. You can easily get around that
by adding error-handling to that procedure:

'------ start of revised code ------
Private Sub BtnClose_Click()

On Error GoTo Err_Handler

DoCmd.Close acForm, Me.Name, acSaveNo

Exit_Point:
Exit Sub

Err_Handler:
If Err.Number = 2501 Then
Resume Next
Else
MsgBox Err.Description, vbExclamation, "Error " & Err.Number
Resume Exit_Point
End If

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

HOWEVER, your code in Form_Unload doesn't make sense, because

(a) it says to always undo and close the form if it is dirty, and only ask
the question about saving data if the form is *not* dirty, and

(b) by the time the form's Unload event fires, any modified record will
already have been saved, so the form will *never* be dirty in the form's
Unload event. Period.

You can keep a record from being saved by cancelling the form's BeforeUpdate
event, or by attempting to control all the user actions that might result in
a record being saved. Those include pressing Shift+Enter, moving to a new
record, closing the form from the "X" button, closing he form using a menu
command, closing the form by clicking your BtnClose button, or even closing
the whole Access application. How many of those things do you want to try
to control?

If you only care about your own command button, then you can move your code
to the command button's Click event procedure, along these lines:

'------ start of revised code #2 ------
Private Sub BtnClose_Click()

On Error GoTo Err_Handler

If Me.Dirty Then

If MsgBox( _
"Do you want to exit without saving data?", _
vbYesNo + vbInformation, _
"Confirm") _
= vbYes
Then
' Undo modifications and close form.
Me.Undo
DoCmd.Close acForm, Me.Name, acSaveNo
Else
' Leave form open, with modified data.
End If

Else
' Form is unmodified, so just close it.
DoCmd.Close acForm, Me.Name, acSaveNo
End If

Exit_Point:
Exit Sub

Err_Handler:
If Err.Number = 2501 Then
Resume Next
Else
MsgBox Err.Description, vbExclamation, "Error " & Err.Number
Resume Exit_Point
End If

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

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