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

    Set Visible Property for Form Fields Based on Option Group Selection

    Hello,




    I have built a form which includes an option group with three options.

    Depending on the selection that the user makes in the option group, I need to change the visible property of other fields on the form - in other words, there are certain controls (combo boxes and text boxes) that I want to appear or disappear based on the selection the user makes in the option group form.

    The scenario is this - the form deals with fabrics, and the option group contains three options - woven, knitted, and other. If the user selects "woven," with the radio button in the option group then I need to have a group of text boxes and combo boxes that I created that are pertinent only to woven fabrics appear. If the user selects "knitted" with its radio button then I need some of those controls that are only relevant to woven fabrics to disappear and other controls that are only relevant to knitted fabrics to appear in their place.

    It seems like this should be fairly easy to do by just setting the visible property of all the controls to change based on the selection the user makes in the option group, but I don't know how to do this.

    Please let me know if you have any advice on how to achieve this.

    Thanks very much in advance.

  2. #2
    benthamq is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Aug 2011
    Posts
    32
    I have written the below code so far, which does not work.

    fmeKnitWovenOther is the option group.
    fmeKnitWovenOtherLbl is one of the things I would like to change based on the selection the user makes. (On Load, there is no default selection in the option group. I have an unbound label above the option group that says, "Please select one of the following:" I want this label to disappear when any selection is made in the option group. This is only for appearance purposes, unlike the combo boxes and and text boxes which are functional, but I figure it's a good place to start to show my bad code rather than give a long list of all the text boxes and combo boxes that I need to change based on the option group selection.)


    Private Sub Form_Load()
    Dim kwoFrame As String
    kwoFrame = fmeKnitWovenOther
    Dim kwoLabel As String
    kwoLabel = fmeKnitWovenOtherLbl
    End Sub

    Private Sub fmeKnitWovenOther_AfterUpdate()
    kwoLabel.Visible = False
    End Sub


    Thanks so much for any help offered.

  3. #3
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    It would look something like this in the after update event of the frame:

    Code:
    If Me.FrameName = 1 Then
      'do whatever
    ElseIf Me.FrameName = 2 Then
      'do something for 2
    Else
      'must be 3, do whatever
    End If
    Or with Select/Case

    Code:
    Select Case Me.FrameName
      Case 1
        'do whatever
      Case 2
        'do for 2
      Case 3
        'do for 3
    End Select
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  4. #4
    benthamq is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Aug 2011
    Posts
    32
    Thanks very much for the rapid reply.

    I have done this per your advice and it works great:

    Private Sub fmeKnitWovenOther_AfterUpdate()
    If Me.fmeKnitWovenOther = 1 Or 2 Or 3 Then
    Me.fmeKnitWovenOtherLbl.Visible = False
    End If
    End Sub


    I will mark this solved.

  5. #5
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Uh, you didn't do that based on my advice. It's isn't really valid, though based on that little bit it will probably do what you want. Proper syntax would be:

    If Me.fmeKnitWovenOther = 1 Or Me.fmeKnitWovenOther = 2 Or Me.fmeKnitWovenOther = 3 Then

    As is, it evaluates 2 and 3 as Booleans, and both will evaluate to true (being non-zero). You would also typically have an Else clause to set visibility to True if the frame wasn't one of those 3 values (presuming that's what you would want done).
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  6. #6
    benthamq is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Aug 2011
    Posts
    32
    Sorry, I am not good with VBA, at least yet. I have corrected it:

    Private Sub fmeKnitWovenOther_AfterUpdate()

    If Me.fmeKnitWovenOther = 1 Or Me.fmeKnitWovenOther = 2 Or _
    Me.fmeKnitWovenOther = 3 Then
    Me.fmeKnitWovenOtherLbl.Visible = False
    Else
    Me.fmeKnitWovenOtherLbl.Visible = True
    End If

    End Sub

    Thanks again very much for your help.

  7. #7
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Happy to help. To clarify, what you had before was no different than having

    If 2 Then

    It wasn't testing your frame value, and would always have evaluated to True.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

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

Similar Threads

  1. Setting visible property of subreport?
    By GaryElwood in forum Reports
    Replies: 1
    Last Post: 09-23-2011, 07:49 PM
  2. Report Fields Visible Based on Checkbox
    By ghillie30 in forum Access
    Replies: 2
    Last Post: 09-21-2011, 09:04 AM
  3. run parameter query based on option box selection
    By rivereridanus in forum Queries
    Replies: 1
    Last Post: 07-01-2011, 01:07 PM
  4. Replies: 5
    Last Post: 05-17-2011, 11:02 AM
  5. Replies: 1
    Last Post: 02-25-2011, 10:03 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