Check if All Forms are closed

willbo987

Registered User.
Local time
Today, 11:59
Joined
Oct 30, 2012
Messages
43
Hi there,

I am creating a few forms one of which is the MAIN FORM that closes upon opening another form.

What i am looking for is when another form closes, if no other forms are open, then open the MAIN FORM again.

Can I use this:
If CurrentProject.AllForms("BUT KEEP THIS EMPTY").IsLoaded Then ???

Thanks for your help in advance
 
what happens when you try it?
 
Not working unfortunately,

This is my code:

Private Sub Command98_Click()
DoCmd.Close acForm, "frmCandidates", acSaveYes
If CurrentProject.AllForms("").IsLoaded Then
DoCmd.OpenForm (frmMainForm)
End If
End Sub

Also Tried removing the quotes in the brackets:
Private Sub Command98_Click()
DoCmd.Close acForm, "frmCandidates", acSaveYes
If CurrentProject.AllForms().IsLoaded Then
DoCmd.OpenForm (frmMainForm)
End If
End Sub
 
Im getting confused here. I need it back to front dont I lol.

I need it If NO FORMS are open, then open main form
 
you have to specify a form name, or loop through the collection

alternatively, try the forms collection - this is populated with open forms only
 
I have so many forms, any quicker way without listing them all!?
 
You may try something like,
Code:
Private Sub Command98_Click()
    Dim objAccObj As AccessObject
    Dim objProj As Object, openFrm As Boolean
    openFrm = True
    
    Set objProj = Application.CurrentProject
    For Each objAccObj In objProj.AllForms
        If objAccObj.IsLoaded() And objAccObj.Name <> Me.Name And objAccObj.Name <> "frmMainForm" Then
            DoCmd.OpenForm "frmMainForm"
        End If
    Next objAccObj
End If
 
Last edited:
Thanks pr2-eugin,

But no goods, get stuck at:
If objAccObj.IsLoaded() And objAccObj.Name <> Me.Name And objAccObj.Name <> frmMainForm Then
 
Getting stuck how? I missed quotes around the form name. Please check my edited code.
 
Still getting stuck (see attachment),

This is the codes as it stands:

Private Sub Command98_Click()
DoCmd.Close acForm, "frmCandidates", acSaveYes
Dim objAccObj As AccessObject
Dim objProj As Object, openFrm As Boolean
openFrm = True

Set objProj = Application.CurrentProject
For Each objAccObj In objProj.AllForms
If objAccObj.IsLoaded() And objAccObj.Name <> Me.Name And objAccObj.Name <> "frmMainForm" Then
DoCmd.OpenForm "frmMainForm"
End If
Next objAccObj
End Sub
 

Attachments

  • printscreen.png
    printscreen.png
    10.8 KB · Views: 139
Please use Code Tags when posting VBA Code

Now you will be getting the error for several reasons, might not be even to do with this routine.

Although one problem you will face is referencing the form that you just closed, you have closed the form, which means Me.Name is not accessible. Try closing the form towards the end, like
Code:
Private Sub Command98_Click()
    Dim objAccObj As AccessObject
    Dim objProj As Object

    Set objProj = Application.CurrentProject
    For Each objAccObj In objProj.AllForms
        If objAccObj.IsLoaded() And objAccObj.Name <> Me.Name And objAccObj.Name <> "frmMainForm" Then
            DoCmd.OpenForm "frmMainForm"
        End If
    Next objAccObj
    
    DoCmd.Close acForm, "frmCandidates", acSaveYes
End Sub
Now, check where your code actually fails.
 
OK!!

The Form that I close closes without any problems. At this point there are no other forms open.
However the main Form does not open.

We are half way there :D
 
No hidden forms either? The forms might be loaded, but hidden. If that is the case the code above will not open the Main Form.
 
hmmm no hidden forms,

I think simple is more in this case and just going to refer to the main forms that a user may use:

Code:
DoCmd.Close acForm, "frmCandidates", acSaveYes
If CurrentProject.AllForms(frmCompanies).IsLoaded Then
Exit Sub

ElseIf CurrentProject.AllForms(frmCourses).IsLoaded Then
Exit Sub

Else
DoCmd.OpenForm "frmMainForm"

End If
End Sub

Just means I will have to manually add/change the code as more main forms are added in the future

Appreciate all your help though!!
 
So, if you run this code what happens?
Code:
Private Sub Command98_Click()
    Dim objAccObj As AccessObject
    Dim objProj As Object

    Set objProj = Application.CurrentProject
    For Each objAccObj In objProj.AllForms
        If objAccObj.IsLoaded() And objAccObj.Name <> Me.Name Then [COLOR=Green]'And objAccObj.Name <> "frmMainForm" Then[/COLOR]
            MsgBox "I am OPEN - " & objAccObj.Name
           [COLOR=Green] 'DoCmd.OpenForm "frmMainForm"[/COLOR]
        End If
    Next objAccObj
    
    DoCmd.Close acForm, "frmCandidates", acSaveYes
End Sub
 
Okay, figured my stupidity, :banghead: try this.
Code:
Private Sub Command98_Click()
    Dim objAccObj As AccessObject
    Dim objProj As Object, openFrm As Boolean
    openFrm = True

    Set objProj = Application.CurrentProject
    For Each objAccObj In objProj.AllForms
        If objAccObj.IsLoaded() And objAccObj.Name <> Me.Name And objAccObj.Name <> "frmMainForm" Then
            openFrm = False
            Exit For
        End If
    Next objAccObj
    
    If openFrm Then _ 
        DoCmd.OpenForm "frmMainForm"
        
    DoCmd.Close acForm, "frmCandidates", acSaveYes
End Sub
 
Well I started off with the logic of introducing a flag that could tell if any other form is open other than the two we are concerned about. Somewhere in the middle, I got distracted.

Now getting back to the technical reason of why it did not work. (Previous code) The loop checked for all forms, if anything is loaded. If so, it opens the Main form. What we needed was (the working code), loop through all the forms available. See if there is any form that is open, if it is open we do not need to open the main form. So we set a flag (to false), when any other form is open, we do not want the main form to be open. So there you go.

Loop through the form collection, see if there is any other form is loaded other than the current form you are in, and the main form. If none of them are Loaded you ask the Main form to be opened. Simples.
 

Users who are viewing this thread

Back
Top Bottom