locked
How to change control name in VBA at run time? RRS feed

  • Question

  • Hi,

    I'm new to these forums. Please guide me if I'm in wrong place to discuss this.

    I got a problem in my code. I'm creating controls at run time. so, i'm giving them names using "for" loop as below.

    For i = 1 To RowCount
    Set chkbox = UserForm1.Controls.Add("Forms.CheckBox.1")
    With chkbox
    .Name = "CheckBox" & i

     

    But when getting its value, i'm facing issues. following is my code.

    controlname = "CheckBox" & i
    If UserForm1!controlname.Value = Checked Then

     

    Please help me how can i get the value of a control or change the name of it which got created at run time.

     

    Thanks,

    Anand

    Thursday, September 29, 2011 9:56 AM

Answers

  • i tried printing name of the control and it shows as "CheckBox1",.. so the control with that name exists.

    How did you refer to the control in order to print its name

    Try this in a form named UserForm1

    [vbnet]
    Private Sub UserForm_Click()
    Dim ctl As Control

    Set ctl = Me.Controls.Add("forms.checkbox.1")
    ctl.Name = "myCheckbox1"
    ctl.Caption = "hello"
    ctl.AutoSize = True

    MsgBox UserForm1.Controls("myCheckbox1").Name & " " & ctl.Caption

    ' normally would use Me within the userform

    End Sub
    [vbnet]

    Peter Thornton

    • Proposed as answer by danishani Monday, November 28, 2011 10:30 PM
    • Marked as answer by danishani Wednesday, January 11, 2012 8:19 PM
    Saturday, October 8, 2011 4:49 PM

All replies

  • Hi,

    I'm new to these forums. Please guide me if I'm in wrong place to discuss this.

    I got a problem in my code. I'm creating controls at run time. so, i'm giving them names using "for" loop as below.

    For i = 1 To RowCount
    Set chkbox = UserForm1.Controls.Add("Forms.CheckBox.1")
    With chkbox
    .Name = "CheckBox" & i

     

    But when getting its value, i'm facing issues. following is my code.

    controlname = "CheckBox" & i
    If UserForm1!controlname.Value = Checked Then

     

    Please help me how can i get the value of a control or change the name of it which got created at run time.

     

    Thanks,

    Anand

    • Merged by Kee Poppy Monday, October 3, 2011 7:16 AM duplicate thread
    Thursday, September 29, 2011 9:04 AM
  • Hi,

    Are you using VB 6 or Visual Studio?

    Regards,

    A.Murugan


    If it solved your problem,Please click "Mark As Answer" on that post and "Mark as Helpful". Happy Programming!
    Thursday, September 29, 2011 9:22 AM
  • Hi,

     

    I use VBA from Excel. Isn't this forum to discuss about it?

     

    Thanks,

    Anand

    Thursday, September 29, 2011 9:26 AM
  • Hi,

     

    I use VBA from Excel. Isn't this forum to discuss about it?

     

    Thanks,

    Anand

    This is a General VB.NET forum (not VB 6.0 or VBA related).

    This is VBA forum on MSDN: http://social.msdn.microsoft.com/Forums/en-US/isvvba/threads. You have much more chances to get correct answer there.

    • Proposed as answer by Cor Ligthert Thursday, September 29, 2011 12:06 PM
    Thursday, September 29, 2011 9:42 AM
  • Change
    UserForm1!controlname.Value

    to
    UserForm1.Controls(controlname).Value

    where controlname is a string

    Peter Thornton

    Thursday, September 29, 2011 11:17 AM
  • Even that is not working Peter. Getting error message as "Could not find the specified object".

     

    tried to print UserForm1.Controls(controlname).Caption

     

    -Anand

    Tuesday, October 4, 2011 4:59 PM
  • That error message strongly suggests that no such control with whatever name you assigned to the variable controlname exists. Check your code to ensure you have correctly assigned a valid name to the variable, eg

    controlname = "CheckBox1"

    Peter Thornton

    Tuesday, October 4, 2011 5:58 PM
  • Hi,

    i tried printing name of the control and it shows as "CheckBox1",.. so the control with that name exists.

     

    -Anand

    Saturday, October 8, 2011 2:15 PM
  • hi,
       With chkbox.Object
      .Caption = "TheNameYouWant" & i
      .Font.Size = 8
      End With
     --
    isabelle
     
     
    Saturday, October 8, 2011 2:26 PM
  • i tried printing name of the control and it shows as "CheckBox1",.. so the control with that name exists.

    How did you refer to the control in order to print its name

    Try this in a form named UserForm1

    [vbnet]
    Private Sub UserForm_Click()
    Dim ctl As Control

    Set ctl = Me.Controls.Add("forms.checkbox.1")
    ctl.Name = "myCheckbox1"
    ctl.Caption = "hello"
    ctl.AutoSize = True

    MsgBox UserForm1.Controls("myCheckbox1").Name & " " & ctl.Caption

    ' normally would use Me within the userform

    End Sub
    [vbnet]

    Peter Thornton

    • Proposed as answer by danishani Monday, November 28, 2011 10:30 PM
    • Marked as answer by danishani Wednesday, January 11, 2012 8:19 PM
    Saturday, October 8, 2011 4:49 PM
  • Hi Perter,

     

    Your idea works. i am able to get the control name using variable. not sure why it didnt work before when you gave the same suggestion. I'll look into my code again and will use it. Thanks much.

     

    -Anand

    Thursday, October 13, 2011 4:39 AM