-
Hi,
I have 3 lines on my form - linemove, line1 and line2. I also have a text box and a command button.
The user can enter the number 1 or 2 into the text box. When they click the command button, the coordinates of linemove should match those exactly of line1 or line2 - depending on what number they have typed in the text box.
I thought this code would work, but it doesn't:
Private Sub Cmdchange_Click()
Linemove.X1 = Line & Str(Text1.Text).X1
Linemove.X2 = Line & Str(Text1.Text).X2
Linemove.Y1 = Line & Str(Text1.Text).Y1
Linemove.Y2 = Line & Str(Text1.Text).Y2
End Sub
I get an error and 'Line' is highlighted. Could someone please help me out?
Thanks.
-
Line is being highlighted because it is a vb function in its own right.
You are actually trying to do somthing quite tricky here. That is, reference a control by a name held in a string.
the easiest way in your case is to use a select statment
Code:
Private Sub Cmdchange_Click()
Select Case Val(Text1.Text)
Case 1
Linemove.X1 = Line1.X1
Linemove.X2 = Line1.X2
Linemove.Y1 = Line1.Y1
Linemove.Y2 = Line1.Y2
Case 2
Linemove.X1 = Line2.X1
Linemove.X2 = Line2.X2
Linemove.Y1 = Line2.Y1
Linemove.Y2 = Line2.Y2
End Select
End Sub
-
if you really want to hold the control's name in a string you can do this:
Code:
Private Sub Cmdchange_Click()
Dim ctrl As Control
For Each ctrl In Me.Controls
If ctrl.Name = "Line" & Text1.Text Then
Linemove.X1 = ctrl.X1
Linemove.X2 = ctrl.X2
Linemove.Y1 = ctrl.Y1
Linemove.Y2 = ctrl.Y2
Exit For
End If
Next
End Sub