
Problem with Controlled Closing of a Form when exiting throught the Window-Close-Button
I have been having some trouble with this exact same
problem, and am now getting somewhere with solving it. I
have done what sounds like a similar thing to you, I
prompt for saving in the Before_Update event, and if the
answer is vbCancel I cancel the event. I also set form
level variables here to tell me later on that I pressed
Cancel, and also to store all the current values of the
record (to avoid the changed data getting wiped). Then I
put a piece of code in the Form_Error event:
If DataErr = 2169 Then
Response = False
End If
This cancels the Access generated error message.
In the On_Unload event I put code like this:
If Not g_blnCloseCompany Then
txtCompany = g_strCompany
g_blnCloseCompany = True
Cancel = True
End If
This way, you can override the close icon on the form
keeping any changed data intact. This seems to work for
me, although I will have to test it some more to be sure.
If you need any help with my suggestions, feel free to
email me - also if you find anything wrong with it or
have any suggestions please let me know.
Andy
Quote:
>-----Original Message-----
>Thank you for your reply.
>I do know that I can use the form UnLoad event to cancel
the form's closing.
>My problem is that when you cancel the BeforeUpdate
event, Access raises the
Quote:
>error=2169 and runs into the Form_error event where it,
by default, lets you
Quote:
>decide whether to close the form and discard all the
changes or keep the
>form open and continue to work.
>How can I customize my own MsgBox to do this?
>If you cancel the BeforeUpdate event all the changes are
lost until you
>reach the UnLoad event!!!
>Thanks,
>Dirk Schlarb
Newsbeitrag
>> Take a look at the form UnLoad event. It fires when
you click the form's
Quote:
>close
>> button and is cancelable.
>> --
>> Sco
>> M.L. "Sco" Scofield, MCSD, MCT, MCP, MSS, Access MVP
>> Useful Metric Conversion #13 of 19: 3 1/3 tridents = 1
decadent
>> Miscellaneous Access and VB "stuff" at www.ScoBiz.com
>> > Hi,
>> > I have a Form where I need to control the data
before the form closes. I
Quote:
>put
>> > in the Form_BeforeUpdate something like:
>> > Private Sub Form_BeforeUpdate(Cancel As Integer)
>> > On Error Resume Next
>> > Dim strMsg As String
>> > Dim strTitle As String
>> > 'Message
>> > If Me.NewRecord Then
>> > strMsg = "New Record?"
>> > strTitle = "Save?"
>> > Me.Angelegt_am = Now()
>> > Me.Angelegt_von = CurrentUser()
>> > Else
>> > strMsg = "Save changes?"
>> > strTitle = "Save changes?"
>> > Me.Aktualisiert_am = Now()
>> > Me.Aktualisiert_von = CurrentUser()
>> > End If
>> > If MsgBox(strMsg, vbYesNo + vbQuestion,
strTitle) = vbYes Then
Quote:
>> > 'Check the Data.
>> > If Not Check_Form_Logic() Then
>> > Cancel = True
>> > End If
>> > Else
>> > 'Undo
>> > Me.Undo
>> > End If
>> > The Check_Form_Logic() - Funktion returns TRUE/FALSE
in relation of the
>> > correct data. When I exit the form throught the
Window-Close-Button and
Quote:
>the
>> > Procedur Cancels the Update, Access runs into the
Form_error-Procedure
Quote:
>> > raising the error 2169 -"You can't save this Record
>> > know.....Access...blablabla".
>> > How can I implement my own Custom Message-Box where
to choose whether I
>want
>> > to close the Form and discard all changes or keep it
open and continue
>to
>> > work. Maybe there is a simple API-Call to prevent
the window from
>closing.
>> > I do know about the examples in Access-Developer-
Books, where you are
Quote:
>> > suppose to use the Form_Unload-Procedure, but until
the Form reaches
>this
>> > Procedure, all the changes the user made are
lost!!!!!!!!!!
>> > Thank you in advance,
>> > Dirk Schlarb
>.