Put a variable into text ?

S

SpookiePower

I have 60 lines like this one



TextBox01.Text = ..

TextBox02.Text = ....

....

....



Now I would like to make it more simple by setting a variable - X - into the TextBox-text

and then do a loop 60 time instead of writing 60 lines.



My idée was to do it like this - TextBox & X &.Text

But it does not work. What is wrong ?
 
B

Bob Phillips

If userform controls

Dim ctl As msforms.Control

For Each ctl In Me.Controls
If TypeName(ctl) = "TextBox" Then
ctl.Value = X
End If
Next ctl



--
HTH

Bob Phillips

(remove nothere from email address if mailing direct)
 
S

SpookiePower

Bob Phillips said:
If userform controls

Dim ctl As msforms.Control

For Each ctl In Me.Controls
If TypeName(ctl) = "TextBox" Then
ctl.Value = X
End If
Next ctl

Thanks...but I have a few questions.

I can understand that I don't have to put .Text after TextBox01 and so on, but
how does this code works ?

When I put this code into my own code and run it, I can see that ctl = label1
I dont know why ctl is holding label1.

I want to run it X times, but I just can't see how to do it.
 
D

Dave Peterson

For each ctrl in me.controls

Tells your program to look at each button, checkbox, textbox, labels,
optionbuttons, etc on that userform.

This line:
If TypeName(ctl) = "TextBox" Then
Says that if the control is a TextBox, then do something special.
ctl.Value = X

But it doesn't do anything else to the other types of controls (buttons,
checkboxes, optionbuttons, etc).
 
B

Bob Phillips

.... and ctl.Value gives the same result as ctl.Text

--
HTH

Bob Phillips

(remove nothere from email address if mailing direct)
 
S

SpookiePower

Dave Peterson said:
For each ctrl in me.controls

Tells your program to look at each button, checkbox, textbox, labels,
optionbuttons, etc on that userform.

This line:
If TypeName(ctl) = "TextBox" Then
Says that if the control is a TextBox, then do something special.
ctl.Value = X

But it doesn't do anything else to the other types of controls (buttons,
checkboxes, optionbuttons, etc).


aaahhh...know I'm starting to understand how this code works.



I have 70 texbox's on my form, but it is only 60 of them that I want

to use. I need especially those 60 textbox's to put some text in.



How do I pick those 60 textboxes from the 70 textboxes ?
 
D

Dave Peterson

You're gonna have to do something to identify which of those textboxes are
which.

Maybe you could use the name?

Maybe the number--1-60 get it, 61-70 don't???

Or give 60 of them a unique prefix--X_Textbox1, X_textbox2, ...

Or maybe you can just give the 10 that don't get it a unique prefix:
N_Textbox1, ...

Then you can check the name...

Option Explicit
Private Sub CommandButton1_Click()
Dim ctl As msforms.Control
For Each ctl In Me.Controls
If TypeName(ctl) = "TextBox" Then
If LCase(Left(ctl.Name, 2)) = "n_" Then
'skip it
Else
ctl.Value = "X"
End If
End If
Next ctl
End Sub
 
S

SpookiePower

Dave Peterson said:
You're gonna have to do something to identify which of those textboxes are
which.

Maybe you could use the name?

I was thinking the same, about using the name of the textbox, but
I was not sure if it was possible to do it this way.

Then you can check the name...

Option Explicit
Private Sub CommandButton1_Click()
Dim ctl As msforms.Control
For Each ctl In Me.Controls
If TypeName(ctl) = "TextBox" Then
If LCase(Left(ctl.Name, 2)) = "n_" Then
'skip it
Else
ctl.Value = "X"
End If
End If
Next ctl
End Sub



Does this line means - If LCase(Left(ctl.Name, 2)) = "n_" Then
that it should looks for the first to letters i the name ?

Thanks for your help :)
 
D

Dave Peterson

Yes, it looks at the first two characters in the name.

If LCase(Left(ctl.Name, 2)) = "n_"

And then compare them against the lower case characters.
 
Top