473,698 Members | 2,601 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Use of Goto Exit_Proc

From http://www.mvps.org/access/tencommandments.htm

9th item:

Thou shalt not use "SendKeys", "Smart Codes" or "GoTo" (unless the GoTo
be part of an OnError process) for these will lead you from the path of
righteousness.

What about also using it as a means of exiting a procedure?

I'm a firm believer that exit sub and exit function should not be
sprinkled about tany procedure. There should one, and only one exit point.

For example:

Function fSilly() as Boolean

'returns true if the remarks are silly

dim int1 as integer 'just to flesh out the proc

On Error Goto Err_Proc

int1 = forms!frmSIlly. txtSIllyIndicat or

if int1 = 0 then

'perform all kinds of stuff

elseif int1 = 1 then

GoTo Exit_Proc

end if

Exit_Proc:

'set database variables to nothing, etc

Exit_Sub

Err_Proc:

Select case err.number

case else

'display error message

goto exit_proc

end select

--
Tim http://www.ucs.mun.ca/~tmarshal/
^o<
/#) "Burp-beep, burp-beep, burp-beep?" - Quaker Jake
/^^ "What's UP, Dittoooooo?" - Ditto
Nov 13 '05
37 3242
On Mon, 21 Feb 2005 20:53:29 GMT, "David W. Fenton"
<dX********@bwa y.net.invalid> wrote:

....

I thought about your code module again, and realized that it could probably be
clarified by extracting a test for whether the database is open into its own
procedure, like the example below.

Of course, I'm a fan of using an explicit variable to hold a return value,
even if it means I have to use a Goto as a return, but using the function name
as an implicit variable, the goto could be replaced with Exit Function if you
wanted to.

== Proposed Code ==

Public Function dbLocal( _
Optional ysnInitialize As Boolean = True _
) As DAO.Database
On Error GoTo err_catch
Dim blnIsDbOpen As Boolean

blnIsDbOpen = IsDbOpen(dbCurr ent)

If ysnInitialize Then GoTo closeDB Then
If Not blnIsDbOpen Then Set dbCurrent = CurrentDb()
Else
If blnIsDbOpen Then dbCurrent.Close
Set dbCurrent = Nothing
End If

Fn_Return:
Set dbLocal = dbCurrent
Exit Function

err_catch:
MsgBox Err.Number & ": " & Err.Description , vbExclamation, _
"Error in Global Code.dbLocal()"
Resume Fn_Return

End Function

' Calling procedure is expected to handle any unexpected errors.
Public Function IsDbOpen(dbToCh eck As DAO.Database) As Boolean
Dim strNameTest As String
Dim blnResult As Boolean

blnResult = True ' Assume True unless we find out otherwise.

If dbToCheck Is Nothing Then
blnResult = False: Goto Fn_Return
End If

On Error Goto Err_Catch
strNameTest = dbToCheck.Name

Fn_Return:
IsDbOpen = blnResult

Err_Catch:
Select Case Err.Number
Case 3420 ' Object invalid or no longer set.
blnResult = False
Resume Fn_Return
Case Else
Err.Raise Err.Number, Err.Source, Err.Description ,
Err.HelpFile, Err.HelpContext
End Select

End Function
== Code from original post ==

Public Function dbLocal(Optiona l ysnInitialize As Boolean = True) As
DAO.Database ' 2003/02/08 DWF added comments to explain it to
myself! ' uses GoTos instead of If/Then because:
' error of dbCurrent not being Nothing but dbCurrent being closed
' would (3420) would then be jumping back into the middle of an
' If/Then statement
On Error GoTo errHandler
Dim strTest As String

If Not ysnInitialize Then GoTo closeDB

retryDB:
If dbCurrent Is Nothing Then
Set dbCurrent = CurrentDb()
End If
' now that we know the db variable is not Nothing,
' test if it's Open
strTest = dbCurrent.Name

exitRoutine:
Set dbLocal = dbCurrent
Exit Function

closeDB:
If Not (dbCurrent Is Nothing) Then
'dbCurrent.clos e
Set dbCurrent = Nothing
End If
GoTo exitRoutine

