if then statement not working in select case?

vblearner
09-22-2003, 02:11 PM
hi,
why is the world does this code work?


Private Sub Command1_Click()
da = "fdfsfbi:dads"
Select Case da
Case Is = InStr(da, ":")
MsgBox "yes"
End Select
End Sub


when this code works


da = "fdfsfbi:dads"

If InStr(da, ":") = 1 Then
MsgBox "yes"
End If


thanks

EDITED AFTER Trivium POSTED: thanks, i think ill just stick with if then... in that case

Trivium
09-22-2003, 02:21 PM
Well if you are dead set on using a Select Case statement, even though your second bit of code works fine, I'd recommend doing it like so:


Private Sub Command1_Click()
da = "fdfsfbi:dads"

Select Case InStr(da, ":")
Case 0
MsgBox "No"
Case 1
MsgBox "Yes"
End Select
End Sub


EDIT: (As to why it doesn't work.)

Your select case being "da", the returned value of instr is 1 or 0... So since da would never equal that and the instr value would always be 0 if it did, that wouldn't ever work...

00100b
09-22-2003, 02:47 PM
Reason Why:

da is equal to "fdfsfbi:dads"
InStr(da, ":") will return a number identifying the starting position of the string to find within another string or 0 if it doesn't exist.
So:

Select Case da
Case Is = InStr(da, ":")

Is checking to see if "fdfsfbi:dads" = 8
Which it doesn't.

Trivium
09-22-2003, 03:04 PM
That's right, instr doesn't return a true/false value, my bad... *Slaps himself a few times with a Unix Manual* Right then... Thanks 00100b

You could do


Case 1 to Len(ba)
MsgBox("Yes")


instead of the other one I had up there...

blindwig
09-22-2003, 03:17 PM
That's right, instr doesn't return a true/false value, my bad... *Slaps himself a few times with a Unix Manual* Right then... Thanks 00100b

You could do


Case 1 to Len(ba)
MsgBox("Yes")


instead of the other one I had up there...
simplify, man, simplify!

Select Case InStr(da, ":")
Case 0
MsgBox "No"
Case else
MsgBox "Yes"
End Select

Trivium
09-22-2003, 03:23 PM
Hey now, some of us do like being precise! I've gotten a few to many random wierd errors that do crazy things not to try and limit things as much as possible. *Grins* That, and it's a nice excuse for not just using case else... Because I didn't think of it.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum