Ending a Case Early

windowsgm picture windowsgm · May 23, 2012 · Viewed 22.6k times · Source

So I have something like the following in Vb6;

Select case Case

case "Case0"
...

case "Case1"
  if Condition Then
     Exit Select
  End If
  *Perform action*

case "Case2"
...

End Select

But for some reason my Exit Select throws the error Expected: Do or For or Sub or Function or Property. I know, not pretty. Should I be using something else? I could just use if statements and not exit the case early, but this would require duplicate code, which I want to avoid. Any help would be really appreciated.

Update

Tried changing Exit Select to End Select and got the error End Select without Select Case. It is definitely within a Select Case and an End Select.

Answer

Deanna picture Deanna · May 23, 2012

VB doesn't have a facility to exit a Select block. Instead, you'll need to make the contents conditional, possibly inverting your Exit Select conditional.

Select case Case 

case "Case0" 
... 

case "Case1" 
  If Not Condition Then 
    *Perform action* 
  End If 

case "Case2" 
... 

End Select 

Which will have exactly the same end result.