Select Case.....AND?

  • Thread starter Thread starter CJ
  • Start date Start date
C

CJ

Hi Groupies

I don't have any experience with Select Case, I usually use complex IIF
statements. However, in this case, I think Case would be much more
efficient, if I can get this portion below to work.

My issue is that I need to test for more than 1 condition at the same time.
I need to test 2 different dates against 2 other dates but can the Select
Case have an AND statement like this?

Private Sub GroupHeader0_Format(Cancel As Integer, FormatCount As Integer)
Dim lblInvalidLabel1 As Label
Dim txtSomething As Single

Select Case txtSomething
Case Me.txtFormStart < Me.dtmStartDate And Me.txtFormEnd > Me.dtmEndDate
Me.txtSomething = 123
Me.lblInvalidLabel1.Visible = True
Case Else
Me.txtSomething = 999
Me.lblInvalidLabel1.Visible = False
End Select

End Sub

In my report, I am getting the first Case result no matter what.

If I can not use AND, how would I need to write this?
 
That will not work unless txtsomething is returning True or false. You
might try an If ...elseIf...end if structure

IF Me.txtFormStart < Me.dtmStartDate And Me.txtFormEnd > Me.dtmEndDate Then
Me.txtSomething = 123
Me.lblInvalidLabel1.Visible = True
Else
Me.txtSomething = 999
Me.lblInvalidLabel1.Visible = False
End If


If you had multiple conditions to check then
IF Me.txtFormStart < Me.dtmStartDate Then
Me.txtSomething = 123
Me.lblInvalidLabel1.Visible = True
ElseIf Me.txtFormEnd > Me.dtmEndDate Then
Me.txtSomething = 999
Me.lblInvalidLabel1.Visible = False
Elseif me.txtformStart <> me.txtFormEnd then
'Oh my goodness do something
else
'Didn't pass any tests so do this as default
End If

'====================================================
John Spencer
Access MVP 2002-2005, 2007-2009
The Hilltop Institute
University of Maryland Baltimore County
'====================================================
 
Thanks John

I thought I would give the Case a try but I'm sure I can find a
use for it somewhere else.

Have a good one!
 
What you have is okay, but if you wanted a fun exercise, you can use an
experssion that evaluates to true/false in a Select Case like this:

Select Case True
Case ((Me.txtFormStart < Me.dtmStartDate) And _
(Me.txtFormEnd > Me.dtmEndDate))
Case ...
 
Back
Top