1

I am currently trying you learn VB6 and came across this issue. I wanted to loop through a for loop and adding a number to a control name.

Dim I As Integer

For I = 1 To 5
    S = CStr(I)
    If TextS.Text = "" Then
        LabelS.ForeColor = &HFF&
    Else
        LabelS.ForeColor = &H80000012
    End If
Next I

This S needs to be added to Text and Label so the colour will be changed without needing to use 5 If Else statements

I hope you can help me with this.

0

1 Answer 1

3

From your comment below:

What i mean is this: If Text1.text = "" Then I need this 1 to be replaced with the variable I, so the for loop can loop through my 5 textboxes and the same for my Labels.

You can't do that (look up a variable using an expression to create its name) in VB6. (Edit: While that statement is true, it's not true that you can't look up form controls using a name from an expression. See "alternative" below.)

What you can do is make an array of your textboxes, and then index into that array. The dev env even helps you do that: Open your form in the dev env and click the first textbox. Change its name to the name you want the array to have (perhaps TextBoxes). Then click the next textbox and change its name to the same thing (TextBoxes). The dev env will ask you:

"You already have a control named 'TextBoxes'. do you want to create a control array?"

(Don't ask me why I have a VM lying around with VB6 on it...)

Click Yes, and then you can rename your other textboxes TextBoxes to add them to the array. Then do the same for your labels.

Then your code should look like this:

For I = TextBoxes.LBound To TextBoxes.UBound
    If TextBoxes(I).Text = "" Then
        Labels(I).ForeColor = &HFF&
    Else
        Labels(I).ForeColor = &H80000012
    End If
Next

LBound is the lowest index of the control array, UBound is the highest. (You can't use the standard LBound and Ubound that take the array as an argument, because control arrays aren't quite normal arrays.) Note also that there's no need to put I on the Next line, that hasn't been required since VB4 or VB5. You can, though, if you like being explicit.

Just make sure that you have exactly the same number of TextBoxes as Labels. Alternately, you could create a user control that consisted of a label and a textbox, and then have a control array of your user control.


Alternative: : You can use the Controls array to look up a control using a name resulting from an expression, like this:

For I = 1 To 5
    If Me.Controls("Text" & I).Text = "" Then
        Me.Controls("Label" & I).ForeColor = &HFF&
    Else
        Me.Controls("Label" & I).ForeColor = &H80000012
    End If
Next

This has the advantage of mapping over to a very similar construct in VB.Net, should you migrate at some point.


Side note:

I am currently trying you learn VB6...

(tl;dr - I'd recommend learning something else instead, VB6 is outdated and the dev env hasn't been supported in years.)

VB6's development environment has been discontinued and unsupported for years (since 2008). The runtime is still (I believe) supported because of the sheer number of apps that use it, although the most recent patch seems to be from 2012. But FWIW, you'd get a better return on your study time learning VB.net or C#.Net (or any of several non-Microsoft languages), rather than VB6...

Sign up to request clarification or add additional context in comments.

5 Comments

What i mean is this: If Text1.text = "" Then I need this 1 to be replaced with the variable I, so the for loop can loop through my 5 textboxes and the same for my Labels. I am very sorry for being so unclear
Thank you ver much!!, This was exactly what i needed. Sorry again for being so unclear. Kind regards, Tim Smits
Note: if you ever plan to migrate your code to some .NET platform, don't use control arrays.
Okay, I will keep it in mind, but I don't think it will happen since I only need VB6 for an internship.
@user2479441: FWIW, I added an alternative which would migrate better, but based on the above it doesn't matter. It's too bad your internship requires you to use something so incredibly outdated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.