Variable Control name

B

Brett

Hi there, I have a sub which gets the value of a Userform (name UF0_QCP)
control name passed into it (tb) but I'm having a little difficulty with the
syntax:

Dim TBC As control
Set TBC = Controls("UF0_QCP." & tb)

What is the missing link please? Regards, Brett
 
J

Jacob Skaria

I am not sure I understood your question...Are you trying to pass the control
name as a variable ....as below

Private Sub CommandButton1_Click()
Dim TBC As Control
Dim strControl As String
strControl = "TextBox1"
Set TBC = UserForm1.Controls(strControl)
MsgBox TBC.Text
End Sub
 
R

Rick Rothstein

While the UserForm is the default object for all controls on it (meaning you
don't have to qualify the control), you can do it (if you want) this way...

Set TBC = UF0_QCP.Controls(tb)

Note: Controls is a property of the UserForm
 
B

Brett

Hi Rick, thanks for that, works like a charm. Are you suggesting that the
code could be simpler "(if you want)"? Regards, Brett
 
B

Brett

Hi Jacob, thanks for your response (apologies for delay in returning - had to
sleep!).
I tried Rick's suggestion first and it did the job. Regards, Brett
 
R

Rick Rothstein

If the code is in the UserForm's module, then you can refer to the UserForm
using the Me object...

Set TBC = Me.Controls(tb)

or, since the Controls parent is always the UserForm (when the code is
within the UserForm's module), you can leave it out altogether...

Set TBC = Controls(tb)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top