Select Case

D

DS

If two cases have the same code can you do this?

Select Case Me.TxtTest
Case 1 and Case 2
MsgBox "1 and 2"
Case 3
MsgBox "3"
Case 4
MsgBox "4"
Case Else
MsgBox "No Msg"
End Select

If not how doy you cut down on repeating code if you have this situation?
Thanks
DS
 
R

Rick Brandt

DS said:
If two cases have the same code can you do this?

Select Case Me.TxtTest
Case 1 and Case 2
MsgBox "1 and 2"
Case 3
MsgBox "3"
Case 4
MsgBox "4"
Case Else
MsgBox "No Msg"
End Select

If not how doy you cut down on repeating code if you have this
situation? Thanks
DS


Yes. It would look like this for text...

Select Case Me.TxtTest
Case "This", "That", "Other"

....and like this for numeric...

Select Case Me.TxtTest
Case 1-5, 8, 12

The '-' provides for a range of values.
 
D

Douglas J. Steele

Rick Brandt said:
Yes. It would look like this for text...

Select Case Me.TxtTest
Case "This", "That", "Other"

...and like this for numeric...

Select Case Me.TxtTest
Case 1-5, 8, 12

The '-' provides for a range of values.

Actually, you need to use "to", not "-":

Case 1 to 5, 8, 12

When you use -, VBA is going to do arithmetic and treat it as

Case -4, 8, 12
 
R

Rick Brandt

Douglas said:
Actually, you need to use "to", not "-":

Case 1 to 5, 8, 12

When you use -, VBA is going to do arithmetic and treat it as

Case -4, 8, 12

Ah, yes. Thanks for the correction.

Isn't there someplace in Access/VBA where a range is represented as I had it?
Perhaps inside sqaure brackets like [1-5]? The syntax certainly "feels"
familiar to me. In validation rules perhaps?
 
D

Douglas J. Steele

Rick Brandt said:
Isn't there someplace in Access/VBA where a range is represented as I had
it? Perhaps inside sqaure brackets like [1-5]? The syntax certainly
"feels" familiar to me. In validation rules perhaps?

I'm not sure. But I'll admit that I thought 1-5 was the correct syntax as
well, because I got bit by that a couple of days ago. (That's why I was able
to jump on your post! <g>)
 
D

DS

Rick said:
Yes. It would look like this for text...

Select Case Me.TxtTest
Case "This", "That", "Other"

...and like this for numeric...

Select Case Me.TxtTest
Case 1-5, 8, 12

The '-' provides for a range of values.
Great! Rick, This saves some typing!
Thank You
DS
 
M

Marshall Barton

Rick said:
Douglas said:
Actually, you need to use "to", not "-":

Case 1 to 5, 8, 12

When you use -, VBA is going to do arithmetic and treat it as

Case -4, 8, 12

Ah, yes. Thanks for the correction.

Isn't there someplace in Access/VBA where a range is represented as I had it?
Perhaps inside sqaure brackets like [1-5]? The syntax certainly "feels"
familiar to me. In validation rules perhaps?


The only place that comes to mind is in a Like wildcard:

Like "*[1-5]*"
 
R

Rick Brandt

Marshall said:
Rick said:
Douglas said:
Yes. It would look like this for text...

Select Case Me.TxtTest
Case "This", "That", "Other"

...and like this for numeric...

Select Case Me.TxtTest
Case 1-5, 8, 12

The '-' provides for a range of values.

Actually, you need to use "to", not "-":

Case 1 to 5, 8, 12

When you use -, VBA is going to do arithmetic and treat it as

Case -4, 8, 12

Ah, yes. Thanks for the correction.

Isn't there someplace in Access/VBA where a range is represented as
I had it? Perhaps inside sqaure brackets like [1-5]? The syntax
certainly "feels" familiar to me. In validation rules perhaps?


The only place that comes to mind is in a Like wildcard:

Like "*[1-5]*"

Okay, if that's valid SQL then it probably works in a validation rule as well.

Thanks;
 
Top