Can I use a LOOP?

Sanchez

OSNN Junior Addict
Joined
8 Jun 2004
Messages
14
In my program that's connected to a form I have code like:

Code:
'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
AND
Code:
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
'etc.

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.
 
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?
 
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...
Code:
Update ("A-1-A" & Variable)
 
I believe the lost post suggested using a CASE statement maybe? Seemed a good idea to me and maybe Sanchez has the email anyway
 
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.
 
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 profile posts

Also Hi EP and people. I found this place again while looking through a oooollllllldddd backup. I have filled over 10TB and was looking at my collection of antiques. Any bids on the 500Mhz Win 95 fix?
Any of the SP crew still out there?
Xie wrote on Electronic Punk's profile.
Impressed you have kept this alive this long EP! So many sites have come and gone. :(

Just did some crude math and I apparently joined almost 18yrs ago, how is that possible???
hello peeps... is been some time since i last came here.
Electronic Punk wrote on Sazar's profile.
Rest in peace my friend, been trying to find you and finally did in the worst way imaginable.

Forum statistics

Threads
62,015
Messages
673,494
Members
5,621
Latest member
naeemsafi
Back