Enumerating through all forms in the database

  • Thread starter Thread starter Max Moor
  • Start date Start date
M

Max Moor

Hi All,
I want to enumerate through all the forms in my database (then
enumerate the controls and set some properties.). I used the syntax:

Dim frm As Form
Dim ctl As Control

For Each frm In Application.Forms
For Each ctl In frm.Controls
With ctl
etc
etc
etc


This only seems to go through the forms that are open, though.
(maybe) I suppose that since I'm setting control properties, having the
form open in design view might be a requirement anyway. So, I have to
ask...

Is there syntax to enumerate through all forms in the database, open
or not? Or, is this code already doing so, and I just only see the changes
on the open forms, because they DO have to be open to change the control
properties?

Thanks for helping me past my latest curiosity...

- Max
 
You're close. Forms need to be open in design view to be
changed and you can do it 'invisibly'...

Whilst I've used this technique before this is untested
code:-

********BEGIN**********
Dim accObj As AccessObject, dbs As Object
Dim lngNumAllForms as Long, strFName as String
Dim ctl as Control, strCtlName as String

Set dbs = Application.CurrentProject
lngNumAllFrms = dbs.AllForms.Count

For Each accObj In dbs2.AllForms
strFName = accObj.Name
DoCmd.OpenForm strFName, acDesign, , , , acHidden

' Do stuff for Forms here and when done go through
controls

For Each ctl In Forms(strFName).Controls
strCtlName = ctl.Properties("Name")
' Use the syntax above to get or change a property
value -
' "Name"
' or "Tag"
' be aware that some return strings, some
integers/longs etc

Next ctl ' Next Control on Form

DoCmd.Close acForm, strFName, acSaveNo

Next accObj 'Next Form

**********END*********
 
If you are using a recent version of Access, you could loop thorugh
CurrentProject.AllForms
to get the names of all forms in the database:

Dim accobj As AccessObject
Dim ctl As Control
Dim strDoc As String
For Each accobj In CurrentProject.AllForms
strDoc = accobj.Name
DoCmd.OpenForm strDoc, , acDesign
For Each ctl In Forms(strDoc).Controls
Debug.Print ctl.Name
Next
DoCmd.Close acForm, strDoc
Next
 
Back
Top