Selection - do I need all this code?

vangogh228

Registered User.
Local time
Today, 15:56
Joined
Apr 19, 2002
Messages
302
I have a selection box with 13 choices. Each one makes visible a different legend. All legends are invisible otherwise. I am providing the code for the first 3 here, and wondering "Is there a better way of doing this?" Any help is appreciated. Tom

--------

Private Sub ViewLegend_AfterUpdate()

If ViewLegend = 1 Then
legendA.Visible = True
Else
legendA.Visible = False
End If
If ViewLegend = 2 Then
LegendB.Visible = True
Else
LegendB.Visible = False
End If
If ViewLegend = 3 Then
LegendC.Visible = True
Else
LegendC.Visible = False
End If

------------

etc until End Sub.
 
Code:
Private Sub ViewLegend_AfterUpdate()

    Dim ctl As Control

    [COLOR="Green"]'Set all the legend visible properties to false.[/COLOR]
    For each ctl In Me.Controls
        If left(ctl.Name,6) = "Legend" Then
            ctl.Visible = False
        End If
    Next

    Select Case ViewLegend
        Case 1
            LegendA.Visible = True
        Case 2
            LegendB.Visible = True
        Case 3
            LegendC.Visible = True   
        .
        .
        .
        Case Else
    End Select

End Sub

An even easier way is to set the Tag property of each legend entry to its corresponding ViewLegend value. For example, set the tag of LegendA to 1, LegendB to 2, and so on. From there, it's a simple loop.

Code:
Private Sub ViewLegend_AfterUpdate()

    Dim ctl As Control

    For each ctl In Me.Controls
        If left(ctl.Name,6) = "Legend" Then
            If(ctl.Tag) = ViewLegend
                ctl.Visible = True
            Else
                ctl.Visible = False
            End If
        End If
    Next

End Sub

Note that both examples assume that each legend value's name starts with "Legend" and that you don't have other controls that start with the word "Legend".
 
First chance I've had to get back to this.

Thanks for the input. I will look this over, and learn the logic. On first pass, it seems straightforward enough.

Thanks!!
 

Users who are viewing this thread

Back
Top Bottom