473,698 Members | 2,942 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 12:38:27 -0330, Tim Marshall
<TI****@PurpleP andaChasers.Moe rtherium> wrote:

.....
Seriously though, how would you do it?


The following is not tested, but it should give you the idea...

Private Sub btnOk_Click()

'This will run an insert/update for the main TBL_ACTION and a
'series of INSERTS for TBL_ACTION_TERR AIN if this is a new
'record. If an old record, a delete of existing
'TBL_ACTION_TER RAIN will occur first

Dim lngPk As Long
Dim strInvalidDataM sg As String
Dim ctlInvalidEntry As Access.Control

On Error GoTo Err_Proc

If Not IsDataValid(str InvalidDataMsg, ctlInvalidEntry ) Then
Exit Sub
End If

lngPk = GetPrimaryKey()
fRestrictions lngPk
Forms!frmsetup. subExplain.Form .Requery
ResyncSubExplai nBookmark lngPk
DoCmd.Close acForm, "frmsetupAction sadd", acSaveNo

Exit_Proc:
Exit Sub

Err_Proc:
Select Case Err.Number
Case 3022 'index - in this case, a repaeated name
MsgBox "You already have an action with the " & _
"abbreviati on """ & Me.txtAbrev & """", _
vbExclamation
Me.txtName.SetF ocus

Case Else
MsgBox "Error " & Err.Number & " " & Err.Description , _
vbCritical, _
"frmSetUpAction sAdd btnOk_Click", _
Err.HelpFile, Err.HelpContext

End Select

Resume Exit_Proc

End Sub

Private Function IsBlankEntry(va rEntry As Variant) As Boolean
IsBlankEntry = (Len(Trim(varEn try & ""))=0)
End Function

Private Function IsDataValid( _
ByRef strInvalidDataM sg As String, _
ByRef ctlInvalidEntry As Access.Control _
) As Boolean
Dim blnResultOk As Boolean

Dim intC As Integer
Dim lngPk As Long

If IsBlankEntry(Me .txtAbrev) Then
blnResultOk = False
strInvalidDataM sg = "Enter a unique (for this campaign" & _
") abbreviation/code for this action!"
Set ctlInvalidEntry = Me.txtAbrev

ElseIf IsBlankEntry(Me .txtName) Then
blnResultOk = False
strInvalidDataM sg = "Enter a name for this action!"
Set ctlInvalidEntry = Me.txtName

ElseIf Nz(Me.fraType, 0) = 0 Then
blnResultOk = False
strInvalidDataM sg = "You haven't chosen an action type."
Set ctlInvalidEntry = Me.fraType

ElseIf fCheckDest = False Then
blnResultOk = False 'a separate procedure

ElseIf fSelectTerrain = False
blnResultOk = False

ElseIf Me.fraType = 11 Or Me.fraType = 12 Then

lngPk = 0
For intC = 0 To Me.lstDestinati on.ListCount - 1
If Me.lstDestinati on.Selected(int C) Then
lngPk = Me.lstDestinati on.Column(0, intC)
Exit For
End If
Next intC

If lngPk = 0 Then 'nothing chosen
blnResultOk = False
strInvalidDataM sg = "Choose a terrain type ""effect""" & _
" for the crossing/obstacle!"
Set ctlInvalidEntry = Me.lstDestinati on
End If

ElseIf Me.txtValue.Ena bled = True And _
IsNull(Me.txtVa lue) _
Then
strInvalidDataM sg = "Enter a value!"
Set ctlInvalidEntry = Me.txtValue

ElseIf Me.txtMissions. Enabled = True And _
IsNull(Me.txtMi ssions) _
Then
strInvalidDataM sg = "Enter number of missions!"
Set ctlInvalidEntry = Me.txtMissions. SetFocus

End If

IsDataValid = blnResultOk
End Function

Private Function GetPrimaryKey() As Long
Dim lngPk As Long

Select Case Forms!frmsetup. subExplain.Form .txtAddEdit
Case "add"
strSql = fInsert Else strSql = fUpdate
Case "edit"
lngPk = Forms!frmsetup. subExplain.Form .ACT_PK
Case Else
lngPk = DMax("ACT_PK", "TBL_ACTION S", _
"ACT_GAM_FK = " & Forms!frmsetup. txtPK)
End Select

GetPrimaryKey = lngPk
End Function

Private Sub ResyncSubExplai nBookmark(lngPk As Long)
Dim rst As DAO.Recordset

Set rst = Forms!frmsetup. subExplain.Form .RecordsetClone
With rst
.MoveFirst
.FindFirst "ACT_PK = " & lngPk
Forms!frmsetup. subExplain.Form .Bookmark = .Bookmark
.MoveFirst
' Don't .Close it if you didn't .Open it
'.Close < Don't do this.
End With

Set rst = Nothing
End Sub

