Close a form and open another

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have lost my reference material on this subject and need some help.
My database window opens and closes with the switchboard form opening up.
Perfect!
NOW
What do I have to do to have each form close the previous form when it
opens? Do I have to code each form and if so what do I code and where?
Thanks
 
How you would close the previous form depends on how you open the new form.
If you open it from the previous form, you could just have that form close
itself once it opens the new form. If you open the new form from the
switchboard though, you'll have to make a check to see what form(s), if any
are open then close them. If you maintain hidden forms, you probably won't
want to close them. The following is an example of a routine that should
test for open forms and close them.

Example:
Dim i As Integer
For i = Forms.Count - 1 To 0 Step -1
If Forms(i).Name <> Me.Name And Forms(i).Name <> "Switchboard" And
Forms(i).Visible Then
DoCmd.Close acForm, Forms(i).Name, acSaveNo
End If
Next

Replace Switchboard with the name of your switchboard form if it stays open
all of the time. You could do this in the Open event of each form you open
or you could place this in a module and call the routine in the module,
passing the name of the form being opened and replacing Me.Name with the
passed information.

A For/Next loop doesn't work to close the forms because it starts at the low
index and counts upward. The problem is that Access rolls the index number
of the remaining open forms down one each time a form is closed, so you wind
up only closing about 1/2 of the forms if you use a For/Next loop.
 
Thanks for the reply I will practice with this.
I have a switchboard form and what I want to do is close or minimize this
form when it goes to one of the forms below. I then go from these forms to
others and want them to open and close as you navigate through.
I have never NEVER created a module (guess that is next in the learning
curve).

I will reply once I have tested out my abilities with your suggestion and
let you know how I make out.
 
put the routine you supplied in the form / open area of my code. Changed the
Switchboard name to the name of my switchboard form and received :
Compile Error:
Syntax Error

This is the code I put in the code are on the form that opened from the cmd
button on the switchboard. It is when this form opens that I would like the
switchboard form to close.
Private Sub Form_Open(Cancel As Integer)
Dim i As Integer
For i = Forms.Count - 1 To 0 Step -1
If Forms(i).Name <> Me.Name And Forms(i).Name <> "Main" And
Forms(i).Visible Then
DoCmd.Close acForm, Forms(i).Name, acSaveNo
End If
Next


End Sub

For the purpose of this test lets say my switchboard form is called Main and
the form I open from here is called Second.

Suggestions to correct????
 
Which line was the error on? Be aware that the line starting with If and the
line ending with Then have been wrapped by the newsreader. It should
actually be one line.
 
Back
Top