Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Access VBA - Rename Label Controls

Posted on 2007-11-14
4
Medium Priority
?
1,955 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 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

If you need a simple but flexible process for maintaining an audit trail of who created, edited, or deleted data from a table, or multiple tables, and you can do all of your work from within a form, this simple Audit Log will work for you.
This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

719 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