Question : Open 1 form close another - only 1 form active at a time

I am using the similar code through out my database.  However it works some of the time - I am looking for a more stable code that I can have on all my forms including the switchboard.  I would like to have only 1 active form at a time.  So I need code that will close 1 form an open another.  I have tried the code in various places on the form code.  OnClose, OnOpen of the 2nd form etc.  w/a button to close the form an open the switchboard.  

I have tried the IsFormLoaded code - and I am using this in some instances - but this can get complicated when numerous forms are involved.

it chokes on "Then DoCmd.Close acForm," portion of the form

Since the Switchboard can be accessed by various forms  this is my current attempt.

 Private Sub Form_Open(Cancel As Integer)
' Minimize the database window and initialize the form.

    ' Move to the switchboard page that is marked as the default.
    Me.Filter = "[ItemNumber] = 0 AND [Argument] = 'Default' "
    Me.FilterOn = True
   
    For Each frm In Forms
        If frm.Name = "Switchboard" Then
            Exit Sub
        ElseIf frm.Name <> "Switchboard" Then
            DoCmd.Close acForm, frm.Name
        End If
    Next frm

End Sub

I do use global declartions - hence no dim of variables.

Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
Private Sub cmdClose_Click()
On Error GoTo Err_cmdClose_Click
 
 
    DoCmd.Close
    DoCmd.OpenForm "Switchboard", acNormal, , , , acDialog
 
Exit_cmdClose_Click:
    Exit Sub
 
Err_cmdClose_Click:
    MsgBox Err.Description
    Resume Exit_cmdClose_Click
    
End Sub
 
Private Sub Form_Open(Cancel As Integer)
    For Each frm In Forms
        If frm.Name <> "frmBuildWSNo_Structure" Then DoCmd.Close acForm, frm.Name
    Next frm
 
End Sub
Private Sub cmdReturnMasterWSNo_Click()
On Error GoTo Err_cmdReturnMasterWSNo_Click
 
    Dim stDocName As String
    Dim stLinkCriteria As String
 
    stDocName = "frmBuild Master WS No"
    DoCmd.OpenForm stDocName, , , stLinkCriteria
 
Exit_cmdReturnMasterWSNo_Click:
    Exit Sub
 
Err_cmdReturnMasterWSNo_Click:
    MsgBox Err.Description
    Resume Exit_cmdReturnMasterWSNo_Click
    
End Sub
Open in New Window Select All

Answer : Open 1 form close another - only 1 form active at a time

My code leaves the switchboard open.
If you want it to close then just mod the if...
If  frm.name <> me.name Then DoCmd.Close acForm, frm.Name
Random Solutions  
 
programming4us programming4us