Need X to act like my "Close without Save" button.

H

h2fcell

Hello,
I’m stuck and I can’t get loose. ïŠ
I have a form that opens with the following “On Open†event:

Private Sub Form_Open(Cancel As Integer)
DoCmd.Maximize
DoCmd.GoToRecord , "", acNewRec
DoCmd.Requery "lboItems"
End Sub

If the user makes no changes “form not dirty†and clicks the X on the upper
right corner, the form closes with no problem. If the user makes changes
“form dirty†and clicks X, the form closes and the record is saved, this is
not good.

I’ve created two buttons on the form that work well.
“Save Record†with the following “On Click’ event:

Private Sub cbSaveRec_Click()
Dim myBookingID As String
myBookingID = Me.cboBookingID
DoCmd.GoToRecord , , acNewRec
Me.cboBookingID = myBookingID
Me.lboItems.Requery
Me.cboPaxName.Requery
Me.cboPaxName = Nz(DMin("Name", "qryPaxName"), "")
Me.cboUSDepDate.Requery
Me.cboUSDepDate = Nz(DMin("UsDepDate", "qryDepDate"), "")
Me.tboPaxName = Me.cboPaxName
Me.tboUSDepDate = Me.cboUSDepDate
End Sub

“Close without Saving†with the following “On Click’ event:

Private Sub cbClose_Click()
If Me.Dirty = True Then
DoCmd.RunCommand acCmdUndo
End If
DoCmd.Close acForm, "frmComplaintsEntry", acSaveNo
End Sub

I would like the X to work like my “Close without Saving†button, but when I
added the code to the “On Close†event or the “On Unload’ event I get:
Run-time error ‘2501’:
The Close Action was canceled.

Any suggestions are greatly appreciated.
Thanks.
 
T

theDBguy

Hi,

One way to do this is by using a Global Variable to indicate whether it is
safe to save the record or not. For example, you could create a Global
Variable in your Form's Class Module as:

Dim blnOK2Save As Boolean

Then in the Open event of the form, you will set it to False

blnOK2Save = False

In your cbSaveRec button, you would modify the Click event to:

blnOK2Save = True
....
DoCmd.GoToRecord , , acNewRec
blnOK2Save = False
....

Then in your Form's BeforeUpdate event, you will check for the variable,
such as:

If Not blnOK2Save Then
Me.Undo
Cancel = True
End If

Not sure if I missed anything but I hope the above helps...
 
H

h2fcell

I hope this "One way" is the best way to do this.
Thanks for the reply. I'll give it a try and let you know how it goes.
 
T

theDBguy

Good luck with it!


h2fcell said:
I hope this "One way" is the best way to do this.
Thanks for the reply. I'll give it a try and let you know how it goes.
 
T

theDBguy

Hi Linq,

Not sure how this forum works, but did you mean to reply to me?

Also, are you missinglinq on UA?

Cheers.
 

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