|
-
Jan 14th, 2000, 06:40 AM
#1
Thread Starter
Hyperactive Member
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:
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
-
Jan 14th, 2000, 07:03 AM
#2
Lively Member
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:
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
-
Jan 14th, 2000, 07:17 AM
#3
Member
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
-
Jan 14th, 2000, 07:17 AM
#4
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:
Code:
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).]
-
Jan 14th, 2000, 07:23 AM
#5
Thread Starter
Hyperactive Member
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).]
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|