|
|
Controlling exiting of AccessQuestionI am trying to control exiting from Access. If a form is open, and there is
information in the fields, a normal close will bring up error 2169, which is: if
you close you will lose any usaved information, do you want to close? Yes/ No,
with No sending you back to the form with no changes, and Yes blanking the form
and closing Access. I want to do the same thing, only I want to control the
error message. If I capture the error in the Error event for the form, I can
replace the default message, but then I cannot give a choice to go back to the
form without changes. Access continues on to the unload event. I can stop the
exiting here, but by the time it gets to this event, all the data in my form has
disappeared. AnswerShawn, Private mblnCanClose As Boolean
Private mcolCtlsValues As Collection
Private Sub Form_Open(Cancel As Integer)
mblnCanClose = True
End Sub
Private Sub Form_BeforeUpdate(Cancel As Integer)
'Debug.Print "BeforeUpdate"
' put your data validation code here
'If DataArentValid(me) Then
' Cancel = True
'End If
End Sub
Private Sub Form_Error(DataErr As Integer, Response As Integer)
'Debug.Print DataErr
If DataErr = 2169 And mblnCanClose = True Then
If MsgBox("Would you like to discard edited data and close form ?", _
vbQuestion + vbYesNo) = vbYes Then
mblnCanClose = True
Response = acDataErrContinue
Else
mblnCanClose = False
Response = acDataErrContinue
' Get texbox control values to restore them in
' Unload event
Dim ctl As Control
Set mcolCtlsValues = New Collection
For Each ctl In Me.Controls
Select Case TypeName(ctl)
Case "TextBox":
mcolCtlsValues.Add ctl.Value, ctl.Name
Case Else
End Select
Next
End If
End If
End Sub
Private Sub Form_Unload(Cancel As Integer)
If mblnCanClose = False Then
mblnCanClose = True
Cancel = True
If Not mcolCtlsValues Is Nothing Then
Cancel = True
Dim ctl As Control
' Restore textboxs' control values
For Each ctl In Me.Controls
' On error resume next is used to suppress field
' validation errors while restoring their values
On Error Resume Next
Select Case TypeName(ctl)
Case "TextBox":
ctl.Value = mcolCtlsValues(ctl.Name)
Case Else
End Select
Err.Clear
Next
Set mcolCtlsValues = Nothing
End If
'MsgBox "You cannot close the dirty form this way"
End If
End Sub
Private Sub cmdClose_Click()
If Me.Dirty Then
MsgBox "Save edited data before closing form", vbExclamation
Else
mblnCanClose = True
DoCmd.Close
End If
End Sub
Copyright © 1999-2008 by Shamil Salakhetdinov. Original version is published here All rights reserved. |