Nov 13 '05 #11
Oops - that's missing a piece...

Should be ...

If Not IsDataValid(str InvalidDataMsg, ctlInvalidEntry ) Then
If Len(strInvalidD ataMsg) > 0 then
Msgbox strInvalidDataM sg
End If
If Not ctlInvalidEntry Is Nothing Then
ctlInvalidEntry .SetFocus
End If

Exit Sub

End If

On Mon, 21 Feb 2005 12:07:14 -0800, Steve Jorgensen <no****@nospam. nospam>
wrote:
On Mon, 21 Feb 2005 12:38:27 -0330, Tim Marshall
<TI****@Purple PandaChasers.Mo ertherium> wrote:

....
Seriously though, how would you do it?


The following is not tested, but it should give you the idea...

Private Sub btnOk_Click()

'This will run an insert/update for the main TBL_ACTION and a
'series of INSERTS for TBL_ACTION_TERR AIN if this is a new
'record. If an old record, a delete of existing
'TBL_ACTION_TE RRAIN will occur first

Dim lngPk As Long
Dim strInvalidDataM sg As String
Dim ctlInvalidEntry As Access.Control

On Error GoTo Err_Proc

If Not IsDataValid(str InvalidDataMsg, ctlInvalidEntry ) Then
Exit Sub
End If

lngPk = GetPrimaryKey()
fRestrictions lngPk
Forms!frmsetup. subExplain.Form .Requery
ResyncSubExplai nBookmark lngPk
DoCmd.Close acForm, "frmsetupAction sadd", acSaveNo

Exit_Proc:
Exit Sub

Err_Proc:
Select Case Err.Number
Case 3022 'index - in this case, a repaeated name
MsgBox "You already have an action with the " & _
"abbreviati on """ & Me.txtAbrev & """", _
vbExclamation
Me.txtName.SetF ocus

Case Else
MsgBox "Error " & Err.Number & " " & Err.Description , _
vbCritical, _
"frmSetUpAction sAdd btnOk_Click", _
Err.HelpFile, Err.HelpContext

End Select

Resume Exit_Proc

End Sub

Private Function IsBlankEntry(va rEntry As Variant) As Boolean
IsBlankEntry = (Len(Trim(varEn try & ""))=0)
End Function

Private Function IsDataValid( _
ByRef strInvalidDataM sg As String, _
ByRef ctlInvalidEntry As Access.Control _
) As Boolean
Dim blnResultOk As Boolean

Dim intC As Integer
Dim lngPk As Long

If IsBlankEntry(Me .txtAbrev) Then
blnResultOk = False
strInvalidDataM sg = "Enter a unique (for this campaign" & _
") abbreviation/code for this action!"
Set ctlInvalidEntry = Me.txtAbrev

ElseIf IsBlankEntry(Me .txtName) Then
blnResultOk = False
strInvalidDataM sg = "Enter a name for this action!"
Set ctlInvalidEntry = Me.txtName

ElseIf Nz(Me.fraType, 0) = 0 Then
blnResultOk = False
strInvalidDataM sg = "You haven't chosen an action type."
Set ctlInvalidEntry = Me.fraType

ElseIf fCheckDest = False Then
blnResultOk = False 'a separate procedure

ElseIf fSelectTerrain = False
blnResultOk = False

ElseIf Me.fraType = 11 Or Me.fraType = 12 Then

lngPk = 0
For intC = 0 To Me.lstDestinati on.ListCount - 1
If Me.lstDestinati on.Selected(int C) Then
lngPk = Me.lstDestinati on.Column(0, intC)
Exit For
End If
Next intC

If lngPk = 0 Then 'nothing chosen
blnResultOk = False
strInvalidDataM sg = "Choose a terrain type ""effect""" & _
" for the crossing/obstacle!"
Set ctlInvalidEntry = Me.lstDestinati on
End If

ElseIf Me.txtValue.Ena bled = True And _
IsNull(Me.txtVa lue) _
Then
strInvalidDataM sg = "Enter a value!"
Set ctlInvalidEntry = Me.txtValue

ElseIf Me.txtMissions. Enabled = True And _
IsNull(Me.txtMi ssions) _
Then
strInvalidDataM sg = "Enter number of missions!"
Set ctlInvalidEntry = Me.txtMissions. SetFocus

End If

IsDataValid = blnResultOk
End Function

Private Function GetPrimaryKey() As Long
Dim lngPk As Long

Select Case Forms!frmsetup. subExplain.Form .txtAddEdit
Case "add"
strSql = fInsert Else strSql = fUpdate
Case "edit"
lngPk = Forms!frmsetup. subExplain.Form .ACT_PK
Case Else
lngPk = DMax("ACT_PK", "TBL_ACTION S", _
"ACT_GAM_FK = " & Forms!frmsetup. txtPK)
End Select

