Results 1 to 3 of 3
  1. #1
    benthamq is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Aug 2011
    Posts
    32

    Setting Properties for Multiple Objects in a Collection or Array Simultaneously

    Hello,



    I have a form which includes an option group and multiple controls that I would like to hide if a user selects a certain option in the option group.

    The option box is called fmeKnitWovenOther. There is a label called fmeKnitWovenOtherLbl that I would like to hide when any option is selected and a number of controls I would like to hide only if the user selects option 2. I have this so far and it works OK:

    Private Sub fmeKnitWovenOther_AfterUpdate()

    If Me.fmeKnitWovenOther = 1 Then
    Me.fmeKnitWovenOtherLbl.Visible = False
    ElseIf Me.fmeKnitWovenOther = 2 Then
    Me.fmeKnitWovenOtherLbl.Visible = False
    Me.txtThreadsWarp.Visible = False
    Me.txtThreadsWarpLbl.Visible = False
    Me.cmbThreadsWarpUOM.Visible = False
    Me.txtThreadsWeft.Visible = False
    Me.txtThreadsWeftLbl.Visible = False
    Me.cmbThreadsWeftUOM.Visible = False
    Me.fmeCombedOrCarded.Visible = False
    Me.txtYarnSizeWarp.Visible = False
    Me.txtYarnSizeWarpLbl.Visible = False
    Me.cmbWarpYarnUOM.Visible = False
    Me.txtYarnSizeWeft.Visible = False
    Me.txtYarnSizeWeftLbl.Visible = False
    Me.cmbWeftYarnUOM.Visible = False
    Me.txtFabricWeight.Visible = False
    Me.txtFabricWeightLbl.Visible = False
    Me.cmbFabricWeightUOM.Visible = False
    ElseIf Me.fmeKnitWovenOther = 3 Then
    Me.fmeKnitWovenOtherLbl.Visible = False
    Else
    Me.fmeKnitWovenOtherLbl.Visible = True
    End If

    End Sub

    The problem is that I have set the .visible property of every control individually, but I will need to expand this form and add more controls to be hidden for the other two options. If I keep setting the .visible property for every control manually, I am going to wind up with a giant list of controls all manually set to .visible = True and .visible = False depending on the If or ElseIf condition.

    It seems like there has to be a better way. Does anyone know if it is possible to create a collection, array, or enumeration of each group of controls and then set the .visible property of just that collection for each If / ElseIf condition? Or is there just some other way to achieve this more neatly?
    Please let me know any thoughts you have.

    Thank you very much in advance for any help you can offer.

  2. #2
    ajetrumpet is offline VIP
    Windows Vista Access 2007
    Join Date
    Mar 2010
    Location
    N/A
    Posts
    2,694
    there IS a better way. the thing that I do, which surprisingly I have not seen on the web very much at all, is place consistency in the control names so they can be looped. for instance, tag them with a number and make all of the controls have some string in common.

    here's a good example:

    say I had a form with 50 entry fields, each one pertinent to each other in some way, but they were split in 2 groups based on entry criteria or something. I would probably name them like this:

    *txtBox1Group1
    *txtBox2Group2
    *txtBox3Group1
    *etc...

    then, if I want to disappear the ones in group 2, I would simply write this behind an event:

    Code:
    dim c as control
      for each c in me.controls
        if typeof c is textbox then
          if right(c.name, 6) = "Group2" then
            c.visible = false
          end if
        end if
      next c
    
    me.repaint

  3. #3
    benthamq is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Aug 2011
    Posts
    32
    Thank you very much for your help; I had not thought of using the Right() function like this.

    I will mark this solved.

    I am still working on the form and have much more to add but this is the direction I am taking it and it works superbly.

    Private Sub fmeKnitWovenOther_AfterUpdate()

    Dim c As Control

    If Me.fmeKnitWovenOther = 1 Then
    Me.fmeKnitWovenOtherLbl.Visible = False
    For Each c In Me.Controls
    If Right(c.Name, 7) = "WvnSpec" Then
    c.Visible = True
    End If
    Next c
    Me.Repaint
    ElseIf Me.fmeKnitWovenOther = 2 Then
    Me.fmeKnitWovenOtherLbl.Visible = False
    For Each c In Me.Controls
    If Right(c.Name, 7) = "WvnSpec" Then
    c.Visible = False
    End If
    Next c
    Me.Repaint
    ElseIf Me.fmeKnitWovenOther = 3 Then
    Me.fmeKnitWovenOtherLbl.Visible = False
    For Each c In Me.Controls
    If Right(c.Name, 7) = "WvnSpec" Then
    c.Visible = False
    End If
    Next c
    ElseIf Me.fmeKnitWovenOther = 4 Then
    Me.fmeKnitWovenOtherLbl.Visible = False
    For Each c In Me.Controls
    If Right(c.Name, 7) = "WvnSpec" Then
    c.Visible = False
    End If
    Next c
    ElseIf Me.fmeKnitWovenOther = 5 Then
    Me.fmeKnitWovenOtherLbl.Visible = False
    For Each c In Me.Controls
    If Right(c.Name, 7) = "WvnSpec" Then
    c.Visible = False
    End If
    Next c
    Else
    Me.fmeKnitWovenOtherLbl.Visible = True
    End If

    End Sub

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Setting Field Properties for Numbers
    By Tim Hardison in forum Access
    Replies: 1
    Last Post: 12-09-2009, 06:47 AM
  2. Users on Database Simultaneously
    By rtemple in forum Access
    Replies: 2
    Last Post: 03-26-2009, 08:38 PM
  3. Relocating objects via "Left" properties value
    By Ripcut in forum Programming
    Replies: 0
    Last Post: 08-01-2008, 06:40 PM
  4. One table multiple fields identical properties.
    By swampdonkey in forum Queries
    Replies: 2
    Last Post: 09-29-2006, 10:53 AM
  5. Setting combo box properties
    By rathfam4 in forum Programming
    Replies: 1
    Last Post: 12-28-2005, 02:27 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums