Allow only one form open at a time

G

Guest

I’m looking for the most efficient way to allow only form or one report open
at a time. For example, in a database which has three forms: If the user
tries to open FormC, a function(?) checks to see if FormA or FormB are open
and, if so, closes the open form and then opens FormC. I would have a similar
function for reports.

I’m assuming a function is the best way to handle this, but I’d like one
which doesn’t require having to hard code the names of each form (or report)
in the database.

I should point out that all forms and reports are accessible only through a
custom toolbar. So, the best method might be to put the name of the function
[e.g., =CheckForm()] in the On Action event of each button or menu which
opens a form.

Here’s the general idea:

###

Function CheckForm()
If [any form but the one you’re trying to open].IsLoaded = True Then
DoCmd.Close acForm, “[the form which is loaded]â€
DoCmd.OpenForm "[name of form you’re trying to open]"
Else
DoCmd.OpenForm "[name of form you’re trying to open]"
End If
End Function

###

Thanks.

Kurt
 
D

Dirk Goldgar

Kurt said:
I'm looking for the most efficient way to allow only form or one
report open at a time. For example, in a database which has three
forms: If the user tries to open FormC, a function(?) checks to see
if FormA or FormB are open and, if so, closes the open form and then
opens FormC. I would have a similar function for reports.

I'm assuming a function is the best way to handle this, but I'd like
one which doesn't require having to hard code the names of each form
(or report) in the database.

I should point out that all forms and reports are accessible only
through a custom toolbar. So, the best method might be to put the
name of the function [e.g., =CheckForm()] in the On Action event of
each button or menu which opens a form.

Here's the general idea:

###

Function CheckForm()
If [any form but the one you're trying to open].IsLoaded = True
Then DoCmd.Close acForm, "[the form which is loaded]"
DoCmd.OpenForm "[name of form you're trying to open]"
Else
DoCmd.OpenForm "[name of form you're trying to open]"
End If
End Function

###

Thanks.

Kurt

Here's one slightly different approach. You could have each form call a
function in its Open event that would close all forms except that form.
The form would pass its own name to the function, so that the form would
know which form to leave open. Code for such a function might look like
this:

'----- start of example code -----
Function CloseOpenForms(Optional strExcept As String)

Dim i As Integer

With Application.Forms

For i = .Count - 1 To 0 Step -1
With .Item(i)
If .Name <> strExcept Then
DoCmd.Close acForm, .Name
End If
End With
Next i

End With

End Function
'----- end of example code -----

Then each form would have its On Open property set to this:

=CloseOpenForms([Form].[Name])

Note that I'm not intending for you to substitute any actual name in the
above expression; that expression should automatically pass the form's
Name property to the function. Note, though, that you mustn't call the
function from the Open event of a subform, because that would result in
the subform's parent form being closed by the code.

On the other hand, if your forms will always be opened from buttons you
code yourself, you could simply write a function similar to your
original idea, like this:

'----- start of code -----
Function OpenOneForm(strFormName As String)

Dim i As Integer

' Close all open forms.
With Application.Forms
For i = .Count - 1 To 0 Step -1
DoCmd.Close acForm, .Item(i).Name
Next i
End With

' Open the requested form.
DoCmd.OpenForm strFormName

End Function
'----- end of example code -----
 
J

John Smith

This is air-code, but barring typos something that searches the Forms
collection similarly to this should do what you seek:-

Function OpenForm(FormName As String)
Dim frm As Form
For Each frm In Forms
If frm.Name <> FormName Then DoCmd.Close acForm, frm.Name
Next frm
DoCmd.OpenForm FormName
End Function

You can do the same with the Reports collection.

HTH
John
 
D

Dirk Goldgar

John Smith said:
This is air-code, but barring typos something that searches the Forms
collection similarly to this should do what you seek:-

Function OpenForm(FormName As String)
Dim frm As Form
For Each frm In Forms
If frm.Name <> FormName Then DoCmd.Close acForm, frm.Name
Next frm
DoCmd.OpenForm FormName
End Function

Just a note: if there are multiple open forms to be closed, you have to
loop backward through the Forms collection, not use the For Each
contstruct. "For Each" will end up leaving some forms open, as the
Forms collection is reordered each time you remove a form from it.
 
J

John Smith

Good point, I did say it was air code... Though in my defence, if it works
there will never be more than one form open!

John
 
G

Guest

Thanks for all the options as they all would work well.

I went with Dirk's first suggestion using the function:

=CloseOpenForms([Form].[Name])

Thanks again!

Kurt
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top