Can I use a LOOP?


OSNN Junior Addict
In my program that's connected to a form I have code like:

'Row 1
If TextExist("A-2-A1") Then
    Me.L1.BackColor = 16776960
    End If
If TextExist("A-2-A2") Then
    Me.L2.BackColor = 16776960
    End If
If TextExist("A-2-A3") Then
    Me.L3.BackColor = 16776960
    End If
If TextExist("A-2-A4") Then
    Me.L4.BackColor = 16776960
    End If
If TextExist("A-2-A5") Then
    Me.L5.BackColor = 16776960
    End If
If TextExist("A-2-A6") Then
    Me.L6.BackColor = 16776960
    End If
If TextExist("A-2-A7") Then
    Me.L7.BackColor = 16776960
    End If
If TextExist("A-2-A8") Then
    Me.L8.BackColor = 16776960
    End If
If TextExist("A-2-A9") Then
    Me.L9.BackColor = 16776960
    End If
If TextExist("A-2-A10") Then
    Me.L10.BackColor = 16776960
    End If
Private Sub L1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    If Me.L1.BackColor = 16776960 Then
        Update ("A-1-A1")
        Me.L1.BackColor = 65535
    End If
End Sub

Private Sub L2_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    If Me.L2.BackColor = 16776960 Then
        Update ("A-1-A2")
        Me.L2.BackColor = 65535
    End If
End Sub

Private Sub L3_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    If Me.L3.BackColor = 16776960 Then
        Update ("A-1-A3")
        Me.L3.BackColor = 65535
    End If
End Sub
Is there anyway to right this using a loop. I don't know how to stick in number variables in the statements [TextExist("A-2-A1")] and [Me.L10,BackColor=1677960]. I think there must be a way tho. And is there any way of writing all those functions as one function with a variable in the name of the function? THANKS.


Debiant by way of Ubuntu
Maybe java with a persistent data store would be one way to go - 'fraid I don;t know much about forms.... but at least I can bump for you and perhaps someone else does?


OSNN Addict
I need a little more information on this one. What are you trying to do with the TextExist part and the Update part? The L1, L2 objects you are referring to are what? Combo boxes or lines?

Some had the answer to adding variables to a string but the posts were lost. I believe it goes something like this...
Update ("A-1-A" & Variable)


Debiant by way of Ubuntu
I believe the lost post suggested using a CASE statement maybe? Seemed a good idea to me and maybe Sanchez has the email anyway


OSNN Addict
That was my original post. But if you look at it a little closer he's setting the background to the same color on all of what ever he's changing. It would be easier just to put it in an array loop.

dave holbon

OSNN Veteran Addict
This can actually be achieved in one loop be designating your (I assume) controls on forms to variables and incrementing then in a for-next loop assigning the constant 16776906 and then the A1, A2 and L1, L2 to strings and manipulating the numerical part of the string (A[1],L[1]) which is the number part, to an integer and using the for next statement all you have to do is assign the For-Next integer to increment by +1 thus using only three lines of code. You will of course, if you are using Visual Basic have to employ the Eval() function to coerce the result back to a string for evaluation purposes (pun). Never use an array if it can be avoided.

Using many If – Then – Else or ElseIf statements forces the compiler or pcode engine to test each instance one after the other and is thus cumbersome and slow, as a general rule I always start to re-think if I find myself writing more than one If-Then- End If statement to test for incremental changes either in strings or other data types.

:) :)

Members online

No members online now.

Latest posts

Latest profile posts

Perris Calderon wrote on Electronic Punk's profile.
Ep, glad to see you come back and tidy up...did want to ask a one day favor, I want to enhance my resume , was hoping you could make me administrator for a day, if so, take me right off since I won't be here to do anything, and don't know the slightest about the board, but it would be nice putting "served administrator osnn", if can do, THANKS

Been running around Quora lately, luv it there
Electronic Punk wrote on Perris Calderon's profile.
All good still mate?
Hello, is there anybody in there? Just nod if you can hear me ...
What a long strange trip it's been. =)

Forum statistics

Latest member