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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Loop through All Controls in All Forms in a Database 1

Status
Not open for further replies.

kjv1611

New member
Jul 9, 2003
10,758
US
I was thinking this was simple, but perhaps it's not as easy as I thought.

I've looped through controls in a form many times, but what I want to do now is to loop through all controls within all forms in a database.

I tried this:
Code:
Dim frm as Form
    For Each frm In CurrentProject.AllForms
        If InStr(frm.Name, "MyString") Then Debug.Print "Forms|" & frm.Name
        For Each ctl In Forms(frm.Name).Controls
            If InStr(ctl.Name, "MyString") Then Debug.Print "Form|" & frm.Name & "|" & ctl.Name
        Next ctl
    Next frm

That didn't work, errored out as soon as I tried to say For Each frm in CurrentProject.AllForms

So then I tried:
Code:
Dim obj As Object
Dim frm as Form
    For Each obj In CurrentProject.AllForms
        If InStr(obj.Name, "MyString") Then Debug.Print "Forms|" & obj.Name
        [green]'Set frm = Forms(obj.Name) '- this had problems as well, so I commented out[/green]
        For Each ctl In Forms(obj.Name).Controls
            If InStr(ctl.Name, "MyString") Then Debug.Print "Form|" & obj.Name & "|" & ctl.Name
        Next ctl
    Next obj
And that one will loop through all the forms, but when I try to loop through the controls on the given form, it tells me:
"Can't find the form, FirstFormNameItComesTo"

Am I forgetting something simple or missing something, or is it somehow not possible to loop through the controls of each form as you're looping through form objects?

Thanks for any suggestions, references, corrections, etc..
 
the forms collection needs an open form

Code:
docmd.openform docmd.OpenForm obj.name,,,,,acHidden 
If InStr(obj.Name, "MyString") Then Debug.Print "Forms|" & obj.Name
        'Set frm = Forms(obj.Name) '- this had problems as well, so I commented out
        For Each ctl In Forms(obj.Name).Controls
            If InStr(ctl.Name, "MyString") Then Debug.Print "Form|" & obj.Name & "|" & ctl.Name
        Next ctl
docmd.close acForm ,obj.name,acSaveNo
 
So... I'd need to open each form as I loop through them? If I do so, and it has startup code in it, will opening it hidden more or less not allow the code to run, or do I also need to disable all the _open and _load events?
 
Dumb question- yes, it needs that for each form.. Sorry.

Thanks for the help there... that fixed it for sure!
 
i think that startupcode will run

if this is a mdb use
docmd.openform docmd.OpenForm obj.name,acDesign,,,,acHidden
 
I'll point out the one typo I noticed, though just in case anyone reads this, tries it, and then can't get it to work:
docmd.openform docmd.OpenForm obj.name,acDesign,,,,acHidden has an extra DoCmd.OpenForm in it - it's listed twice... just a typo on PWise's part. Great fix, but typed on the fly I'm sure. [wink]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top