Isloaded for all open forms?

J

Jim

I've used "isloaded" to verify if a form is open. I'd like to set the On
Change event of a combo box to close all other open forms excep the form the
combo box is on. Is there a quick way to close all of them, or will I need
to add the IsLoaded statement for each form that could potentially be open?

Thanks,
Jim
 
D

Dirk Goldgar

Jim said:
I've used "isloaded" to verify if a form is open. I'd like to set the On
Change event of a combo box to close all other open forms excep the form
the
combo box is on. Is there a quick way to close all of them, or will I
need
to add the IsLoaded statement for each form that could potentially be
open?


Here's a function that will close all forms except those whose names you
specify as arguments:

'------ start of code ------
Sub CloseAllFormsExcept(ParamArray ExceptForm())

Dim lngFrm As Long
Dim intX As Integer
Dim blnPreserve As Boolean

' Close all open forms except those in the list.

For lngFrm = Forms.Count - 1 To 0 Step -1
With Forms(lngFrm)
blnPreserve = False
For intX = LBound(ExceptForm) To UBound(ExceptForm)
If .Name = ExceptForm(intX) Then
blnPreserve = True
Exit For
End If
Next intX
If blnPreserve Then
' Spare this form
Else
DoCmd.Close acForm, .Name
End If
End With
Next lngFrm

End Sub
'------ end of code ------

Don't use the combo's Change event to call this -- if the user types in the
combo rather than selecting from the list, the Change event will fire with
every keystroke. Instead, use the combo box's AfterUpdate event, and
execute this line of code to close all forms except the one containing the
combo box:

CloseAllFormsExcept Me.Name
 
M

Marshall Barton

Jim said:
I've used "isloaded" to verify if a form is open. I'd like to set the On
Change event of a combo box to close all other open forms excep the form the
combo box is on. Is there a quick way to close all of them, or will I need
to add the IsLoaded statement for each form that could potentially be open?


Do NOT use a combo box's Change event for this. The Change
event is triggered for every keystroke as well as a
selection in the combo box. Use the AfterUpdate event
instead.

Dim ao As AccessObject
For Each ao In CurrentProject.AllForms
If ao.IsLoaded Then
If ao.CurrentView <> 0 And ao.Name <> Me.Name Then
DoCmd.Close acForm, ao.Name, acSaveNo
End If
End If
Next ao
 
J

Jim

I'm using Access 2007 in Access 2002-2003 format. The code won't accept
me.oa_home (my form name). It will accept CloseAllFormsExcept (oa_home), but
it closes all forms, including that one. Is there something else I need to
do?

Thanks again,
Jim
 
D

Douglas J. Steele

As Dirk said, you're supposed to use Me.Name. That returns the name of the
current form.
 
J

Jim

I've tried me.name with both of your examples. The form closes and then I
get a debug error "The expression you entered refers to an object that is
closed or doesn't exist"

The combo box is used to find records on the form. I also have this code in
the After Update event:

Dim rs As Object
Set rs = Me.Recordset.Clone
rs.FindFirst "[ID] = " & Str(Nz(Me![Combo1], 0))
If Not rs.EOF Then Me.Bookmark = rs.Bookmark

Could this be causing the problem?

Thanks,
Jim
 
D

Dirk Goldgar

Jim said:
I'm using Access 2007 in Access 2002-2003 format. The code won't accept
me.oa_home (my form name). It will accept CloseAllFormsExcept (oa_home),
but
it closes all forms, including that one. Is there something else I need
to
do?

In this case, "Name" was not meant as a placeholder for your form's name,
but literally as written. If your form's name is "oa_home", then

CloseAllFormsExcept Me.Name

is equivalent to

CloseAllFormsExcept "oa_home"

which would also work.
 
J

Jim

Thanks - that did the trick!

Dirk Goldgar said:
In this case, "Name" was not meant as a placeholder for your form's name,
but literally as written. If your form's name is "oa_home", then

CloseAllFormsExcept Me.Name

is equivalent to

CloseAllFormsExcept "oa_home"

which would also work.

--
Dirk Goldgar, MS Access MVP
Access tips: www.datagnostics.com/tips.html

(please reply to the newsgroup)
 

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