473,698 Members | 2,643 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
Steve Jorgensen <no****@nospam. nospam> wrote:
I used to have separate exit function/sub along with whatever closing of
variables. I began using GOTo as a means to end the procedure
prematurel y if certain conditions are not meant.


I do exactly the same thing myself.


As do I - it's called a guard clause. The code presented, however, when
restructured in a clear way, no longer has a need for a guard clause employing
Goto. My example (for example) does include a guard clause, but it uses Exit
Sub.


Agreed that there are some interesting ideas which I will certainly consider for the
future.

Tony
--
Tony Toews, Microsoft Access MVP
Please respond only in the newsgroups so that others can
read the entire thread of messages.
Microsoft Access Links, Hints, Tips & Accounting Systems at
http://www.granite.ab.ca/accsmstr.htm
Nov 13 '05 #21
Steve Jorgensen <no****@nospam. nospam> wrote:
I used to have separate exit function/sub along with whatever closing of
variables. I began using GOTo as a means to end the procedure
prematurel y if certain conditions are not meant.


I do exactly the same thing myself.


As do I - it's called a guard clause. The code presented, however, when
restructured in a clear way, no longer has a need for a guard clause employing
Goto. My example (for example) does include a guard clause, but it uses Exit
Sub.


Agreed that there are some interesting ideas which I will certainly consider for the
future.

Tony
--
Tony Toews, Microsoft Access MVP
Please respond only in the newsgroups so that others can
read the entire thread of messages.
Microsoft Access Links, Hints, Tips & Accounting Systems at
http://www.granite.ab.ca/accsmstr.htm
Nov 13 '05 #22
Steve Jorgensen <no****@nospam. nospam> wrote in
news:63******** *************** *********@4ax.c om:
On Mon, 21 Feb 2005 20:53:29 GMT, "David W. Fenton"
<dX********@bw ay.net.invalid> wrote:

...

(Putting stuff after the Sig line turns out to mess up Forte Agent
because it does not quote the text in the reply).
xNews quotes nothing after the sig marker unless you select the
whole message before initiating the followup. I always assume that
people are using tools that are just as good as mine. ;)
IMO, your code is about as good as it can get. The only things I
can see that would get rid of the Goto involve adding duplication,
and adding duplication is way worse than employing a Goto.


You know, looking at it again, there aren't as many GoTo's as I
thought -- I think I was consider the Resume statements in the error
handler as GoTo's, which is kind of what they are, actually (a GoTo
that will work during an error condition). They have the same bad
effect as GoTo's, in that they multiply the number of possible paths
through the code.

--
David W. Fenton http://www.bway.net/~dfenton
dfenton at bway dot net http://www.bway.net/~dfassoc
Nov 13 '05 #23
Steve Jorgensen <no****@nospam. nospam> wrote in
news:63******** *************** *********@4ax.c om:
On Mon, 21 Feb 2005 20:53:29 GMT, "David W. Fenton"
<dX********@bw ay.net.invalid> wrote:

...

(Putting stuff after the Sig line turns out to mess up Forte Agent
because it does not quote the text in the reply).
xNews quotes nothing after the sig marker unless you select the
whole message before initiating the followup. I always assume that
people are using tools that are just as good as mine. ;)
IMO, your code is about as good as it can get. The only things I
can see that would get rid of the Goto involve adding duplication,
and adding duplication is way worse than employing a Goto.


You know, looking at it again, there aren't as many GoTo's as I
thought -- I think I was consider the Resume statements in the error
handler as GoTo's, which is kind of what they are, actually (a GoTo
that will work during an error condition). They have the same bad
effect as GoTo's, in that they multiply the number of possible paths
through the code.

--
David W. Fenton http://www.bway.net/~dfenton
dfenton at bway dot net http://www.bway.net/~dfassoc
Nov 13 '05 #24
David W. Fenton wrote:
xNews quotes nothing after the sig marker unless you select the
whole message before initiating the followup. I always assume that
people are using tools that are just as good as mine. ;)


ISTR Agent does that as well.

--
This sig left intentionally blank
Nov 13 '05 #25
David,
Maybe I'm missing something but isn't your code functionally the same as

If ysnInitialize Then
If dbCurrent Is Nothing Then
Set dbCurrent = CurrentDb()
End If
Set dbLocal = dbCurrent
Else
Set dbCurrent = Nothing
End If
--
Terry Kreft
MVP Microsoft Access
"David W. Fenton" <dX********@bwa y.net.invalid> wrote in message
news:Xn******** *************** ***********@24. 168.128.86...
Steve Jorgensen <no****@nospam. nospam> wrote in
news:k7******** *************** *********@4ax.c om:
On Mon, 21 Feb 2005 12:43:18 -0330, Tim Marshall
<TI****@Purple PandaChasers.Mo ertherium> wrote:
Steve Jorgensen wrote:

