Multiple Select Case

todgers

Registered User.
Local time
Today, 22:48
Joined
Mar 28, 2006
Messages
42
Hiya is there an easier way of grouping together 'Select Case' statements rather than this way I have done them. They work but I still have more to add and feel this way is a little long winded!
Code:
Private Sub Form_Current()

Select Case DatePart("w", Tape_Date)
    Case 1, 3, 5
        Me.Datapulse.Visible = True
        Me.Label86.Visible = True
        Me.Label87.Visible = True
    Case Else
        Me.Datapulse.Visible = False
        Me.Label86.Visible = False
        Me.Label87.Visible = False
End Select

Select Case DatePart("w", Create_U607ST50)
    Case 6
        Me.Create_U607ST50.Visible = True
        Me.Label88.Visible = True
        Me.Label89.Visible = True
        Me.Label129.Visible = True
    Case Else
        Me.Create_U607ST50.Visible = False
        Me.Label88.Visible = False
        Me.Label89.Visible = False
        Me.Label129.Visible = False
    End Select
    
Select Case DatePart("w", U607ST94)
    Case 6
        Me.U607ST94.Visible = True
        Me.Label90.Visible = True
        Me.Label91.Visible = True
    Case Else
        Me.U607ST94.Visible = False
        Me.Label90.Visible = False
        Me.Label91.Visible = False
    End Select

End Sub
 
Of course I don't know what your form looks like, or what you are ultimately trying to achieve but the below may be easier to maintain...

Code:
Private Sub Form_Current()
    dim bVisDataPulse as boolean
    dim bVisCreateU607ST50 as boolean
    dim bVisU607ST94 as boolean

    Select Case DatePart("w", Tape_Date)
    Case 1, 3, 5
        bVisDataPulse = True
    Case Else
        bVisDataPulse = False
    End Select

    Select Case DatePart("w", Create_U607ST50)
    Case 6
        bVisCreateU607ST50 = True
    Case Else
        bVisCreateU607ST50 = False
    End Select
    
    Select Case DatePart("w", U607ST94)
    Case 6
        bVisU607ST94 = True
    Case Else
        bVisU607ST94 = False
    End Select

    With Me
        .Datapulse.Visible = bVisDataPulse 
        .Label86.Visible = bVisDataPulse 
        .Label87.Visible = bVisDataPulse 

        .Create_U607ST50.Visible = bVisCreateU607ST50 
        .Label88.Visible = bVisCreateU607ST50 
        .Label89.Visible = bVisCreateU607ST50 
        .Label129.Visible = bVisCreateU607ST50 
    
        .U607ST94.Visible = bVisU607ST94 
        .Label90.Visible = bVisU607ST94 
        .Label91.Visible = bVisU607ST94 
    End With
End Sub

I would also suggest giving your objects more meaningful names than "Label86" etc.

HTH

Regards

John
 
Last edited:
OK Thanks

Any chance you can just explain the benefits of having it that way rather than the way I was doing, as it still seems a rather long winded way of doing it?
 
Readability & Maintenance

I find it easier to understand what the intent is, with less detailed examination... are those the *same* three (four, five whatever) controls being set to opposite values on either side of the else?

Plus, if you want to add/change/delete one, you only have one line of code to add/change/delete, instead of two.

That's all really.
 
A couple of other observations:
1) You don't need to explicitly hide a label that is a child of another control. A label that is attached to a textbox, for instance, is hidden automatically when the textbox is hidden. This may save you a few lines of code.
2) You don't need an If...Else...End If or Select Case block to toggle a boolean value, but rather you can assign the result of a boolean expression directly. Consider...
Code:
If x = 6 Then
  y.visible = true
Else
  y.visible = false
end if
has the same effect as...
Code:
y.visible = (x = 6)
so your code might be simplified, assuming one of the labels is the child of another control, as follows...
Code:
[COLOR="Green"]'evaluate the visibility of the control[/COLOR]
Me.DataPulse.Visible = (DatePart("w", Tape_Date) Like "[1, 3, 5]")
[COLOR="Green"]'assign the visibility of the control to it's associated labels[/COLOR]
Me.Label87.Visible = Me.DataPulse.Visible

Me.Create_U607ST50.Visible = DatePart("w", Create_U607ST50) = 6
Me.Label89.Visible = Me.Create_U607ST50.Visible
Me.Label129.Visible = Me.Create_U607ST50.Visible

Me.U607ST94.Visible = DatePart("w", U607ST94) = 6
Me.Label91.Visible = Me.U607ST94.Visible

Finally, to make a label a child of another control, select and "Cut" the label, then select the parent control to be, and "Paste" the label.
 
Last edited:
Thanks for that, just another point

Finally, to make a label a child of another control, select and "Cut" the label, then select the parent control to be, and "Paste" the label.

Is it possible to have more than one label/child assigned to one parent
 
Slight Glitch

When I try to add a new record I get
Code:
Run Time Error: 94

Invalid use of Null

The debug highlights the first line of code
Code:
Me.Create_U607ST50.Visible = DatePart("w", Tape_Date) = 6
 
Your DatePart() function is returning a null, probably because
Code:
IsNull(Tape_Date) = True
You can ammend lines as follows...
Code:
Me.Create_U607ST50.Visible = [COLOR="DarkRed"]Nz([/COLOR]DatePart("w", Tape_Date)[COLOR="DarkRed"], -1)[/COLOR] = 6
This ensures your DatePart function returns an integer even if Tape_Date is null, and using -1 ensures that it will not be mistaken for a real weekday.
Cheers,
Mark
 
Mark

Thanks for all your help, its much appreciated!
 

Users who are viewing this thread

Back
Top Bottom