locked
rename controls in vba RRS feed

  • Question

  • Is it possible to rename control (check boxes, text boxes,...) in vba? I know how to iterate through them is it possible to change the controls name?

    TIA,

    Martin

    Thursday, July 19, 2012 6:19 PM

Answers

  • Martin

    Yes, if the form is opened in Design view. This snippet works.

    Public Sub TestCode()
        Dim frm As Form
        
        DoCmd.OpenForm "frmEmployees", acDesign
        Set frm = Forms("frmEmployees")
        frm.Controls("txtLastName").Name = "LName"
        Set frm = Nothing
        
        DoCmd.Close acForm, "frmEmployees", acSaveYes
        
    End Sub


    Bill Mosca
    http://www.thatlldoit.com
    http://tech.groups.yahoo.com/group/MS_Access_Professionals

    • Marked as answer by mjoshua Thursday, July 19, 2012 7:58 PM
    Thursday, July 19, 2012 7:14 PM

All replies

  • I believe you can, but you have to open the form in design view first.

    Bob Fitz BizSoftware

    Thursday, July 19, 2012 6:49 PM
  • Martin

    Yes, if the form is opened in Design view. This snippet works.

    Public Sub TestCode()
        Dim frm As Form
        
        DoCmd.OpenForm "frmEmployees", acDesign
        Set frm = Forms("frmEmployees")
        frm.Controls("txtLastName").Name = "LName"
        Set frm = Nothing
        
        DoCmd.Close acForm, "frmEmployees", acSaveYes
        
    End Sub


    Bill Mosca
    http://www.thatlldoit.com
    http://tech.groups.yahoo.com/group/MS_Access_Professionals

    • Marked as answer by mjoshua Thursday, July 19, 2012 7:58 PM
    Thursday, July 19, 2012 7:14 PM
  • Is it possible to rename control (check boxes, text boxes,...) in vba? I know how to iterate through them is it possible to change the controls name?

    TIA,

    Martin

    Hi Martin,

    Only in Design mode you can change the control's name. Those changes can be saved.
    But in FormView (ReportView) you can modify quite a lot of properties, for instance caption, colour, position, so that it for the user looks as if the control is completely changed. Those changes are not saved.

    Imb.

    Thursday, July 19, 2012 7:19 PM
  • Is it possible to rename control (check boxes, text boxes,...) in vba? I know how to iterate through them is it possible to change the controls name?

    Yes, you can do it (if the form is open in design view) by the simple expedient of assigning to the control's Name property, as for example:

    Forms!fmMyForm!MyTextbox.Name = "txtMyTextbox"

    Or in a loop:

    Dim ctl As Access.Control
    
    For Each ctl In Forms!SomeForm  ' must be open in design view
        If ctl.ControlType = acTextbox Then
            If Left(ctl.Name, 3) <> "txt" Then
                ctl.Name = "txt" & ctl.Name
            End If
        End If
    Next ctl

    BUT BEAR IN MIND that, if there are VBA event procedures associated with a control, those event procedures will not automatically be renamed when you rename the control, and hence their connection to the control will be lost.  Also, of course, any other VBA code that refers to the controls by name will become invalid.  You would have to run additional code to find and replace the control name in the form's code module.


    Dirk Goldgar, MS Access MVP
    Access tips: www.datagnostics.com/tips.html

    Thursday, July 19, 2012 7:20 PM
  • Martin

    Just curious...why you you want to rename controls? The only thing I've done in the past is to change ControlSources should I be using a form for more than one record source.


    Bill Mosca
    http://www.thatlldoit.com
    http://tech.groups.yahoo.com/group/MS_Access_Professionals

    Friday, July 20, 2012 1:53 PM