Solved

Access VBA - Rename Label Controls

Posted on 2007-11-14
4
1,777 Views
Last Modified: 2012-05-05
Hi,

Is there anyway using VBA to rename all controls that begin with Label...? I have 150, with varoius names and want to standardise them from Label 1 to Label 150? I have a similar function to hide every control on my form, but thats all controls not a spcific one?

Thanks
0
Comment
Question by:andyb7901
  • 2
4 Comments
 
LVL 65

Expert Comment

by:rockiroads
ID: 20279230
what name do you want to give them?

you could try this

dim ctl as control

for each ctl in me.controls
    if ctl.controltype = acLabel then
        ctl.name = "some new name"
    end if
next ctl


But then you cant give each a meaningful name as its automated, if you know what I mean
0
 
LVL 7

Expert Comment

by:hippohood
ID: 20279246
Try this code

Sub Rename()
Dim aControl As OLEObject, i As Long
For Each aControl In ActiveSheet.OLEObjects
  If Left(aControl.Name, 5) = "Label" Then
    i = i + 1
    aControl.Name = "Label" & i
  End If
Next
End Sub
0
 
LVL 48

Expert Comment

by:jpaulino
ID: 20279265
Or this:

Dim ctl As Control
Dim x As Integer
x = 1
For Each ctl In Me.Controls
    If ctl.ControlType = acLabel Then
     ctl.Caption = "Label " & x
     x = x + 1
    End If
Next ctl
0
 
LVL 65

Accepted Solution

by:
rockiroads earned 500 total points
ID: 20279294
Sorry, slight problem. When changing the actual control name, you cant run the code in runmode. It has to be in design mode

Example below gives it a new prefix - this is just to give you an idea


Public Sub ModifyLabels(frm As form)

    Dim ctl As Control
    Dim i As Integer
   
    DoCmd.OpenForm frm.Name, acDesign
   
    i = 1
    For Each ctl In frm.Controls
        If ctl.ControlType = acLabel Then
            Debug.Print "OldName=", ctl.Name
            ctl.Name = "some new name " & i
            Debug.Print "NewName=", ctl.Name
            i = i + 1
        End If
    Next ctl
   
    DoCmd.Close acForm, frm.Name, acSaveYes
   
End Sub


now you call this passing in your form object
but question still remains on what you want to name as them
0

Featured Post

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question