The use of Goto has lead to such awful spaghetti in the past
that common wisdom has been to avoid using it - period.

Thanks teve - could I ask you to have a look at my response to
Terry Kreft in this thread and offer an opinion? I think, though,
that your check list answered my question... 8)


I could, but it will probably come down to 2 smart people who
simply disagree. The only thing I might add is to say that I make
my choices pragmatically, based on what will be easiest to read
and maintain. I use certain things like the use of GoTo as red
flags to tell me I might be on the wrong track, but with the
emphasis on "might".


I would say that, discounting "On Error GoTo ErrHandler", if you
have more than one GoTo in your subroutine, you should consider
restructuring it.

After my .sig I've appended a piece of code I use in all my
applications. Can it be restructured to omit the GoTos into the
sub-subroutines? I don't think so, but maybe I'm missing something.

--
David W. Fenton http://www.bway.net/~dfenton
dfenton at bway dot net http://www.bway.net/~dfassoc

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 #26
"Terry Kreft" <te*********@mp s.co.uk> wrote in
news:vZ******** ************@ka roo.co.uk:
Maybe I'm missing something but isn't your code functionally the
same as

If ysnInitialize Then
If dbCurrent Is Nothing Then
Set dbCurrent = CurrentDb()
End If
Set dbLocal = dbCurrent
Else
Set dbCurrent = Nothing
End If


No, because dbCurrent can be Nothing while the db it points to is
closed. I don't know how it happens with the CurrentDB() (perhaps
the wizard problem?), but it happened to me, so I wrote the code to
avoid it. If you can come up with a simpler way to handle that
condition, then I'm all for it.

This code did, in fact, start out very simple, not unlike what you
suggest, and it only got complicated after I was using it for a long
enough time to encounter a number of errors that weren't obvious to
me at first.

--
David W. Fenton http://www.bway.net/~dfenton
dfenton at bway dot net http://www.bway.net/~dfassoc
Nov 13 '05 #27
Arno R wrote:
Hi Tim,

You don't want suggestions for code-change, so skip this message... ;-)
Not at all, I appeciate any input or suggestions...
I validate field-input on the before_update event of the field,
I must say I do this too, though if my field validation involves
changing the value, I go with the after update event since I have found
a lot of difficulty manipulating the .text value.
but I also need
record-validation at the form-level.
Nowadays I *always* put all my form-level validation (fields that must have a value) in a
separate function.
If you would do the same then IMO the readability of your code would improve much.
Good idea. I suspect I sometimes pack too much into my procedures.
Another advantage: This function can be used elsewhere on your form !

Thanks I really appreciate your time, Arno.
--
Tim http://www.ucs.mun.ca/~tmarshal/
^o<
/#) "Burp-beep, burp-beep, burp-beep?" - Quaker Jake
/^^ "What's UP, Dittoooooo?" - Ditto
Nov 13 '05 #28
Bri wrote:
Tim,

Your code does allow for a lot of confusion and maintenance problems
down the line. Also, it can frustrate your users as they fix one problem
and try again only to get yet another new error message. Here is
something I've used that might help.


Thanks. I think perhaps the main thing to d is break up the code. I do
this sometimes and othertimes I don't. I should do it mor often.

--
Tim http://www.ucs.mun.ca/~tmarshal/
^o<
/#) "Burp-beep, burp-beep, burp-beep?" - Quaker Jake
/^^ "What's UP, Dittoooooo?" - Ditto
Nov 13 '05 #29
David W. Fenton wrote:
Don't enable the OK button until all the conditions are met. You can
then use the AfterUpdate event of all the controls that have
required values to call a function that checks all the controls
involved and then enables the OK button.


<snip>

Thanks very much David. I've done this before and should try to
remember it. I think the message I'm getting from all and sundry 8) is
to a combination of breaking up the code into sub routines and/or field
level validation.

I appreciate your time, thanks. 8)
--
Tim http://www.ucs.mun.ca/~tmarshal/
^o<
/#) "Burp-beep, burp-beep, burp-beep?" - Quaker Jake
/^^ "What's UP, Dittoooooo?" - Ditto
Nov 13 '05 #30

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
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...
0
7741
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5867
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
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...
1
3052
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
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.