Exit code on case else

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi, I have several select case statements and if none of these are met I want
the code to stop. I know I can use the case else statement but what is the
best method to use to stop code? I thought it would be with a goto label
statement but this seems to be only used with onerror? Sorry I'm new at all
this ;-)

Thanks in advance for any help.
Sue
 
hughess7 said:
Hi, I have several select case statements and if none of these are met I
want
the code to stop. I know I can use the case else statement but what is the
best method to use to stop code? I thought it would be with a goto label
statement but this seems to be only used with onerror? Sorry I'm new at
all
this ;-)

Thanks in advance for any help.
Sue

You can use Exit Sub or Exit Function but you could also use the Goto
MyLabel syntax - it doesn't need to be connected with OnError. This may be
an advantage in that you ensure that the code always exits at a particular
place so you can do any clearing up which might be necessary.
 
NEVER EVER USE A GOTO!!!!!!!!!!
When you say you want the code to stop, do you mean quit the application, go
into debug, or just exit the sub or function you are in?

The best way to do it is with the Case Else in your Select Case statement.
In case you are not aware, the way the Select Case works is that it executes
the code for the first case expression that is true and ignores all the rest.
Even if every case in the select is true, only the first case will execute.
As a side note, always keep this in mind when constructing Select Case
statements. So, if you put whatever code you want in the Case Else
statement, then if none of the cases is true, you can stop your code.

Select Case x
Case is = 1
'Do Stuff for one
Case is = 2
'Do Stuff for two
Case Else
MsgBox("I Quit"....)
Exit Sub
End Select
 
Thanks, I thought I had heard somewhere that the goto was bad coding. If I
just put exit sub as per your example does it matter that I have not cleared
down my variables though? eg set db = nothing

Sue
 
As far as memory variables, you don't need to worry about them. I would,
however, as a matter of good practice allways close any recordsets and set
thier variables to nothing. There are a couple of ways to do that, and it
will depend on the circumstances. My favorite (or favourite if you are not
US :)) is to use a Boolean variable to keep unwanted code from executing. for
example:

Sub MySub()
Dim blnNoGo
blnNoGo = False 'Not necessary as Boolean is initialized as False, but
good
'practice

A bunch of Code Here
Select Case x
Case is = 1
Stuff for 1
Case is = 99
Stuff for 99
Case Else
blnNoGo = True
End Select

If blnNoGo then
rst.close
set rst = nothing
Else
a bunch more code
Endif
End Sub

Or

Sub MySub()
A bunch of Code Here
Select Case x
Case is = 1
Stuff for 1
Case is = 99
Stuff for 99
Case Else
rst.close
Set rst = nothing
Exit Sub
End Select
a bunch more code
End Sub
:

Either way, depending on your style and the requirements of the sub. Main
Rule is KEEP IT SIMPLE! ie which ever is easiest for you are another to have
to modify or debug in the future.

Now, GoTo. In every programming language, there is a GoTo statement. It is
not in of itselft evil, it is that it has been used so badly, that it is
taught never to use it. In the old days of basic when there were line
numbers, it was used a lot. What developed was called "Spagetti Code". That
is, it was almost impossible to follow the logic. There also existed the
problem of jumping outside of a current sub or function and never coming
back. This creates all kinds of problems, stack overflows, etc. The GoTo is
like a gun. Gotos don't create bad code, Programmers create bad code. The
Goto, however has been pretty universally banned.

I will, on the rare ocassion, use a Goto. But, I have my own rules on when
and how. I only use it when otherwise, the codeing in the rest of the Sub or
Function would be difficult or have a bunch of nested If's. I never go
outside the sub. I never go up, that is back toward the top of the sub,
there are all kinds of looping and for next statements for that. It is only
to get out of a sub when it is the cleanest way to do it and it is alwasy to
a tag where clean up at the end of the sub occurs.
 
Thanks very much for your concise explanation, even including UK spelling
;-). My goto label does send it to the bottom of the code where clean up
takes place but I will change to Exit Sub (no rst's etc defined before this
stage). I do already have a nested if statement so I think this might be
cleaner in this instance.

Thanks again
Sue
 
hughess7 said:
Thanks very much for your concise explanation, even including UK
spelling ;-). My goto label does send it to the bottom of the code
where clean up takes place but I will change to Exit Sub (no rst's
etc defined before this stage). I do already have a nested if
statement so I think this might be cleaner in this instance.

In my opinion having two exit points is worse than the use of a GoTo. With
a little extra work you can usually avoid both.

(never say never)
 
Back
Top