Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Change name of Control in VBA 1

Status
Not open for further replies.

FoxProProgrammer

Programmer
Apr 26, 2002
967
US
Hi,

Is there a way to change the name of a Control in a VBA program? I have the following:

Code:
Dim itm As Control
Dim oldName, newName as String

For Each itm In Form.Controls
    If itm.ControlType = acOptionButton Then
        oldName = itm.Name
        newName = Left(oldName, 2) & "p" & Right(oldName, Len(oldName) - 3)
        itm.Name = newName
    End If
Next

newName is calculated properly, but an error occurs when itm.Name is assigned to newName. The error is:

To set this property, open the form or report in Design view.

Thank you,


dz
 
Do you want to rename the control or the label assigned to the optionbutton?

ck1999
 
I want to rename the control. The label is hidden and not used. This is a one time change because I decided to rename a bunch of controls on the form to something more descriptive, and I don't want to do each one manually unless that's the only way. I want to change the third character in each name to a "p" and there are over 100 of these controls on the form.

Thanks,


dz
 
Did you open the form in design view? It is not possible to rename controls otherwise.
 
Thank you, Remou. It worked.

Code:
Dim itm As Control
Dim frm As Form

DoCmd.OpenForm "frm_selectPins", acDesign
Set frm = Forms!frm_selectPins


For Each itm In frm.Controls
    If itm.ControlType = acOptionButton Then
        oldName = itm.Name
        newName = Left(oldName, 2) & "p" & Right(oldName, Len(oldName) - 3)
        itm.Name = newName
    End If
Next

dz
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top