Code to rename all controls of a form.

bfuchs
bfuchs used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Martin LissOlder than dirt
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
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.
Senior Developer
Commented:
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

Martin LissOlder than dirt
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
@ste5an: Note that he says "in design mode".
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

ste5anSenior Developer

Commented:
That's what acDesign does ;)
Thank you!
@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 dirt
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
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?
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

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial