Results 1 to 2 of 2

Thread: [Access 2003] Hiding a subform based off of a selection

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2007
    Posts
    1

    [Access 2003] Hiding a subform based off of a selection

    Greetings everyone!

    First off, I know very little about VBA. What I do know, I've copied modified it to work in my databases.

    Ok, here's my problem that I can not figure out. I hope I can explain it right

    1. I have a sub form that I want hidden unless it matches text in a field.
    2. The field that I will be using is a Combo Box using a look up to a table. I have 5 different answers that could be selected. Let's call them :

    Answer 1
    Answer 2
    Answer 3
    Answer 4
    Answer 5

    So far this is what I have and it works fine. It hides the sub form unless Answer1 is selected.


    Code:
    Private Sub PersonnelType_AfterUpdate()
    Dim ctl As Control
        
        For Each ctl In Me.Controls
            If Me.Controls(ctl.Name).Tag = "MyControllName" Then
                Me.Controls(ctl.Name).Visible = False
            End If
        Next ctl
        If Me!PersonnelType = "Answer 1" Then
            For Each ctl In Me.Controls
                If Me.Controls(ctl.Name).Tag = "MyControllName" Then
                    Me.Controls(ctl.Name).Visible = True
                End If
            Next ctl
        End If
    
    End Sub
    However, what I need is the sub-form to be hidden unless Answer1 OR Answer 4 is selected. It will still remain hidden if Answer 2, 3, or 5 is selected but if Answer 1 OR Answer 4 (or more if I want) is selected, it will show the sub-form so I can enter data.

    I've tried using Or but like I said, I really don't know VBA to figure out what will work.

    Can anyone please lend a helping hand?

    Thank you!

  2. #2
    Addicted Member
    Join Date
    Nov 2006
    Posts
    170

    Re: [Access 2003] Hiding a subform based off of a selection

    one thing i often do when i want to be adjusting the visible toggle on and off is to set the name of the items in question to all have something simaler. For example: Txt_customers_customername, lst_customers_locations etc

    you can then do
    Code:
    for each ctl in me.controls
      if instr(ctl.name, "_customers_") > 0 then
        ctl.visible=true/false
      end if
    next ctl
    in your case your probably going to want to run that code based on the value of your combo-box. I would suggest you organize the items in the combobox so that the true and false items are together. This will allow for:
    Code:
    if combo1.value > 3 then
    Last edited by Xilbus; Aug 9th, 2007 at 02:31 PM.
    You're the King? Well I didn't vote for you...

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