Compile Error - Label Not Defined

K

Kathryn

Hi,

I have the following code that resutls in a compile
error. It's purpose is to run some append queries to
create new records so the user can enter additional data.
The user chooses a grant and their name from drop-downs
before adding the info so it can be correlated to them.
Only one response per user is allowed. I wanted to set an
error message box so that if the user had already
evaluated a particular grant, they would get a message.

Do I need to attach the error event to another obejct?
Can you review my code and tell me if I am on track? I
have not used any On Error code before...

Private Sub cmdReviewGrant_Click()
On Error GoTo Err_cmdReviewGrant_Click

DoCmd.DoMenuItem acFormBar, acRecordsMenu,
acSaveRecord, , acMenuVer70

DoCmd.SetWarnings False
DoCmd.OpenQuery "qappGrant"
DoCmd.OpenQuery "qappOrgAdd"
Me![sfrmGrants].Requery
Me![sfrmOrgAdd].Requery
DoCmd.SetWarnings True

Exit_cmdReviewGrant_Click:
Exit Sub

Err_cmdReviewGrant:
MsgBox Error$
Resume Exit_cmdReviewGrant_Click

End Sub

Thank you!
 
J

John Vinson

Hi,

I have the following code that resutls in a compile
error. It's purpose is to run some append queries to
create new records so the user can enter additional data.
The user chooses a grant and their name from drop-downs
before adding the info so it can be correlated to them.
Only one response per user is allowed. I wanted to set an
error message box so that if the user had already
evaluated a particular grant, they would get a message.

Do I need to attach the error event to another obejct?
Can you review my code and tell me if I am on track? I
have not used any On Error code before...

Private Sub cmdReviewGrant_Click()
On Error GoTo Err_cmdReviewGrant_Click

Err_cmdReviewGrant:

You're telling Access to go to the label Err_cmdReviewGrant_Click but
there is no such label. Instead you have a label Err_cmdReviewGrant.

Change either one so that they both match and you'll be OK!

John W. Vinson[MVP]
 
E

Eric Schittlipz

Kathryn said:
Hi,

I have the following code that resutls in a compile
error. It's purpose is to run some append queries to
create new records so the user can enter additional data.
The user chooses a grant and their name from drop-downs
before adding the info so it can be correlated to them.
Only one response per user is allowed. I wanted to set an
error message box so that if the user had already
evaluated a particular grant, they would get a message.

Do I need to attach the error event to another obejct?
Can you review my code and tell me if I am on track? I
have not used any On Error code before...

Private Sub cmdReviewGrant_Click()
On Error GoTo Err_cmdReviewGrant_Click

DoCmd.DoMenuItem acFormBar, acRecordsMenu,
acSaveRecord, , acMenuVer70

DoCmd.SetWarnings False
DoCmd.OpenQuery "qappGrant"
DoCmd.OpenQuery "qappOrgAdd"
Me![sfrmGrants].Requery
Me![sfrmOrgAdd].Requery
DoCmd.SetWarnings True

Exit_cmdReviewGrant_Click:
Exit Sub

Err_cmdReviewGrant:
MsgBox Error$
Resume Exit_cmdReviewGrant_Click

End Sub

Thank you!


The immediate cause of the problem is the line
On Error GoTo Err_cmdReviewGrant_Click
which says: if an error occurs go to the line labelled
"Err_cmdReviewGrant_Click" (a line label is followed by a colon). However,
you do not have this label - you have "Err_cmdReviewGrant" hich is close -
but not close enough!

The labels do not have to be so obscure and involve underscores - this is a
legacy from wizard-written code which uses this format. It's not too bad
but you might adopt your own convention. My subs often look like this:

Private Sub cmdExit_Click()

On Error GoTo Err_Handler

DoCmd.Close

Exit_Handler:
Exit Sub

Err_Handler:
MsgBox Err.Description, vbExclamation, "Error No: " & Err.Number
Resume Exit_Handler

End Sub

The error-handling line is always labelled "Err_Handler" it doesn't change
to reflect the control name and event it reacts to (eg
Err_cmdReviewGrant_Click). This makes code more standard and more
'cut-and-pastable'.


A final point is that, I can't exactly see what those queries do but if you
turn warnings off in a procedure, you should make sure they turned back on
in the event of an error. ie turn them back on after the exit line:

Exit_cmdReviewGrant_Click:
DoCmd.SetWarnings True
Exit Sub
 
Top