How to close all open forms and reopen initial startup form

S

Steve G.

I followed article 210297 on 'HOW TO: Detect User Idle Time or Inactivity in
Access 2000' and it works fine, but instead of closing the access
application entirely, I need to close all open forms and then reopen the
initial startup form. Can anyone help with a solution ?
Steve
 
A

Allen Browne

This kind of thing will close all forms:
For i = Forms.Count - 1 to 0 Step -1
DoCmd.Close acForm, Forms(i).Name
Next
Use OpenForm to open the original.

What I do is to set the On Close property of all forms and reports to:
=Keep1Open([Form])
or for reports:
=Keep1Open([Report])
Paste the code below into a standard module. What happens is that when the
user closes the last visible form or report, it opens the switchboard.

(Note that you don't set the On Close property for the switchboard.)

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 strDoc 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
strDoc = 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 <> strDoc 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 <> strDoc 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 "Switchboard"
End If

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

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

Jared

Hi Steve,
I have a table called dashboard_settings that stores names, sizes and
positions of forms that i wish to open/close as a group. The form name is
stored in a field frm_ID". In looking for a way to do what your after, I
found your post. I've since used this to close all open forms. (may not be
programmatically correct, but it works)

'close any open dashboard forms
Dim rst As Recordset

Set rst = DBEngine(0)(0).OpenRecordset("dashboard_settings")

'go to first record
rst.MoveFirst

Do Until rst.EOF
DoCmd.Close acForm, rst!frm_id
rst.MoveNext
Loop
rst.Close

you can then use teh docmd.openform to open your initial form.

Jared
 
S

Steve G.

Thanks Jared...I'll give it a try !
Steve

Jared said:
Hi Steve,
I have a table called dashboard_settings that stores names, sizes and
positions of forms that i wish to open/close as a group. The form name is
stored in a field frm_ID". In looking for a way to do what your after, I
found your post. I've since used this to close all open forms. (may not be
programmatically correct, but it works)

'close any open dashboard forms
Dim rst As Recordset

Set rst = DBEngine(0)(0).OpenRecordset("dashboard_settings")

'go to first record
rst.MoveFirst

Do Until rst.EOF
DoCmd.Close acForm, rst!frm_id
rst.MoveNext
Loop
rst.Close

you can then use teh docmd.openform to open your initial form.

Jared
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top