Handling "exit" without error msg

  • Thread starter Thread starter SillySally
  • Start date Start date
S

SillySally

Hi. When code expects an action like clicking a button
and the user chooses instead to just close the window, I
get an error message because I have no code to handle
these exceptions. Could you give me an idea of what I
would need to add to make the exit smooth? Kind of an
On Error GoTo and what that GoTo might look like?

Thanks.
 
This is a sample of what I use. Keep in mind that when I reference
GeneralErrorHandler this is a Public Sub that I have created in a module.
Private Sub Form_Unload(Cancel As Integer)

On Error GoTo HandleError

'close the recordset and free the memory
rsVacRequest.Close
Set rsVacRequest = Nothing

Exit Sub

HandleError:
GeneralErrorHandler Err.Number, Err.Description, VACREQUEST_FORM,
"Form_Unload"
Exit Sub

End Sub


***************Below is Public Sub in module**********
Public Sub GeneralErrorHandler(lngErrNumber As Long, strErrDesc As String,
strModuleSource As String, strProcedureSource As String)

On Error Resume Next

Dim strMessage As String

'build the error message string from the parameters passed in
strMessage = "An error has occurred in the application."
strMessage = strMessage & vbCrLf & "Error Number: " & lngErrNumber
strMessage = strMessage & vbCrLf & "Error Description: " & strErrDesc
strMessage = strMessage & vbCrLf & "Module Source: " & strModuleSource
strMessage = strMessage & vbCrLf & "Procedure Source: " &
strProcedureSource

'display the message to the user
MsgBox strMessage, vbCritical


Exit Sub

End Sub
 
Thanks- I'll take a look at this. It looks as though you
are informing the user that an error occurred. I'm
thinking I just want the code to "close down". Meaning
that if the user closed the window instead of clicking
the button, they most likely decided not to run
something. Rather than having the user see an error
message (either one Access gives or one I create), I'd
just like Access to "soft close" so that the user doesn't
even know there was an issue. Not sure if that makes
sense...
 
On Error Resume Next will bypass errors and not throw error messages,
assuming you're speaking of VBA code and not Macros ... thus, you would do
something like this:

Sub Form_Close()

On Error Resume Next
<your code here>

End Sub

Of course, if you NEED to handle errors in your exit routine, you can't use
this. Instead, you would need to check for conditions that could cause an
Error message to be generated and then respond accordingly. For example, if
your code expects a value in varMyVariable, and the user clicks the Exit
button in the upper right corner of the screen, you would need to determine
if you SHOULD handle that error ... how you do that depends on how your code
is written, but in general you shouldn't base code on events over which you
can't really control.

One method to "catch" users closing the window is to force them to close the
window via a button you provide (and to which you can respond). To do this,
add a flag to the General Declarations section of the form and check it's
value in the Form's Unload event:

[General Declarations]
Private mflgClose As Boolean

Sub Form_Unload(Cancel As True)
If Not mflgClose Then
msgbox "Please click Close to exit this form"
Cancel = True
End If
End Sub

On your form, place a button named btnClose, and add this to the Click
Event:

Sub btnClose_Click()
mflgClose = True
End Sub

Now as long as mglClose = False the form won't close unless the user clicks
the Close button. You can then trap for your errors BEFORE the form closes
(which is often too late).
 
Thanks- I'll give it a try!
-----Original Message-----
On Error Resume Next will bypass errors and not throw error messages,
assuming you're speaking of VBA code and not Macros ... thus, you would do
something like this:

Sub Form_Close()

On Error Resume Next
<your code here>

End Sub

Of course, if you NEED to handle errors in your exit routine, you can't use
this. Instead, you would need to check for conditions that could cause an
Error message to be generated and then respond accordingly. For example, if
your code expects a value in varMyVariable, and the user clicks the Exit
button in the upper right corner of the screen, you would need to determine
if you SHOULD handle that error ... how you do that depends on how your code
is written, but in general you shouldn't base code on events over which you
can't really control.

One method to "catch" users closing the window is to force them to close the
window via a button you provide (and to which you can respond). To do this,
add a flag to the General Declarations section of the form and check it's
value in the Form's Unload event:

[General Declarations]
Private mflgClose As Boolean

Sub Form_Unload(Cancel As True)
If Not mflgClose Then
msgbox "Please click Close to exit this form"
Cancel = True
End If
End Sub

On your form, place a button named btnClose, and add this to the Click
Event:

Sub btnClose_Click()
mflgClose = True
End Sub

Now as long as mglClose = False the form won't close unless the user clicks
the Close button. You can then trap for your errors BEFORE the form closes
(which is often too late).
Hi. When code expects an action like clicking a button
and the user chooses instead to just close the window, I
get an error message because I have no code to handle
these exceptions. Could you give me an idea of what I
would need to add to make the exit smooth? Kind of an
On Error GoTo and what that GoTo might look like?

Thanks.


.
 
Back
Top