VBA: Select Case

M

Martin

FOR EXAMPLE:

Select Case [salary]
Case 56 To 100
a1 = 1
Case 26 To 35
a1 = 5
End Select

MY QUESTION IS: if I got a salary value of "58.3", is it(58.3) belong to
"case 56 to 100" ? That is, if [salary]=58.3 (instead of integer 58 or 59)
, can a1=1 ????

Thanks!

Martin
 
A

Arvin Meyer [MVP]

Martin said:
FOR EXAMPLE:

Select Case [salary]
Case 56 To 100
a1 = 1
Case 26 To 35
a1 = 5
End Select

MY QUESTION IS: if I got a salary value of "58.3", is it(58.3) belong to
"case 56 to 100" ? That is, if [salary]=58.3 (instead of integer 58 or 59)
, can a1=1 ????

Yes Case will be 56 to 100

But your case statement should also allow for a Case Else as:

Select Case Me.txtSalary
Case 26 To 35
a1 = 5
Case 56 To 100
a1 = 1
Case Else
MsgBox "Too Much or Too Little", vbOKOnly, "Out of Bounds"
End Select

Notice how I incremented the Case instead of decrementing it. Ordering the
Case statement makes it more efficient.
--
Arvin Meyer, MCP, MVP
Microsoft Access
Free Access downloads:
http://www.datastrat.com
http://www.mvps.org/access
 
J

John Vinson

FOR EXAMPLE:

Select Case [salary]
Case 56 To 100
a1 = 1
Case 26 To 35
a1 = 5
End Select

MY QUESTION IS: if I got a salary value of "58.3", is it(58.3) belong to
"case 56 to 100" ? That is, if [salary]=58.3 (instead of integer 58 or 59)
, can a1=1 ????

Thanks!

Martin

58.3 is in fact a larger number than 56. So yes, a1 will be set to 1.

John W. Vinson[MVP]
 
D

Dirk Goldgar

Arvin Meyer said:
Martin said:
FOR EXAMPLE:

Select Case [salary]
Case 56 To 100
a1 = 1
Case 26 To 35
a1 = 5
End Select

MY QUESTION IS: if I got a salary value of "58.3", is it(58.3)
belong to "case 56 to 100" ? That is, if [salary]=58.3 (instead of
integer 58 or 59) , can a1=1 ????

Yes Case will be 56 to 100

But your case statement should also allow for a Case Else as:

Select Case Me.txtSalary
Case 26 To 35
a1 = 5
Case 56 To 100
a1 = 1
Case Else
MsgBox "Too Much or Too Little", vbOKOnly, "Out of Bounds"
End Select

Notice how I incremented the Case instead of decrementing it.
Ordering the Case statement makes it more efficient.

Does it, Arvin? I've always assumed that the cases are evaluated from
top to bottom, so for efficiency the case that is expected to be most
common should be first, regardless of the specific numeric values
involved. Am I mistaken?
 
A

Arvin Meyer

Does it, Arvin? I've always assumed that the cases are evaluated from
top to bottom, so for efficiency the case that is expected to be most
common should be first, regardless of the specific numeric values
involved. Am I mistaken?

Starting with the most expected will always be faster.

I believe that since binary starts with fewer bits and keeps adding them
until it finds the match, incrementing is faster than decrementing. That's
based more on what appears to be logical than any fact I have read.
--
Arvin Meyer, MCP, MVP
Microsoft Access
Free Access Downloads
http://www.datastrat.com
http://www.mvps.org/access
 
Top