errHandler:
Select Case Err.Number
Case 3420 ' Object invalid or no longer set.
Set dbCurrent = Nothing
If ysnInitialize Then
Resume retryDB
Else
Resume closeDB
End If
Case Else
MsgBox Err.Number & ": " & Err.Description , vbExclamation, _
"Error in Global Code.dbLocal()"
Resume exitRoutine
End Select
End Function
Nov 13 '05 #31
"Terry Kreft" wrote
I've been coding for 13 years in VB and
VBA and can think of only one occasion
where I have used GoTo except as part
of an error handling routine.

The example you show does not require GoTo at all.

My personal advice is; if you feel the need to use goto:
1) Try calmimg down, you are obviously
overworked and possibly a bit
emotional.
2) Have a cold shower
3) Consider the ridicule of your peers when
they see what you have done. <G>


I was one of IBM's internal advocates for structured programming, back when
it was a "new thing" (a long time ago -- a little over 30 years ago, I was
invited to be part of a team to take a rotational assignment in Europe to
promote structured programming in IBM World Trade, but I opted to take an
assignment in San Francisco instead, and do not regret having done so) and
my view was, and is, this: When one "converts" from spaghetti code to
structured, it is useful to be strict with yourself for a period of time and
allow yourself no violations; then (as Steve said) _judicious_ violations
can make your code easier to read/follow, and that is the whole reason for
structured programming -- make it easier to read/follow and, thus, avoid
mistakes.

It is just as much a violation of structure to sprinkle Exit Sub/Function
statements through the code as it is to use Go To Exit_Proc. And, of course,
the Exit_Proc may contain other code besides the Exit Sub/Function (like the
"Finally" code of "Try / Catch / Finally" that the OOP advocates think is so
much better).

And, if I understood, the only case in which either Tim or Steve proposed
using Go To was _in conjunction with error handling_. If you do not, you are
very likely to find yourself including unnneccesary layers of conditions
just to avoid GoTos in case of error and clear need to "quit right now".
Unneccesary layers of conditions will make the code harder to read/follow
and be more conducive to the introduction of errors than a few GoTos.

Larry Linson
Microsoft Access MVP
Nov 13 '05 #32
On Thu, 24 Feb 2005 17:04:24 GMT, "Larry Linson" <bo*****@localh ost.not>
wrote:
"Terry Kreft" wrote
I've been coding for 13 years in VB and
VBA and can think of only one occasion
where I have used GoTo except as part
of an error handling routine.

The example you show does not require GoTo at all.

My personal advice is; if you feel the need to use goto:
1) Try calmimg down, you are obviously
overworked and possibly a bit
emotional.
2) Have a cold shower
3) Consider the ridicule of your peers when
they see what you have done. <G>


I was one of IBM's internal advocates for structured programming, back when
it was a "new thing" (a long time ago -- a little over 30 years ago, I was
invited to be part of a team to take a rotational assignment in Europe to
promote structured programming in IBM World Trade, but I opted to take an
assignment in San Francisco instead, and do not regret having done so) and
my view was, and is, this: When one "converts" from spaghetti code to
structured, it is useful to be strict with yourself for a period of time and
allow yourself no violations; then (as Steve said) _judicious_ violations
can make your code easier to read/follow, and that is the whole reason for
structured programming -- make it easier to read/follow and, thus, avoid
mistakes.

It is just as much a violation of structure to sprinkle Exit Sub/Function
statements through the code as it is to use Go To Exit_Proc. And, of course,
the Exit_Proc may contain other code besides the Exit Sub/Function (like the
"Finally" code of "Try / Catch / Finally" that the OOP advocates think is so
much better).

And, if I understood, the only case in which either Tim or Steve proposed
using Go To was _in conjunction with error handling_. If you do not, you are
very likely to find yourself including unnneccesary layers of conditions
just to avoid GoTos in case of error and clear need to "quit right now".
Unneccesary layers of conditions will make the code harder to read/follow
and be more conducive to the introduction of errors than a few GoTos.

Larry Linson
Microsoft Access MVP


