Rename most controls in all forms in a database

igillesp

Registered User.
Local time
Today, 23:48
Joined
Feb 27, 2008
Messages
20
Hi!

I've got ~10 forms in a database, each with numerous (say 15) option buttons in frames.

Each is named [variable name]Frame, but I'd like to name them all to [variable name]YN to reflect their purpose.

Rather than renaming each by hand, is there a piece of VBA which will do this programatically?

I've tried...
Code:
Sub FormName()
Dim frm As Form
Dim ctl As Control
For Each frm In Forms
    DoCmd.OpenForm frm.Name, acDesign
    For Each ctl In frm.Name
        ctl.Name = Replace(ctl.Name, "Frame", "YN")
    Next
Next
End Sub
...but I get 'Compile error: For Each may only iterate over a collection object or an array', highlighting .Name of...
Code:
For Each ctl In frm.Name

Thanks!

Iain
 
Code:
For Each ctl In frm.[COLOR="DarkRed"][B]Controls[/B][/COLOR]
 
Thanks, but only half solved.

For one...
Code:
For Each frm In Forms
...only recognised open forms, so I have to open them manually. This isn't a huge problem, but it would be nice to know how to do it properly.

Second, I get "Compile error: Syntax error" with...
Code:
Replace(ctl.Name, "Frame", "YN")
Any ideas?
 
you probably want to use allforms collection.


Code:
Sub FormName()
Dim frm As object
Dim ctl As Control
For Each frm In currentproject.allForms
    DoCmd.OpenForm frm.Name, acDesign
    For Each ctl In frm.controls
        ctl.Name = Replace(ctl.Name, "Frame", "YN")
    Next
Next
 
Last edited:
Thanks, but I'm now getting "runtime error '438': object does not support this property or method" on...
Code:
    For Each ctl In frm.Controls
Am I missing a reference, or just the plot?
 
Solved by adapting code from elsewhere (http://www.vbaexpress.com/forum/archive/index.php/t-3115.html)

Final code:

Code:
Sub ChangeControlName()
Dim dbs As Database
Dim ctr As Container
Dim doc As Document
Dim frm As Form
Dim ctrl As Control
Set dbs = CurrentDb
Set ctr = dbs.Containers!Forms
For Each doc In ctr.Documents
    DoCmd.OpenForm doc.Name, acDesign
    Set frm = Forms(doc.Name)
        For Each ctrl In frm.Controls
            ctrl.Name = Replace(ctrl.Name, "Frame", "YN")
        Next
    DoCmd.Close acForm, frm.Name, acSaveYes
Next
Set dbs = Nothing
End Sub
Thanks for others efforts though!
 

Users who are viewing this thread

Back
Top Bottom