Solved

Access VBA - Rename Label Controls

Posted on 2007-11-14
4
1,843 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MS Access / SQL Server - ODBC Problems 16 59
ms access - Helpdesk ticket system 5 52
Access Report via Windows Desktop Shortcut 12 44
how to get hundreds part from the number 1 31
Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
This article describes two methods for creating a combo box that can be used to add new items to the row source -- one for simple lookup tables, and one for a more complex row source where the new item needs data for several fields.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

739 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