Code to rename all controls of a form.

Hi Experts,
Looking for code that will open a form in design mode, loop thru all controls and change their name to "txt" &  their control source.
Thanks
LVL 6
bfuchsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Martin LissOlder than dirtCommented:
Unless you want to write an add-in or find on on the web that does it, you can't run code in design mode.
ste5anSenior DeveloperCommented:
It's pretty simple, if you know the twist:

Option Compare Database
Option Explicit

Public Sub RenameTextBoxes(AFormName As String)

  Dim Control As Access.Control
  Dim Form As Access.Form
  
  DoCmd.OpenForm AFormName, acDesign    
  Set Form = Forms(AFormName)
  For Each Control In Form.Controls
    If TypeOf Control Is Access.TextBox Then
      If Left(Control.Name, 3) <> "txt" Then
        Control.Name = "txt" & Control.Name
      End If
    End If
  Next Control
  
  Set Control = Nothing
  DoCmd.Close acForm, AFormName, acSaveYes
  Set Form = Nothing

End Sub

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Martin LissOlder than dirtCommented:
@ste5an: Note that he says "in design mode".
Price Your IT Services for Profit

Managed service contracts are great - when they're making you money. Yes, you’re getting paid monthly, but is it actually profitable? Learn to calculate your hourly overhead burden so you can master your IT services pricing strategy.

ste5anSenior DeveloperCommented:
That's what acDesign does ;)
bfuchsAuthor Commented:
Thank you!
bfuchsAuthor Commented:
@Martin,
Is this a question about Classic Visual Basic which is VB6 or Access?
I happened to use Access but doesn't the same question/answer applied to VB as well (except for Access.TextBox)?
The reason I include that in the topic is that I can get VB folks look into as well. (By now if you feel like removing is ok with me:)
Thanks,
Ben
Martin LissOlder than dirtCommented:
That's for you to decide, but I can say that Classic Visual Basic is an outdated (sniff) language which is stand-alone and can no be used inside any Office product including Access, Excel, Word and Visio). On the other hand VBA which is very similar to VB6 can be, so would you like me to add that topic?
bfuchsAuthor Commented:
but I can say that Classic Visual Basic is an outdated
Well under VB Classic there are 164K users assigned, while for VBA I see only 11K, guess by selecting VB I get more attention...
so would you like me to add that topic?
It doesnt matter as the issue has been resolved.
Thanks,
Ben
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.