Close all forms - except one!

fraser_lindsay

Access wannabe
Local time
Today, 22:39
Joined
Sep 7, 2005
Messages
218
Hi,

I have been using this code to close all open forms when loading another form. I launch it form a command button and it keeps things tidy.

However, now I would like to exclude one form - I want to keep one form open, but hidden, no matter what.

How do I write that into the code?

Code:
'This code closes all open forms in the current project and then opens the named form in quotations
Dim obj As Object
Dim strName As String
For Each obj In Application.CurrentProject.AllForms
If obj.Name <> "Your Form" Then
DoCmd.Close acForm, obj.Name, acSaveNo
End If
Next obj


DoCmd.OpenForm "Switchboard", acNormal, "", "", acEdit, acNormal


I was thinking it would be in this line

Code:
If obj.Name <> "Your Form" Then

I tried "frm*" to only close forms titled frm-something and removed the 'frm' from the form I want to keep open.

That's didn't work .

Can anyone help please?

Thanks
 
It is this line
If obj.Name <> "Your Form" Then

Try adding in
debug.print obj.Name

To see a list of all form names and see if your form is there or not.... You can then find that list by pressing CTRL + G.

The point probably is your are using the "Caption" instead of the actual form name!

Good luck.
 
Do you mean like this:

Code:
Private Sub cmdHome_Click()

'This code closes all open forms in the current project and then opens the named form in quotations
Dim obj As Object
Dim strName As String
For Each obj In Application.CurrentProject.AllForms
If obj.Name <> Forms![StartFilter] Then

Else
DoCmd.Close acForm, obj.Name, acSaveNo
End If
Next obj


DoCmd.OpenForm "Switchboard", acNormal, "", "", acEdit, acNormal


End Sub


It doesn't like that for my object name, type mismatch.
 
For Each obj In Application.CurrentProject.AllForms
debug.print obj.Name
If obj.Name <> "" Then

Then press CTRL + G after running your code...
Find the form you do not want to close and insert its name in the "" part.
 
Ok, the code runs but no no forms close. I press CRTL + G and I go to the code window. There's a list of objects in the 'immediate' box at the bottom.

The form I don't want closed is called "StartFilter".

It's in the quotations but it doesn't work.

Is there perhaps more code to define NOT to close that form but all others?
 
If obj.Name <> "StartFilter" Then

Else
DoCmd.Close acForm, obj.Name, acSaveNo
End If

Guess what above code does?? Close ... ONLY startfilter...
What you want is if = "StartFilter" then do nothing else close...
or If <> "StartFilter" then close

Not what you have but just the reverse.
 
Fantastic, got there in the end. Thanks for your help. I thought I was close but as ever my basic coding skills were too basic. I never get the syntax correct.

This works:

Code:
Private Sub cmdHome_Click()

'This code closes all open forms in the current project and then opens the named form in quotations
Dim obj As Object
Dim strName As String
For Each obj In Application.CurrentProject.AllForms
Debug.Print obj.Name
If obj.Name = "StartFilter" Then

Else
DoCmd.Close acForm, obj.Name, acSaveNo
End If
Next obj


DoCmd.OpenForm "Switchboard", acNormal, "", "", acEdit, acNormal


End Sub
 

Users who are viewing this thread

Back
Top Bottom