Solved

Access VBA - Rename Label Controls

Posted on 2007-11-14
4
1,737 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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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 retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…

919 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now