shows Rename associated Labels on active Access form Ms Access Gurus

help support this site, thank you

Rename associated Labels on active Access form

Depending on how you create forms, label controls can have wonky names. Loop through each control on a form with VBA. If there's an associated label, rename it to correlate to the control it's tied to. I like to make the names = "controlname_Label" where controlname is what it's associated with. In my applications, if names start with "Label", they're stand-alone. Since the code is open-source, you can change it for your preference.

selection summary information for 2 different selections in a Word document

Quick Jump

Goto the Very Top  


Download

Download zipped BAS file you can import into your Word documents: mod_Controls_RenameAssociatedLabels_s4p__BAS.zip

If you have trouble with a download, you may need to unblock the ZIP file, aka remove Mark of the Web, before extracting the file(s). Here are steps to do that: https://msaccessgurus.com/MOTW_Unblock.htm

Goto the Top  

VBA

Standard module

Be in Design or Layout View of form you want to rename labels on, then run ActiveForm_RenameAssociatedLabels_s4p

If you don't like what it did, then don't save the form. Look at the Debug window for details to see old and new label control names.

Option Compare Database 
Option Explicit 

'*************** Code Start *****************************************************
' module name: mod_ActiveForm_RenameAssociatedLabels_s4p
'-------------------------------------------------------------------------------
' Purpose  : rename associated label controls to "controlname_Label"
'                 FORM MUST BE IN DESIGN or LAYOUT VIEW AND ACTIVE
' Author   : crystal (strive4peace)
' web site : https://msaccessgurus.com
' This code: https://msaccessgurus.com/tool/ActiveForm_RenameAssociatedLabels.htm
' LICENSE  :
'   You may freely use and share this code, but not sell it.
'   Keep attribution. Mark your changes. Use at your own risk
'-------------------------------------------------------------------------------
'           ActiveForm_RenameAssociatedLabels_s4p
'-------------------------------------------------------------------------------
Public Sub ActiveForm_RenameAssociatedLabels_s4p() 
'150225, strive4peace, 231003, 05

   'Click HERE and press F5 to Run!
   
   'CALLs
   '  GetControlname_Label_s4p
   
   On Error GoTo Proc_Err 

   Dim oControl As Control _ 
      ,obj As Object 
      
   Dim sControlName As String _ 
      ,sLabelName As String _ 
      ,sLabelNameNew As String _ 
      ,sMsg As String _ 
      ,iCount As Integer 
   
   iCount = 0 
   
   Set obj = Screen.ActiveForm 
   
   With obj 
      If MsgBox(.Name _ 
            & vbCrLf & vbCrLf &  "Rename labels? " _ 
            ,vbYesNo, "Rename Controls on " & .Name &  "?") _ 
      = vbNo Then Exit Sub 
      
      Debug.Print  "*** rename label controls on " & .Name 
      
      For Each oControl In .Controls 
         If oControl.ControlType <> acLabel Then 
            sControlName = oControl.Name 
            
            sLabelName = GetControlname_Label_s4p(oControl) 
            
            If sLabelName <>  "" Then 
               'change this if you want to name labels differently
               sLabelNameNew = sControlName &  "_Label"
               If sLabelName <> sLabelNameNew Then 
                  Debug.Print Space(3) & sLabelName _ 
                              &  " --> " & sLabelNameNew 
                  .Controls(sLabelName).Name = sLabelNameNew 
                  iCount = iCount + 1 
               End If 
            End If 
         End If 
      Next oControl 
   End With   'obj ActiveForm
   
   sMsg =  "Renamed " & iCount &  " label controls"
   Debug.Print sMsg 
 
   sMsg = sMsg & vbCrLf & vbCrLf &  "Look at Debug window for details"
   MsgBox sMsg,, "Done"
      
Proc_Exit: 
   On Error Resume Next 
   Set oControl = Nothing 
   Set obj = Nothing 
   On Error GoTo 0 
   Exit Sub 
  
Proc_Err: 
   'err 2104 'name already in use
   '     -- fix this manually or modify this code
   MsgBox Err.Description _ 
        ,, "ERROR " & Err.Number _ 
        &  "   RenameAssociatedLabel_Controls"

   Resume Proc_Exit 
   Resume 
End Sub 
'-------------------------------------------------------------------------------
'           GetControlname_Label_s4p
'-------------------------------------------------------------------------------
Private Function GetControlname_Label_s4p( _ 
   poControl As Control _ 
   ) As String 
'220425 strive4peace,231003
   On Error GoTo Proc_Err 
   Dim sControlSource As String 
   GetControlname_Label_s4p =  ""
   
   With poControl.Controls(0) 
      If .ControlType <> acLabel Then Exit Function 
      GetControlname_Label_s4p = .Name 
   End With 
   
Proc_Exit: 
   On Error GoTo 0 
   Exit Function 
  
Proc_Err: 
   Resume Proc_Exit 
   Resume 
   
End Function 
'*************** Code End *****************************************************
' Code was generated with colors using the free Color Code add-in for Access

Goto Top  

Reference

Microsoft Learn

Screen.ActiveForm property (Access)

Controls object (Access)

Control object (Access)

Control.Name property (Access)

Goto Top  

Backstory

How often do you find yourself renaming a bunch of labels to correlate with their associated controls? I did it one time too many -- then I wrote this code to make it faster. Ideally, it would be an add-in so a module wouldn't have to be imported.

I've also written code to rename controls to what they're bound to. Some say that's a bad thing to do, but it's so much easier! In all my years of developing Access applications, only one very complex application needed a control to have a different name than the bound field. You'll know when you need it.

Share with others

Here's the link for this page in case you want to copy it and share it with someone:

https://msaccessgurus.com/VBA/ActiveForm_RenameAssociatedLabels.htm

or in old browsers:
http://www.msaccessgurus.com/VBA/ActiveForm_RenameAssociatedLabels.htm

Get Help building Access applications

Let's connect and team-develop your application together. I teach you how to do it yourself. My goal is to empower you.

While we build something great together, I'll pull in code and features from my vast libraries as needed, cutting out lots of development time. I'll give you lots of links to good resources.

You do what you're good at and I do what I'm good at. Together, we build a successful application. Email me, training@msAccessGurus.com

~ crystal

the simplest way is best, but usually the hardest to see

Goto Top