Select Case

  • Thread starter Thread starter DS
  • Start date Start date
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
 
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.
 
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
 
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?
 
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>)
 
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
 
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]*"
 
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;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top