Easy One - Syntax for variables in TextBox & Label names

M

Marty

Hello:

What is the correct syntax for including a variable (in this case "i") in
the name of a TextBox and/or Label?

I have a list of TextBoxes and Labels on a userform and I want to program a
reset button to change what is displayed back to the defaults. I need
something like this:

Private Sub CommandButton8_Click()
For i = 1 To 13
TextBox(i).Text = "0"
Label(i).Caption = "N/A"
Next i
End Sub

I'm getting a Sub or Function not defined error, and I know it's because the
syntax is wrong. Help is appreciated.

Thanks,
MARTY
 
J

JLGWhiz

Could be looking for the parent to the textbox and label. i.e.
UserForm1.TextBox(i) and UserForm1.Label(i), etc.
 
M

merjet

For i = 1 To 13
Controls("TextBox" & i).Value = "0"
Controls("Label" & i).Caption = "N/A"
Next i

Hth,
Merjet
 
M

Marty

Nope. I tried this as a test based on your respone:

Private Sub CommandButton8_Click()
For i = 9 To 13
DATAENTRY.TextBox(i).Text = "0"
Next i
End Sub

and now I get a "method or data member not found" error. The ".TextBox"
part is highlighted while the error message is up, so apparently it doesn't
like "TextBox" without an integer suffix. This is what I'm trying to do:
make a variable suffix.

Other ideas?
 
I

Incidental

Hi

the code below is one way to do it

Option Explicit
Dim Ctrl As Control
Dim i As Integer
Private Sub CommandButton1_Click()

For i = 1 To 13

Set Ctrl = Me.Controls("TextBox" & i)

Ctrl.Value = ""

Set Ctrl = Me.Controls("Label" & i)

Ctrl.Caption = ""

Next

End Sub

Hope this helps

Steve
 

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