Print Page | Close Window

Rename controls on form in design view

Printed From: Roger's Access Library
Category: Other Download Libraries
Forum Name: Long, Crystal
Forum Description: Access Basics is designed for those of you with a thirst to understand the fundamentals of Access with programming in mind ... whether you realize it or not.
Printed Date: 20 Feb 2018 at 12:57am

Topic: Rename controls on form in design view
Posted By: Crystal Long
Subject: Rename controls on form in design view
Date Posted: 12 Nov 2013 at 11:45am
this code:
  1.  renames controls to match the ControlSource for bound controls
  2.  renames labels to include controlname. Associated labels are named with the controlname first and unassociated labels have 'Label_' first.


  1. must be in design view
  2. must be active
This is designed so you can run the code while the form you want to change is in design view and active.  Compile the code before running.  Then run and compile again.  Most likely, changes will be needed in the code behind the form that was just changed -- you can either fix things or, if you don't like what happened, the form can be closed without saving.  

Be sure to check the control Subs behind the form (ctrl-home to go to top, drop procedures list in upper right and see if there are names like 'myoldcontrolname_AfterUpdate' which means it is not hooked up to anything -- otherwise you would have to navigate to the code via the object list dropdown in the upper left then the event dropdown in the upper right) to make manual changes to the  sub names in the declaration line.  After compiling and saving, ensure that [Event Procedure] is where it needs to be on the Property Sheet.  If it is not, type "[" in the property to trigger Access to fill it in.   Just because the code is behind the form does not mean it is still 'hooked up'.  Once you have changed all procedures with event names in the general list, you can be sure everything is hooked up.  Avoid naming your custom routines in a way that looks like they belong to control events.

At some point, I hope to hook in my code documenter* to make more of the changes needed by renaming  -- thanks to those that have found it and let me know

* Crystal's Code Documenter for Access (update to document code in Excel and other Office projects on my back burner ... emails showing interest can help move that forward)" rel="nofollow -

This tool can be used one form at a time to rename controls ~ simply run this:

Public Sub ActiveFormRenameControls()
'131112, strive4peace

   '     Code only changes names. The form is not saved ~
   '     Compile code and decide if you like what was done - if so, you can Save
   '  bound controls to match ControlSource
   '  labels:
   '     associated: controlname_Label
   '     unassociated: Label_controlname
   'Click HERE and press F5 to Run!

   On Error GoTo Proc_Err

   Dim ctl As Control _
      , ctl2 As Control
   Dim sControlSource As String _
      , sLabelName As String _
      , sLabelName2 As String _
      , iCountName As Integer _
      , iCountLabel As Integer
   iCountName = 0
   iCountLabel = 0
   With Screen.ActiveForm
      If MsgBox(.Name _
         & vbCrLf & vbCrLf & "Rename bound controlnames to be the field they are bound to? " _
         & vbCrLf & vbCrLf & "... and associated Label controlnames to Controlname_Label?" _
         & vbCrLf & "... and unassociated Label controlnames to Label_Controlname?" _
         , vbYesNo, "Rename Controls on " & .Name & "?") = vbNo Then Exit Sub
       For Each ctl In .Controls
         If ctl.ControlType <> acLabel Then
            sControlSource = Nz(Get_Property_relinker("controlsource", ctl), "")
            If Len(sControlSource) > 0 Then
               If Left(sControlSource, 1) <> "=" Then
                  If ctl.Name <> sControlSource Then
                     ctl.Name = sControlSource
                     iCountName = iCountName + 1
                  End If
                  sLabelName = sControlSource & "_Label" 'associated
                  sLabelName2 = "Label_" & sControlSource 'unassociated
                  sLabelName = ctl.Name & "_Label"
                  sLabelName2 = "Label_" & ctl.Name 'unassociated
                  sControlSource = ctl.Name
               End If
               If ctl.Controls.Count > 0 Then
                  With ctl.Controls(0)
                     If .ControlType = acLabel Then
                        If .Name <> sLabelName Then
                           .Name = sLabelName
                           iCountLabel = iCountLabel + 1
                        End If
                     End If
                  End With
                  'no associated label
                  'look for a label whose caption is the control source
                  For Each ctl2 In .Controls
                     If ctl2.ControlType = acLabel Then
                        If ctl2.Caption = sControlSource Then
                           If ctl2.Name <> sLabelName2 Then
                              ctl2.Name = sLabelName2
                              iCountLabel = iCountLabel + 1
                           End If
                        End If
                     End If
                  Next ctl2
               End If
            End If 'Len(sControlSource) > 0
         End If 'not a label
       Next ctl
   End With
   MsgBox "Renamed " & iCountName & " controls, " _
      & iCountLabel & " Labels" _
      , , "Done"
   On Error Resume Next
   'release object variables
   Set ctl = Nothing
   Set ctl2 = Nothing
   Exit Sub
   'err 2104 'name already in use -- fix this manually or modify this code
   MsgBox Err.Description, , _
        "ERROR " & Err.Number _
        & "   ActiveFormRenameControls"

   Resume Proc_Exit
End Sub

Warm Regards,
Microsoft MVP
Remote Training & Programming
Let's Connect and Build Together
~have an awesome day ~

Print Page | Close Window