Rename label boxes (1 Viewer)

diakis

Registered User.
Local time
Today, 07:39
Joined
Jul 4, 2006
Messages
16
Hi
In a form in access 2002-2003 I have 100 label boxes with the names lab_1, lab_2 ... lab_100
I want to rename all these label boxes to etik_1, etik_2 ... etik_100
Can I do this all together and not one by one from properties?
 

Gasman

Enthusiastic Amateur
Local time
Today, 05:39
Joined
Sep 21, 2011
Messages
14,310
Yes. Though not something I have ever done.
Open form in design mode.
Cycle through all the controls testing for label type.
If label name begins with your criteria, then set new name.
Close form, saving the changes.
 

diakis

Registered User.
Local time
Today, 07:39
Joined
Jul 4, 2006
Messages
16
Thank you Gasman for the answer to my question, but this is not works.
When I select more than one label boxes in form then disappeared the name from properties
 

Moosak

New member
Local time
Today, 05:39
Joined
Jan 26, 2022
Messages
26
This is my code for this :
Code:
Public Sub RenameFormControls(txtFormName As String, NewName As String)
'Designed By : Moosak
    Dim Frm As Form
    Dim Ctrl As Control
    Dim x As Integer
    Dim CtrlType As Variant

DoCmd.OpenForm txtFormName, acDesign
Set Frm = Forms(txtFormName)

' UnComment your controle type from here :
'CtrlType = acCommandButton
'CtrlType = acTextBox
CtrlType = acLabel
'CtrlType = acComboBo
'CtrlType = acCheckBox
'CtrlType = acListBox

x = 1
For Each Ctrl In Frm.Controls
'Debug.Print Ctrl.Name & vbTab & Ctrl.ControlType
    If Ctrl.ControlType = CtrlType Then
    
' Change Control Name or Caption
'        Frm.Controls(Ctrl.Name).Name = NewName & Format(x, "00")
        Frm.Controls(Ctrl.Name).Caption = NewName & Format(x, "00")
    x = x + 1
    End If
    
Next

'DoCmd.Close acForm, txtFormName, acSaveYes
Set Frm = Nothing

MsgBox "done"
End Sub

Sub test()

 Call RenameFormControls("FormName", "NewName")

End Sub
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 00:39
Joined
Feb 19, 2002
Messages
43,293
Normalizing the schema is probably a better option:)
 

Gasman

Enthusiastic Amateur
Local time
Today, 05:39
Joined
Sep 21, 2011
Messages
14,310
You said you wanted to change the name, not the caption?
I would have just used the existing suffix of the label and give it the new prefix?

Who is to say Label1 would be the first found?

I would also debug the resulting name to see what I actually had as well as walking through the code line by line for at least a few controls.

Edit: Just seen your code for calling it.
You said the labels were lab_1 and needed to be etik_1, so why are you passing in "NewName" and do you really have a form called "FormName" ? :(
 
Last edited:

Moosak

New member
Local time
Today, 05:39
Joined
Jan 26, 2022
Messages
26
Hi Gasman

You pass the form name which contains the controls that you want to rename or change there captions.
I meant by NewName the prefix that you want for the controls. They will be renamed some thing like this (If you pass "Cont" as NewName) :

( Cont01, Cont02, Cont03, Cont04, Cont05, ...........)

I usually use it when I have a form with to many controls . Renaming controls is a real headache .
1667322061971.png
 

Gasman

Enthusiastic Amateur
Local time
Today, 05:39
Joined
Sep 21, 2011
Messages
14,310
Sorry Moosak,
I attributed your code to the O/P :(
I have never had the need to rename a whole bunch of controls or labels like that. The odd one or two perhaps, but that would be it.
 

moke123

AWF VIP
Local time
Today, 00:39
Joined
Jan 11, 2013
Messages
3,920
Code:
Dim ctl As Control

    For Each ctl In Forms("Form1").Controls  'Change Form name as needed

        If Left(ctl.Name, 3) = "Lab" Then

            ctl.Name = Replace(ctl.Name, "Lab", "etik")
        End If
    Next
 

Users who are viewing this thread

Top Bottom