Select Case question

rbinder

rbinder
Local time
Tomorrow, 07:14
Joined
Aug 13, 2005
Messages
25
Greetings all,

done a quick search and came up empty handed

what is the best way to use 'or' in a Case statement. I am using

Case "14" or "15"

however it only seems to recognise the first number not the second. Sometimes it does not recognise either number.

~rbinswe
 
I didn't have to much trouble getting it to work mate.
I set up a unbound text box to test and the following code activated on the On Click Event.

Code:
Private Sub Command3_Click()
Dim stText As String
stText = Me.txtName

Select Case stText
    Case Is = "1" Or "3"
        MsgBox "Its 1", vbExclamation
    Case Is = "2"
        MsgBox "Its 2", vbInformation
    End Select


End Sub

Again, if you enter 1 or 3 in the text box[txtName] and click the "Command3" button you get the message box "Its 1" pop up.

Hope this helps. :)
 
Remove the quotes around the numbers
 
Had a though about this one, first I saw the quotes and just assumed that you were working with string values.
But you could mod this to work with integer if you wanted.

But perhaps you could catch the "X" or "Y" earlier on.

Code:
Private Sub Command3_Click()
Dim stText As String
Dim stSet As String

stText = Me.txtName


If stText = "Pig" Or stText = "Dog" Then
    stSet = "First"
Else
    stSet = stText
End If

Select Case stSet
    Case Is = "First"
        MsgBox "Its " & stText, vbExclamation
    Case Is = "Cat"
        MsgBox "Its " & stText, vbInformation
    Case Else
        MsgBox "Not on the list", vbInformation
    End Select


End Sub

This way if you know that there is only going to be one or two "Or"s you can catch and validate before it's passed to the Select Case.

Just a thought, hope it helps. :)
 

Users who are viewing this thread

Back
Top Bottom