Roger's Access Library Homepage
Forum Home Forum Home > Other Download Libraries > MS Access MVP Libraries > Long, Crystal
  New Posts New Posts RSS Feed - Rename controls on form in design view
  FAQ FAQ  Forum Search   Events   Register Register  Login Login

>
Want a good read? Try The Summer of His Life (available on Amazon!)

Rename controls on form in design view

 Post Reply Post Reply
Author
Message Reverse Sort Order
Crystal Long View Drop Down
Microsoft MVP
Microsoft MVP


Joined: 21 May 2012
Location: Colorado
Status: Offline
Points: 35
Post Options Post Options   Thanks (0) Thanks(0)   Quote Crystal Long Quote  Post ReplyReply Direct Link To This Post Topic: Rename controls on form in design view
    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.

FORM:

  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)
http://www.accessmvp.com/strive4peace/CodeDocumenter.htm

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

Public Sub ActiveFormRenameControls()
'131112, strive4peace

   'FORM MUST BE IN DESIGN VIEW AND ACTIVE
   '     Code only changes names. The form is not saved ~
   '     Compile code and decide if you like what was done - if so, you can Save
  
   'RENAME
   '  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
               Else
                  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
               Else
                  '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"
     
Proc_Exit:
   On Error Resume Next
   'release object variables
   Set ctl = Nothing
   Set ctl2 = Nothing
   Exit Sub
 
Proc_Err:
   'err 2104 'name already in use -- fix this manually or modify this code
   MsgBox Err.Description, , _
        "ERROR " & Err.Number _
        & "   ActiveFormRenameControls"

   Resume Proc_Exit
   Resume
End Sub


Warm Regards,
Crystal
Microsoft MVP
Remote Training & Programming
Let's Connect and Build Together

http://www.AccessMVP.com/strive4peace
http://YouTube.com/LearnAccessByCrystal
~have an awesome day ~
Back to Top
Sponsored Links
>
Want a good read? Try The Summer of His Life (available on Amazon!)

Back to Top
 Post Reply Post Reply
  Share Topic   

Forum Jump Forum Permissions View Drop Down

Forum Software by Web Wiz Forums® version 12.03
Copyright ©2001-2019 Web Wiz Ltd.