Error on Closing a form

  • Thread starter Thread starter Angel_G
  • Start date Start date
A

Angel_G

Hi There!
I have a recordsform that has an OnClose event where I unhide the
Switchboard form
Private Sub Form_Close()
Forms![Switchboard].Visible = True
End Sub
But when the users decide to close the application instead of the
recordsform, Access gives a Runtime error 2450 with a debug option. Of
course I do not want the users to be able to debug. Is there a way to
suppress this message? or is there a better way to do this?
Thank you in advance
 
Use error handling to trap error 2046.

Here's what I use. The code opens/activates the switchboard when the user
closes all other visible forms and reports.

Set the On Close property of all forms except the switchboard to:
=Keep1Open([Form])
and for reports:
=Keep1Open([Report])
Note: it's exactly that: do not substitute the name of the report or add
quotes.

------------------code starts------------------------
Public Function Keep1Open(objMe As Object)
On Error GoTo Err_Keep1Open
'Purpose: Open the Switchboard if nothing else is visible.
'Argument: The object being closed.
'Return: None.
'Usage: In the OnClose property of forms and reports:
' =Keep1Open([Form])
' =Keep1Open([Report])
Dim sName As String 'Name of the object being closed.
Dim lngObjType As Long 'acForm or acReport
Dim bFound As Boolean 'Flag not to open the switchboard.
Dim frm As Form 'an open form.
Dim rpt As Report 'an open report.

'Initialize
sName = objMe.Name
If TypeOf objMe Is Form Then
lngObjType = acForm
ElseIf TypeOf objMe Is Report Then
lngObjType = acReport
End If

'Any other visible forms?
For Each frm In Forms
If frm.Visible Then
If frm.Name <> sName Or lngObjType <> acForm Then
bFound = True
Exit For
End If
End If
Next

'Any other visible reports?
If Not bFound Then
For Each rpt In Reports
If rpt.Visible Then
If rpt.Name <> sName Or lngObjType <> acReport Then
bFound = True
Exit For
End If
End If
Next
End If

'If none found, open the switchboard.
If Not bFound Then
DoCmd.OpenForm "fSwitchboard"
End If

Exit_Keep1Open:
Set frm = Nothing
Set rpt = Nothing
Exit Function

Err_Keep1Open:
If Err.Number <> 2046& Then 'OpenForm is not available when closing
database.
Call LogError(Err.Number, Err.Description, conMod & ".Keep1Open()")
End If
Resume Exit_Keep1Open
End Function
------------------code ends------------------------
 
Back
Top