GetPrimaryKey = lngPk
End Function

Private Sub ResyncSubExplai nBookmark(lngPk As Long)
Dim rst As DAO.Recordset

Set rst = Forms!frmsetup. subExplain.Form .RecordsetClone
With rst
.MoveFirst
.FindFirst "ACT_PK = " & lngPk
Forms!frmsetup. subExplain.Form .Bookmark = .Bookmark
.MoveFirst
' Don't .Close it if you didn't .Open it
'.Close < Don't do this.
End With

Set rst = Nothing
End Sub


Nov 13 '05 #12
Tim Marshall <TI****@PurpleP andaChasers.Moe rtherium> wrote in
news:cv******** **@coranto.ucs. mun.ca:
Here's a "real life" proc. It's the OK button for a form that
creates a new record. The table for the insert action has plenty
and plenty of database level constraints, plus there are some
other constarints I felt could best be handled at the form level.


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.

It's probably a good idea in that case to use some kind of visual
indicator to show which fields are required, and, perhaps some kind
of "help" text that reads "all required fields (outlined in red)
must be completed to save the record."

This puts the validation code in the controls where the values are
edited, which is where I prefer for it to be, and informs the user
of invalid values as soon as they enter them, rather than waiting to
the end. Your code could result in a user clicking OK repeatedly,
each time getting a different error message, whereas my structure
tells them what to do *before* they click the OK button.

It also means the code behind the OK button doesn't need to do much
at all, and needs no GoTo's.

--
David W. Fenton http://www.bway.net/~dfenton
dfenton at bway dot net http://www.bway.net/~dfassoc
Nov 13 '05 #13
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 #14
Hi Tim,

You don't want suggestions for code-change, so skip this message... ;-)

I validate field-input on the before_update event of the field, 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.
Another advantage: This function can be used elsewhere on your form !
e.g. in the Before_Update event, or when a user clicks a print-button (situations where
the record is saved, or must be saved)

Private Function RecordValidatio nFailed() as Boolean
Dim strmsg as string
Dim fError as boolean
strmsg = "This record fails validation... Please fill in:" & VbNewLine
'Now check all your fields
If IsNull(Field1) Then
strmsg=strmsg & " - Field 1" & vbNewLine
fError=True
end if
If IsNull(Field2) Then
strmsg=strmsg & " - Field 2" & vbNewLine
fError=True
end if
If IsNull(Field3) Then
strmsg=strmsg & " - Field 3" & vbNewLine
fError=True
end if
.. . . . . . .'and so on
If fError Then
Msgbox strmsg, VbExclamation, "Validation "
RecordValidatio nFailed=True
end if
End function

As you see this function 'does' basically the same as your code, but this function can be
used now in a generic way for this form.
A simular function can be used (modified for the fields) on *all* your other input-forms.

So now you could use:
If not RecordValidatio nFailed() Then
'Do your thing here
Else
Screen.Previous Control.setfocu s
End if
--
Hope this helps
Arno R

"Tim Marshall" <TI****@PurpleP andaChasers.Moe rtherium> schreef in bericht
news:cv******** **@coranto.ucs. mun.ca...
Tim Marshall wrote:
Sorry for the change of topic, but I think I'm having a catharsis of sorts here...

Terry Kreft wrote:
3) Consider the ridicule of your peers when they see what you have done.


OK, rather than ask people to suggest code changes, perhaps Terry or someone else can
tell me if the following is OK. Personally, I don't see a problem with the goto
exit_proc approach... but I must avoid ridicule!!! 8)

booProceed and strProceed are new variables here.

Private Sub btnOk_Click()

'This will run an insert/update for the main TBL_ACTION and a series of INSERTS for
'TBL_ACTION_TER RAIN if this is a new record. If an old record, a delete of
'existing TBL_ACTION_TERR AIN will occur first

Dim strSql As String
Dim intC As Integer
Dim lngPk As Long
Dim rst As DAO.Recordset

Dim booProceed As Boolean
Dim strProceed As String

On Error GoTo Err_Proc

booProceed = True 'if any of the following make it false, procedure will not go
ahead
strProceed = ""

'ensure abbreviation and name is chosen

If Nz(Me.txtAbrev, "") = "" Then

strProceed = "Enter a unique (for this campaign) abbreviation/code for this
action!"

Me.txtAbrev.Set Focus

booProceed = False

End If

'ensure a name is selected for this action

If Trim(Nz(Me.txtN ame, "")) = "" Then

strProceed = strProceed & IIf(strProceed <> "", vbCrLf & vbCrLf, "") & "Enter a
name for this action!"

Me.txtName.SetF ocus

booProceed = False

End If

'make sure an action type is selected

If IsNull(Me.fraTy pe) Or Me.fraType = 0 Then

strProceed = strProceed & IIf(strProceed <> "", vbCrLf & vbCrLf, "") & "You
haven't chosen an action type."

