Using a string to point to a form object?

  • Thread starter Thread starter plumbye
  • Start date Start date
P

plumbye

Hi,

How do I make a string that points to a form object.

I have a form with many input textboxes and I would like to make a code
using a for....next to check the input.

For example, i have 20 textboxes named tb_input1, tb_input2 ..etc.

How can I make this idea work
For i=1 to 20
string="tb_input" & i
form.string.value=10
next i

VBA wont accept this at all. I have tried using the Object data type,
but to no avail.

Can anybody help?

Regards,
Plumbye
 
Try the following, which should give you some ideas...

Create a form with three text boxes, and one command button:

txt_1
txt_2
txt_3

CommandButton1

Copy the code below behind the command button:

Private Sub CommandButton1_Click()

Dim i As Integer
Dim txtSource As MSForms.TextBox
Dim strTest As String

For i = 1 To 3
Set txtSource = Me.Controls("txt_" & i)
strTest = txtSource.Text
Next i

Set txtSource = Nothing

End Sub

Not very elegant, but it will give you the values in teh text boxes.
All you need to do is refer to the controls collection with the correct
name - this type of reference will also work with dynamically created
controls.

HTH

Please remove NOSPAM to email, and please post any replies directly to
group.
 
Back
Top