Welcome to SPN

Register and Join the most happening forum of Sikh community & intellectuals from around the world.

Sign Up Now!

Close all open forms except 1

Discussion in 'Information Technology' started by Simon, Jul 28, 2006.

  1. Simon

    Simon
    Expand Collapse
    Guest

    I have a lot of different forms that can be opened from a form called "Client
    screen" which all contain data relative to that client. I would like the
    main "Client screens" form close button, to close all forms except form "Main
    Menu". It wouldn't be a problem to close the form "Main Menu" and reopen it
    but I don't know how to refer to all forms to close them.
    I have tried the response to the question "Check and close all forms except
    the Switchboard" but the code didn't work. I do not have a Switchboard, just
    a standard form called "Main Menu". Is this the problem?
    I am using Access 2003 and would be most appreciative of any help
     
  2. Loading...

    Similar Threads Forum Date
    India Who says we have skeletons only in our closets? Breaking News Aug 22, 2014
    India After Badrinath, Kedarnath now Sikh shrine Hemkunt Sahib closed temporarily Breaking News Jun 28, 2013
    Grammer / Vyakarn Fateh multimedia forced to close for lack of funds Language, Arts & Culture Nov 21, 2012
    General The Closer Videos Jul 26, 2012
    Close Brush with the Law, a Personal Story Business, Lifestyle & Leisure Sep 24, 2011

  3. Douglas J. Steele

    Douglas J. Steele
    Expand Collapse
    Guest

    Dim lngLoop As Long

    For lngLoop = (Forms.Count - 1) To 0 Step -1
    If Forms(lngLoop).Name <> "Main Menu" Then
    DoCmd.Close acForm, Forms(lngLoop).Name, acSaveNo
    End If
    Next lngLoop

    --
    Doug Steele, Microsoft Access MVP
    http://I.Am/DougSteele
    (no private e-mails, please)


    "Simon" <Simon@discussions.microsoft.com> wrote in message
    news:B7CC37AE-F0AF-470B-BB30-2C9E8BC94A9F@microsoft.com...
    >I have a lot of different forms that can be opened from a form called
    >"Client
    > screen" which all contain data relative to that client. I would like the
    > main "Client screens" form close button, to close all forms except form
    > "Main
    > Menu". It wouldn't be a problem to close the form "Main Menu" and reopen
    > it
    > but I don't know how to refer to all forms to close them.
    > I have tried the response to the question "Check and close all forms
    > except
    > the Switchboard" but the code didn't work. I do not have a Switchboard,
    > just
    > a standard form called "Main Menu". Is this the problem?
    > I am using Access 2003 and would be most appreciative of any help
     
  4. Allen Browne

    Allen Browne
    Expand Collapse
    Guest

    This code will close all forms except the main menu:
    Dim i As Integer
    For i = Forms.Count - 1 to 0
    If Forms(i).Name <> "Main Menu" Then
    DoCmd.Close acForm, Forms(i).Name
    End If
    Next

    The Forms collection contains all open forms. The code loops backwards
    through this collection, because the loop gets confused if you close the
    lower count first.

    Be aware that if any form is dirty with an entry that cannot be saved (e.g.
    a required field is missing), Access just loses the entry with no warning.
    More info in:
    Losing data when you close a form
    at:
    http://allenbrowne.com/bug-01.html

    You could also attack this issue from the other direction: let the user have
    as many forms and reports open as they like but once they close the last
    one, you automatically open your Main Menu again:

    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 "Main Menu"
    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.
    MsgBox "Error " & Err.Number & ": " &Err.Description
    End If
    Resume Exit_Keep1Open
    End Function

    --
    Allen Browne - Microsoft MVP. Perth, Western Australia.
    Tips for Access users - http://allenbrowne.com/tips.html
    Reply to group, rather than allenbrowne at mvps dot org.

    "Simon" <Simon@discussions.microsoft.com> wrote in message
    news:B7CC37AE-F0AF-470B-BB30-2C9E8BC94A9F@microsoft.com...
    >I have a lot of different forms that can be opened from a form called
    >"Client
    > screen" which all contain data relative to that client. I would like the
    > main "Client screens" form close button, to close all forms except form
    > "Main
    > Menu". It wouldn't be a problem to close the form "Main Menu" and reopen
    > it
    > but I don't know how to refer to all forms to close them.
    > I have tried the response to the question "Check and close all forms
    > except
    > the Switchboard" but the code didn't work. I do not have a Switchboard,
    > just
    > a standard form called "Main Menu". Is this the problem?
    > I am using Access 2003 and would be most appreciative of any help
     
  5. Simon

    Simon
    Expand Collapse
    Guest

    Thank you both very much.

    "Allen Browne" wrote:

    > This code will close all forms except the main menu:
    > Dim i As Integer
    > For i = Forms.Count - 1 to 0
    > If Forms(i).Name <> "Main Menu" Then
    > DoCmd.Close acForm, Forms(i).Name
    > End If
    > Next
    >
    > The Forms collection contains all open forms. The code loops backwards
    > through this collection, because the loop gets confused if you close the
    > lower count first.
    >
    > Be aware that if any form is dirty with an entry that cannot be saved (e.g.
    > a required field is missing), Access just loses the entry with no warning.
    > More info in:
    > Losing data when you close a form
    > at:
    > http://allenbrowne.com/bug-01.html
    >
    > You could also attack this issue from the other direction: let the user have
    > as many forms and reports open as they like but once they close the last
    > one, you automatically open your Main Menu again:
    >
    > 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 "Main Menu"
    > 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.
    > MsgBox "Error " & Err.Number & ": " &Err.Description
    > End If
    > Resume Exit_Keep1Open
    > End Function
    >
    > --
    > Allen Browne - Microsoft MVP. Perth, Western Australia.
    > Tips for Access users - http://allenbrowne.com/tips.html
    > Reply to group, rather than allenbrowne at mvps dot org.
    >
    > "Simon" <Simon@discussions.microsoft.com> wrote in message
    > news:B7CC37AE-F0AF-470B-BB30-2C9E8BC94A9F@microsoft.com...
    > >I have a lot of different forms that can be opened from a form called
    > >"Client
    > > screen" which all contain data relative to that client. I would like the
    > > main "Client screens" form close button, to close all forms except form
    > > "Main
    > > Menu". It wouldn't be a problem to close the form "Main Menu" and reopen
    > > it
    > > but I don't know how to refer to all forms to close them.
    > > I have tried the response to the question "Check and close all forms
    > > except
    > > the Switchboard" but the code didn't work. I do not have a Switchboard,
    > > just
    > > a standard form called "Main Menu". Is this the problem?
    > > I am using Access 2003 and would be most appreciative of any help

    >
    >
    >
     

Share This Page