Sorry for the change of topic, but I think I'm having a catharsis of
sorts here...
Terry Kreft wrote:
Tim,
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>
8) 8) <sob> I've left myself wide open now...
Seriously though, how would you do it?
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.
About 1993, a programmer at an organization I was involved with told me
GOTO was bad in any language. I've made a large and successful effort
to avoid it by using if statements, loops and so on. However, in the
past year or so, I have started using using it only as I describe above.
Of course for ensuring conditions are met, you can use an if statement:
If <conditions met> then
do stuff
end if
Exit_Proc:
However, you would have to be very creative in the case of the example
below.
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.
I'm curious how folks would handle the verification section in which the
app checks to make sure the user has supplied specific values. It's a
large procedure, but the majority of it is to check the correct values
are there to avoid a runtime error.
I've learned an incredible amount from you and all the other folks here
over the past 7 or so years, so I'll gladly take large amounts of
ridicule 8) for the following, but am sure I'll learn a better way to do
it amidst it all!
There are a couple of statements that call on other routines to check
stuff. See If fSelectTerrain = False Then GoTo Exit_Proc.... Perhaps I
could have:
If fselectTerrain = true and <insert various other subroutine checks> then
end if
Anyway, thanks in advance! 8)
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
On Error GoTo Err_Proc
'ensure abbreviation and name is chosen
If Nz(Me.txtAbrev, "") = "" Then
MsgBox "Enter a unique (for this campaign) abbreviation/code
for this action!"
Me.txtAbrev.Set Focus
GoTo Exit_Proc
End If
'ensure a name is selected for this action
If Trim(Nz(Me.txtN ame, "")) = "" Then
MsgBox "Enter a name for this action!"
Me.txtName.SetF ocus
GoTo Exit_Proc
End If
'make sure an action type is selected
If IsNull(Me.fraTy pe) Or Me.fraType = 0 Then
MsgBox "You haven't chosen an action type."
Me.fraType.SetF ocus
GoTo Exit_Proc
End If
'next verify that if 11 or 12 that one or all destination list box
item is selected
If fCheckDest = False Then GoTo Exit_Proc 'a separate procedure
If fSelectTerrain = False Then GoTo Exit_Proc
'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
MsgBox "Choose a terrain type ""effect"" for the
crossing/obstacle!"
Me.lstDestinati on.SetFocus
GoTo Exit_Proc
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
MsgBox "Enter a value!"
Me.txtValue.Set Focus
GoTo Exit_Proc
End If
End If
'missions
If Me.txtMissions. Enabled = True Then
If IsNull(Me.txtMi ssions) Then
MsgBox "Enter number of missions!"
Me.txtMissions. SetFocus
GoTo Exit_Proc
End If
End If
'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
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