Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can I use "for each" to loop through inactive forms?

Status
Not open for further replies.

dkaplan

Programmer
Jan 29, 2001
98
0
0
US
I'm trying to loop through all the forms in a database and printout specific atrtributes of the controls

If I use

For each frm in Forms
do something
next frm

I only loop through the active forms.

Is there a way I can loop through the inactive forms as well?

 
By "active" and "inactive" do you mean open and closed forms?

The Missinglinq

Richmond, Virginia

There's ALWAYS more than one way to skin a cat!
 
Public Sub test()
On Error Resume Next
Dim frm As AccessObject
Dim ctrl As Access.Control
For Each frm In CurrentProject.AllForms
DoCmd.OpenForm frm.Name, acDesign, , , acFormReadOnly, acHidden
For Each ctrl In Forms(frm.Name)
Debug.Print ctrl.Name
Next ctrl
DoCmd.Close acForm, frm.Name
Next frm
End Sub
 
Just a slight enhancement to MajP's code:

Public Sub test()
On Error Resume Next
Dim frm As AccessObject
Dim ctrl As Access.Control
Dim frmOpen as Form
Dim blnClose As Boolean

For Each frm In CurrentProject.AllForms
Set frmOpen=Forms(frm.Name)
blnClose=(Err.Number<>0)
Err.Clear
If blnClose Then
DoCmd.OpenForm frm.Name, acNormal, , "0=1", , acHidden
End If
For Each ctrl In Forms(frm.Name)
Debug.Print ctrl.Name
Next ctrl
If blnClose Then
DoCmd.Close acForm, frm.Name
End If
set frmOpen=Nothing
Next frm
End Sub

The differences:
- acDesign cannot be used in MDE, so open the form in normal view, but hidden
- If the form has a data source, filter it so that no records are returned (0=1)
- close only the forms that were opened by the procedure



[pipe]
Daniel Vlas
Systems Consultant

 
Thanks, danvalas,

I gave it a try but I get the error

"MS Acess can't fiond the field "formName" referred to in your expression"

at the line:

Set frmOpen=Forms(frm.Name)
 
Thanks again, MajP and danvlas,

I tried again in the immediate window and got it to work.

In the previous message I was trying to run the code from a button on a form, which was probably not the greatest idea.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top