Me.fraType.SetF ocus

booProceed = False

End If

'next verify that if 11 or 12 that one or all destination list box item is selected

If fCheckDest = False Then booProceed = False

If fSelectTerrain = False Then booProceed = False

'next make sure that only one value is selected

If Me.fraType = 11 Or Me.fraType = 12 Then

lngPk = 0

For intC = 0 To Me.lstDestinati on.ListCount - 1

If Me.lstDestinati on.Selected(int C) Then

lngPk = Me.lstDestinati on.Column(0, intC)

Exit For

End If

Next intC

If lngPk = 0 Then 'nothing chosen

strProceed = strProceed & IIf(strProceed <> "", vbCrLf & vbCrLf, "") &
"Choose a terrain type ""effect"" for the crossing/obstacle!"

Me.lstDestinati on.SetFocus

booProceed = False

End If

End If

'is there a

'verify that there is a value in txtValue. if it is enabled, there needs to be a
value

If Me.txtValue.Ena bled = True Then

If IsNull(Me.txtVa lue) Then

strProceed = strProceed & IIf(strProceed <> "", vbCrLf & vbCrLf, "") &
"Enter a value!"

Me.txtValue.Set Focus

booProceed = False

End If

End If

'missions

If Me.txtMissions. Enabled = True Then

If IsNull(Me.txtMi ssions) Then

strProceed = strProceed & IIf(strProceed <> "", vbCrLf & vbCrLf, "") &
"Enter number of missions!"

Me.txtMissions. SetFocus

booProceed = False

End If

End If

If booProceed = False Then

'display error messsage if strProceed is not empty string, otherwise, the
'separate routines will have displayed a message

If strProceed <> "" Then MsgBox strProceed, vbExclamation, "Incomplete
Information for This Action"

Else

'do the action in tbl_action first and get the pk

If Forms!frmsetup. subExplain.Form .txtAddEdit = "add" Then strSql = fInsert Else
strSql = fUpdate

'Find lngPK, ACT_PK of new/updated action

If Forms!frmsetup. subExplain.Form .txtAddEdit = "edit" Then

lngPk = Forms!frmsetup. subExplain.Form .ACT_PK

Else

'just do a dmax, that will be the latest one

lngPk = DMax("ACT_PK", "TBL_ACTION S", "ACT_GAM_FK = " &
Forms!frmsetup. txtPK)

End If

'Now do terrain restrictions

fRestrictions lngPk

'requery

Forms!frmsetup. subExplain.Form .Requery

'bookmark

Set rst = Forms!frmsetup. subExplain.Form .RecordsetClone

With rst

.MoveFirst

.FindFirst "ACT_PK = " & lngPk

Forms!frmsetup. subExplain.Form .Bookmark = .Bookmark

.MoveFirst

.Close

End With

DoCmd.Close acForm, "frmsetupAction sadd", acSaveNo

End If

Exit_Proc:

Set rst = Nothing

Exit Sub

Err_Proc:

Select Case Err.Number

Case 3022 'index - in this case, a repaeated name

MsgBox "You already have an action with the abbreviation """ & Me.txtAbrev &
"""", vbExclamation

Me.txtName.SetF ocus

GoTo Exit_Proc

Case Else

MsgBox "Error " & Err.Number & " " & Err.Description , vbCritical,
"frmSetUpAction sAdd btnOk_Click", Err.HelpFile, Err.HelpContext

GoTo Exit_Proc

End Select
End Sub
--
Tim http://www.ucs.mun.ca/~tmarshal/
^o<
/#) "Burp-beep, burp-beep, burp-beep?" - Quaker Jake
/^^ "What's UP, Dittoooooo?" - Ditto


Nov 13 '05 #15
Tim Marshall <TI****@PurpleP andaChasers.Moe rtherium> 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
prematurely if certain conditions are not meant.


I do exactly the same thing myself.

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 #16
On Mon, 21 Feb 2005 20:53:29 GMT, "David W. Fenton"
<dX********@bwa y.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).

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.
Copy of code in message replied to:

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 #17
On Mon, 21 Feb 2005 21:33:43 GMT, Tony Toews <tt****@teluspl anet.net> wrote:
Tim Marshall <TI****@PurpleP andaChasers.Moe rtherium> 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
prematurely 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.
Nov 13 '05 #18
On Mon, 21 Feb 2005 20:53:29 GMT, "David W. Fenton"
<dX********@bwa y.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).

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.
Copy of code in message replied to:

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 #19
On Mon, 21 Feb 2005 21:33:43 GMT, Tony Toews <tt****@teluspl anet.net> wrote:
Tim Marshall <TI****@PurpleP andaChasers.Moe rtherium> 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
prematurely 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.
Nov 13 '05 #20

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
4624
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
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.

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.