Results 1 to 9 of 9
  1. #1
    Join Date
    Aug 2004
    Posts
    364

    Unanswered: Displaying labels based on Combobox selections?

    Hello,

    I need suggestions as to the best way to approach displaying labels on a form based on selections made in two combo boxes.

    Here is the senario -

    Two combo boxes on a form - "combo1" and "combo2" both with Yes / No selections.

    Three labels on the form "label1", "label2" and "label3".

    If combo1 and combo2 both display "yes" then display label2 and hide label1 and label3.

    If combo1 and combo2 both display "no" then display label1 and hide label2 and label3

    If combo1 displays "yes" and combo2 displays "no then display label3 and hide label1 and label2

    Is it best for me to use an IF statement, should I use a select case statement? Also, under which event should I put any code?

    Thankyou

  2. #2
    Join Date
    Mar 2009
    Posts
    5,442
    Provided Answers: 14
    Code:
    If Me.combo1.Value = "yes" And Me.combo2.Value = "yes" Then 
        Me.label2.Visible = True 
        Me.label1.Visible = False
        Me.label3.Visible = False
    ElseIf Me.combo1.Value = "no" And Me.combo2.Value = "no" Then
        Me.label1.Visible =  True
        Me.label2.Visible =  False
        Me.Me.label3.Visible = False 
    ElseIf Me.combo1.Value = "yes" And Me.combo2.Value = "no" Then 
        Me.label3.Visible =  True
        Me.label1.Visible =  False
        Me.label2.Visible =  False
    Else ' Me.combo1.Value = "no" And Me.combo2.Value = "yes"
         ' The fourth case is missing 
    End If
    Have a nice day!

  3. #3
    Join Date
    Aug 2004
    Posts
    364
    Thankyou for replying, that code is what I had come up with, but nothing is working. I have put the code in the form "on current" event procedure. Where should I place the code?

  4. #4
    Join Date
    Sep 2006
    Posts
    265
    Try setting the default values for combi1 and combi2 = No

    Label1, Label2 and Label3 property Visible No

    On Enter Combi1
    Me.combo1.Value="no"
    Me.combo2.Value="no"
    Me.label1.Visible = False
    Me.label2.Visible = False
    Me.label3.Visible = False

    The On Enter resets the values should a user wants different values to the previous set.

    On Exit Combi1 Goto Combi2

    Use on Exit because combi1 may not be updated

    On Exit Combi2

    If Me.combo1.Value = "yes" And Me.combo2.Value = "yes" Then
    Me.label2.Visible = True
    ElseIf Me.combo1.Value = "no" And Me.combo2.Value = "no" Then
    Me.label1.Visible = True
    ElseIf Me.combo1.Value = "yes" And Me.combo2.Value = "no" Then
    Me.label3.Visible = True

    You may wish to vary method.

    Simon

  5. #5
    Join Date
    Aug 2004
    Posts
    364
    Im sorry I must be doing something wrong, I have followed your advice (or think I have!) and the labels remain hidden.

  6. #6
    Join Date
    Sep 2006
    Posts
    265
    Are the combibox values string or boolean?

    Simon

  7. #7
    Join Date
    Aug 2004
    Posts
    364
    String data type

  8. #8
    Join Date
    Mar 2009
    Posts
    5,442
    Provided Answers: 14
    1 Place the code I sent into a function in the form module:
    Code:
    Private Function AdjustLabels()
        If Me.combo1.Value = "yes" And Me.combo2.Value = "yes" Then 
            Me.label2.Visible = True 
            Me.label1.Visible = False
            Me.label3.Visible = False
        ElseIf Me.combo1.Value = "no" And Me.combo2.Value = "no" Then
            Me.label1.Visible =  True
            Me.label2.Visible =  False
            Me.Me.label3.Visible = False 
        ElseIf Me.combo1.Value = "yes" And Me.combo2.Value = "no" Then 
            Me.label3.Visible =  True
            Me.label1.Visible =  False
            Me.label2.Visible =  False
        Else ' Me.combo1.Value = "no" And Me.combo2.Value = "yes"
              ' The fourth case is missing 
        End If
    End Function
    Now, use the AfterUpdate event of each combo:
    Code:
    Private Sub Combo1_AfterUpdate()
        AdjustLabels
    End Sub
    
    Private Sub Combo2_AfterUpdate()
        AdjustLabels
    End Sub
    Have a nice day!

  9. #9
    Join Date
    Aug 2004
    Posts
    364

    backha as

    Brilliant, you have cracked it! I didn't think that making labels display would be harder than I thought it would be! Thankyou for all your help

Posting Permissions

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