Variable Control Names

G

Garry Jones

Within a user form I am trying to reduce the amount of code needed to
enter text into text box

I have two text boxes. TextBox1 and TextBox2

When Command Button 1 is clicked I want the result
TextBox1.Text = "This is my test text"

When Command Button 2 is clicked I want the result
TextBox2.Text = "This is my test text"

But I wish to use the same piece of code for both examples.

I am missing something here. This is my test example.

_____________________________________________________________

Private Sub CommandButton1_Click()
setar (1)
'when Command Button 1 is clicked it sends the number 1 as a variable to
the code
End Sub
_____________________________________________________________

Private Sub CommandButton2_Click()
setar (2)
'when Command Button 2 is clicked it sends the number 2 as a variable to
the code
End Sub
_____________________________________________________________

Private Sub setar(ArgValue As Integer)
TextBoxArgValue.Text = "This is my test text"
'Missing something here
End Sub

Bottom Line
-----------

How do I get Argvalue to place itself between the x of TextBoxand the .

?

On a similar note, how do I add this number mid text?

When Command Button 1 is clicked I want the result
TextBox1.Text = "This is my test 1 text"

When Command Button 2 is clicked I want the result
TextBox2.Text = "This is my test 2 text"



Garry Jones
Sweden

PS, Yes I know I can get the same effect by using unique code in each
command button control. But I have 198 command buttons so I want to use
the same code 198 times.
 
A

Anders S

Garry,

Try this for a start:

'--------
Private Sub setar(ArgValue As Integer)
Dim boxName As String
boxName = "Textbox" & ArgValue
Me.Controls(boxName).Value = boxName
End Sub
'--------

HTH
Anders Silvén
 

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