PDA

Click to See Complete Forum and Search --> : changing a variable


Gimpster
Jan 14th, 2000, 05:40 AM
What I'm trying to do is to read through a form with a bunch of text boxes that have matching labels. I want to check to see if the text box is empty and if so, then I want to change the label's forecolor to red. Here is my code:

Dim x as Object
For Each x In Me.Controls
If Left(x.name, 3) = "txt" Then
If x.name <> "txtdate_terminated" Or x.name <> "txtemp_mi" Or x.name <> "txtnotes" Then
If x.Text = "" Then
Dim name As Variant
name = Mid(x.name, 4)
name = "lbl" + name
name.ForeColor = &HFF&
End If
End If
End If
Next x

I also tried defining the name variable as a string and defining another variable as an object and then setting the object equal to the string, but that didn't work. All of my text boxes' names begin with "txt" and all of my labels' names begin with "lbl". So all I need to be able to do is to replace the "txt" with "lbl". Any suggestions are welcome. Thank you.

------------------
Ryan

ChrisJackson
Jan 14th, 2000, 06:03 AM
Hi Ryan.

After I worked on it for a while, I saw why this one gave you problems. But here's a solution.

In the Tag Property of your labels that correspond with Text Boxes, put the name of the Text box. For example, set the Tag property of lblOne to txtOne, and set the Tag property of lblTwo to txtTwo, and so on.

Then, replace your code with this code:

Dim x As Object
For Each x In Me.Controls
If Left(x.name, 3) = "txt" Then
If x.name <> "txtdate_terminated" Or x.name <> "txtemp_mi" Or x.name <> "txtnotes" Then
If x.Text = "" Then
Dim y As Object
For Each y In Me.Controls
If y.Tag = x.name Then
y.ForeColor = &HFF&
Exit For
End If
Next y
End If
End If
End If
Next x.

There may have been an easier way to do this, but it's Friday and I'm going home! :)

All the best.

Chris

jpark
Jan 14th, 2000, 06:17 AM
Or Try this


Dim txtControl As Control
Dim lblControl As Control

For Each varControl In Form1.Controls
If TypeOf varControl Is TextBox Then
If (varControl.name <> "txtdate_terminated" Or varControl.name <> "txtemp_mi" _
Or varControl.name <> "txtnotes") And varControl.Text = "" Then

For Each lblControl In Form1
If TypeOf lblControl Is Label Then
If Mid(lblControl.name, 4) = Mid(varControl.name, 4) Then _
lblControl.ForeColor = &HFF&
End If
Next

End If
End If
Next


Joon

MartinLiss
Jan 14th, 2000, 06:17 AM
This may be easier. Change your TextBoxes from individual controls to be members of a control array. Do the same thing for the matching labels. That way if you determine that MyTextBox(2) is blank, you know you need to change MyLabel(2). By the way, to determine if an object is a TextBox, you don't need to know it's name (or prefix). Just do this:For Each x In Controls
If TypeOf x Is TextBox Then
' It's a Textbox
End If
Next


------------------
Marty

[This message has been edited by MartinLiss (edited 01-14-2000).]

Gimpster
Jan 14th, 2000, 06:23 AM
Yes!!! Thank you so much, you're a life saver, ChrisJackson! And thank you to MartinLiss and jpark as well.

------------------
Ryan


[This message has been edited by Gimpster (edited 01-14-2000).]