Results 1 to 5 of 5

Thread: changing a variable

  1. #1

    Thread Starter
    Hyperactive Member Gimpster's Avatar
    Join Date
    Oct 1999
    Location
    Redmond, WA 98052
    Posts
    331

    Post

    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

  2. #2
    Lively Member
    Join Date
    Oct 1999
    Posts
    101

    Post

    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

  3. #3
    Member
    Join Date
    Jan 1999
    Location
    Garden Grove, CA, Orange
    Posts
    55

    Post

    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

  4. #4
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Post

    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).]

  5. #5

    Thread Starter
    Hyperactive Member Gimpster's Avatar
    Join Date
    Oct 1999
    Location
    Redmond, WA 98052
    Posts
    331

    Post

    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
  •  



Click Here to Expand Forum to Full Width