Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using BeforeUpdate to run code: problems closing form 1

Status
Not open for further replies.

irishandy

Programmer
Nov 18, 2003
39
IE
I've been going to hell and back with this one.

I've got a form that I'm using for data entry. Users enter data and then press a "Log Case" button to enter their userID and a timestamp. It is not essential that all cases be logged straight away but it is essential that a user be prompted to log a record if they try to navigate between records or to close the form.

I'm using the form's beforeUpdate event to execute this code and it's working fine for navigation between records. The user is prompted with a simple message box and asked if they are sure they want to leave the record unlogged. If they say no then whatever they were trying to go (e.g. go to next record) is cancelled (Cancel = True). If they say yes then they can carry on.

As I said, this is working perfectly fine for the navigation buttons. Closing the form however is getting me into trouble. There are two options:

1) Use the close button on the top right of the form.
2) Make an exit button on the form.

Using 1) in the case where a user does NOT want to leave the record unlogged produces the following error message:

"You can't save this record at this time

[FormName] may have encountered an error while trying to save a record. If you close this object now, the data changes you made will be lost. Do you want to close the database object anyway?"

Therefore I decided to use 2). The problem this time is that the DoCmd.Close command appears to be able to ignore the Cancel = True statement in the BeforeUpdate function.

Any and all assistance would be hugely appreciated because I'm going nuts.
 
Hi!

What code are you using in the exit button?



Jeff Bridgham
bridgham@purdue.edu
 
Just the standard exit code:

Private Sub Button_Exit_Click()
On Error GoTo Err_Button_Exit_Click

DoCmd.Close

Exit_Button_Exit_Click:
Exit Sub

Err_Button_Exit_Click:
MsgBox Err.Description
Resume Exit_Button_Exit_Click

End Sub
 
Hi!

Try this:

Private Sub Button_Exit_Click()
On Error GoTo Err_Button_Exit_Click
If MsgBox("Are you sure you want to leave without closing?", vbYesNo) = vbYes Then
DoCmd.Close
End If

Exit_Button_Exit_Click:
Exit Sub

Err_Button_Exit_Click:
MsgBox Err.Description
Resume Exit_Button_Exit_Click

End Sub

hth


Jeff Bridgham
bridgham@purdue.edu
 
I already tried a variation of that (it was identical except it said "logging this record" instead of "closing").

It seemed like the obvious solution however the form's BeforeUpdate() kicks in after you say no.

Here's the sequence of events...

DoCmd.Close

BeforeUpdate then kicks in and displays this message:

"This case has not been logged. Are you sure you want to leave it unlogged?".

If you click Yes, it exits as it should do.

If you click No, it also exits. However it should not exit, it should cancel.
 
Hi!

Try my code as it is and see what happens.

BTW, if you are using a close button don't forget to get rid of the close button at the top of the form.

hth


Jeff Bridgham
bridgham@purdue.edu
 
Jebry,

I tried your code as is - pasted it straight in. Same result as with mine.

What I've decided to do as an interim solution is to use the close button in the corner (i.e. solution #1) but to trap the error message and supress it. It's error 2169 - as far as I know it's a known bug in Access 97.

It works, but this is far from an ideal solution!

There are days when I wish I'd become a journalist instead...
 
Hi!

I think I see the problem, first add a form level variable:

Private bolClosingForm As Boolean

In the Form_Load procedure put

bolClosingForm = False

In the button click procedure, add inside the If

bolClosingForm = True

Then, in your BeforeUpdate procedure wrap all of your code in:

If bolClosingForm = False Then
YourCode
End If

hth


Jeff Bridgham
bridgham@purdue.edu
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top