Actually, I also often advocate using Goto to jump to a clean-up/exit block,
even when no error handling code exists in the procedure.
Nov 13 '05 #33
Steve Jorgensen wrote:
Actually, I also often advocate using Goto to jump to a clean-up/exit block, even when no error handling code exists in the procedure.


I think unconditional exclusion of GoTo's (space omitted) has served
it's purpose of eliminating the unorganized spaghetti code that
prevaled under BASIC and FORTRAN. GoTo was certainly good at making
code confusing and unstructured. That kind of coding is long gone (I
hope) so I won't malign any programmer who has a decent reason for
using it. I usually rewrite control structures to avoid GoTo since
it's nearly always possible to do that. In certain cases I guess I'd
even include not having the time to rewrite the control structure as a
valid reason for temporarily having a GoTo. The idea of using GoTo to
jump to a clean-up/exit block won't draw any criticism from me either.
Just allow me to decide how many times I need to duplicate clean-up
code before using it :-). You could even use a GoSub ... Return to run
the clean-up before exiting. Someone in this NG mentioned that it only
takes a few lines of code to allow a called Sub to access the variables
of the calling routine. I should have followed up on that.

James A. Fortune

People rationalize traffic laws. Police take them literally. Choose
wisely.

Nov 13 '05 #34
Steve Jorgensen <no****@nospam. nospam> wrote in
news:js******** *************** *********@4ax.c om:
If ysnInitialize Then GoTo closeDB Then
If Not blnIsDbOpen Then Set dbCurrent = CurrentDb()
Else
If blnIsDbOpen Then dbCurrent.Close
Set dbCurrent = Nothing
End If


The first line of your code block is nonsense.

--
David W. Fenton http://www.bway.net/~dfenton
dfenton at bway dot net http://www.bway.net/~dfassoc
Nov 13 '05 #35
Steve Jorgensen <no****@nospam. nospam> wrote in
news:7u******** *************** *********@4ax.c om:
Actually, I also often advocate using Goto to jump to a
clean-up/exit block, even when no error handling code exists in
the procedure.


A guard clause that is the first thing in a procedure doesn't need
to jump to the clean-up/exit block -- Exit Sub/Function is fine
there, since if the guard clause is the first thing, there are no
structures to clean up.

--
David W. Fenton http://www.bway.net/~dfenton
dfenton at bway dot net http://www.bway.net/~dfassoc
Nov 13 '05 #36
On Thu, 24 Feb 2005 19:50:35 GMT, "David W. Fenton"
<dX********@bwa y.net.invalid> wrote:
Steve Jorgensen <no****@nospam. nospam> wrote in
news:js******* *************** **********@4ax. com:
If ysnInitialize Then GoTo closeDB Then
If Not blnIsDbOpen Then Set dbCurrent = CurrentDb()
Else
If blnIsDbOpen Then dbCurrent.Close
Set dbCurrent = Nothing
End If


The first line of your code block is nonsense.


Yes, it's a typo. it should read just "If ysnInitialize Then".
Nov 13 '05 #37
On Thu, 24 Feb 2005 19:53:14 GMT, "David W. Fenton"
<dX********@bwa y.net.invalid> wrote:
Steve Jorgensen <no****@nospam. nospam> wrote in
news:7u******* *************** **********@4ax. com:
Actually, I also often advocate using Goto to jump to a
clean-up/exit block, even when no error handling code exists in
the procedure.


A guard clause that is the first thing in a procedure doesn't need
to jump to the clean-up/exit block -- Exit Sub/Function is fine
there, since if the guard clause is the first thing, there are no
structures to clean up.


Guard clauses are not always at the absolute top. Sometimes, they are
checking initial state conditions that cannot be determined prior to
initializing the objects necessary to check them. These objects must be torn
down. I do try to avoid the issue, but the cure for that is sometimes worse
than the disease.
Nov 13 '05 #38

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

30
4266
by: Hayri ERDENER | last post by:
hi, what is the equivalent of C languages' goto statement in python? best regards
3
2982
by: HDI | last post by:
Hi, I've got following code: For each .... on error goto errhand errhand:
0
8683
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8611
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9170
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8904
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8876
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6531
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4372
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2341
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.