Results 1 to 5 of 5
  1. #1
    Join Date
    May 2005
    Posts
    1,191

    Unanswered: Array/Collection Issue

    I have a form that I need to execute some code for a few different text boxes on the form, call them TxtBox1, TxtBox2, and TxtBox3. For each control, I need to execute the following code:
    Code:
    If me.TxtBox1.Text = "" Or IsNull(TxtBox1) Then
            me.TxtBox1.BackColor = vbYellow
            blnIncomplete = True
        Else
            me.TxtBox1.BackColor = vbWhite
        End If
    Instead of retyping this for each control (as there could potentially be 10 or more in my case), is there a way to execute this in a For loop? I'm thinking something like:
    Code:
    For Each Control In RequiredField
        If Control.Text = "" Or IsNull(Control) Then
            Control.BackColor = vbYellow
            blnIncomplete = True
        Else
            Control.BackColor = vbWhite
        End If
    Next
    What I'm wondering is how do I define the Array/Collection RequiredField to contain a select group of controls on the form. Thanks in advance to anyone who can help.
    Me.Geek = True

  2. #2
    Join Date
    Dec 2004
    Location
    Madison, WI
    Posts
    3,926

    Array

    There are 2 things I can think of:

    1. In regards to changing the colors of the text fields, you do realize this can be done via the conditional formatting.

    2. You could also consider putting the text field box names in a table itself and just loop through the fields in the table. One way to do it but you'll probably get some better ways.
    Expert Database Programming
    MSAccess since 1.0, SQL Server since 6.5, Visual Basic (5.0, 6.0)

  3. #3
    Join Date
    Mar 2003
    Location
    The Bottom of The Barrel
    Posts
    6,102
    Provided Answers: 1
    A handy and common method of creating arbitrary control groups is to use the "tag" property. The Tag property is exactly that, an arbitrary "tag" to use as you wish. An example of this might be:

    For Each ctrl In me.Controls
    If ctrl.tag = "YourGroup" Then
    'do some stuff
    end if
    Next ctrl
    oh yeah... documentation... I have heard of that.

    *** What Do You Want In The MS Access Forum? ***

  4. #4
    Join Date
    Feb 2004
    Location
    One Flump in One Place
    Posts
    14,912
    Or use the tag property or some sort of naming convention. Air code:

    Code:
    Dim c as Control
     
    
    For Each c In Me.Controls
    If c.tag = "ColourMeBad" Then If Nz(c.Text, vbNullString) = vbNullString Then c.BackColor = vbYellow blnIncomplete = True Else c.BackColor = vbWhite End If End If Next c
    EDIT - bugger - Teddy in there like a flash as ever....
    Testimonial:
    pootle flump
    ur codings are working excelent.

  5. #5
    Join Date
    May 2005
    Posts
    1,191
    Thanks for the help!
    Me.Geek = True

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •