Rename Form Controls & Labels

Chrisss

New member
Local time
Today, 07:54
Joined
Nov 25, 2020
Messages
2
Hello,

I have a specific form, “frmCompany” and I want to copy it then paste it as “frmPerson”.

I then would like to do a Search for controls & labels in the pasted form to Search for any containing "Cmpny" and Replace with "Prsn" so “lblCmpnyName” -> “lblPrsnName” and “txtCmpnyName” -> “txtPrsnName” etc.

Since I want to be able to repeat this technique, I've spent a couple days trying to figure this out without success, any help greatly appreciated.

Thanks,
Chris
 
Hi Chris. Have you considered using a third-party add-in to do this task? I'm not sure which tool would have that feature, but MZ Tools is a popular one.
 
In code you can open a form in design view....
Code:
    DoCmd.OpenForm "FormName", acDesign
Then you can enumerate the controls collection of that form...
Code:
    For Each ctrl In Forms("FormName").Controls
        ' do something with ctrl
    Next
But this is not something that makes sense to do as a matter of routine.
 
Code:
Public Sub testIt()
  'Change City to State in Form1
  RenameControls "Form1", "City", "State"
End Sub

Public Sub RenameControls(formName As String, Find As String, Replace As
String)
  Dim frm As Access.Form
  Dim ctl As Access.Control
  Dim ctlName As String
  DoCmd.OpenForm formName, acDesign
  Set frm = Forms(formName)
  For Each ctl In frm
    ctl.Name = VBA.Replace(ctl.Name, Find, Replace)
  Next ctl
  DoCmd.Close acForm, formName, acSaveYes

End Sub
 

Users who are viewing this thread

Back
Top Bottom