Solved

Access VBA - Rename Label Controls

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

760 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

17 Experts available now in Live!

Get 1:1 Help Now