Error Handling (label not defined)

Dick7Access

Dick S
Local time
Today, 10:25
Joined
Jun 9, 2009
Messages
4,253
I am studying error handling. I am using VBA Developers Hand Book and some Google stuff. I have been over the material several times and cannot find my mistake. When I click cmdFindState it give me "compile error" " label not defined",and stops at:

Code:
"On Error GoTo Err_cmdFindState_Click"

Code:
  Private Sub cmdFindState_Click()
   On Error GoTo Err_cmdFindState_Click
  DoCmd.OpenForm "frmChurchesAll"
  DoCmd.ApplyFilter "qryFindState"
  '-----------------------------------------
   ' 51 Vermont
    '------------------------------------------
     DoCmd.GoToControl "ComboState"
      If ComboState.Text = "VT" Then
      Me.lblStLong.Caption = "Vermont"
      Me.lblStateOf.Visible = True
      Me.lblStLong.Visible = True
          End If
          
  Exit_cmdFindState:
   Exit Sub
  Err_cmdFindState:
  MsgBox "this is a test", , "Access"
          
  lbAllChurches.Visible = False
  End Sub
 
Firstly, the location Err_cmdFindState_Click is not defined in the procedure.

However you appear to still be insisting on farting around with data in the code which you were told previously is the wrong way to handle this enitirely.
 
Firstly, the location Err_cmdFindState_Click is not defined in the procedure.

However you appear to still be insisting on farting around with data in the code which you were told previously is the wrong way to handle this enitirely.

Maybe this is where I don't understand the book. Isn't the line just before the message box the location. Isn't that the line that will ecute if an error occurs?

I will address the farting later!:)
 
The On Error GoTo refers to a label in the procedure. The label itself is just a string followed by a colon.

That label can be called anything so long as it is unique in the procedure. Microsoft encouraged the use of a label based on the name of the procedure but it is not essential.
 
Plus your coding layout is a mess, readable code is maintanable code.

Like Galaxiom said, no "On error goto" means no error handling.... Your book should say so
 
Plus your coding layout is a mess, readable code is maintanable code.

Like Galaxiom said, no "On error goto" means no error handling.... Your book should say so

It may be a mess but when you are learning you don't know that. As far as I can tell I entered it just like the book said. I have another book called the "Access Cookbook" and that one bounces back and forth from one example to another that it is of little use to me. Even now, I can't tell what I am missing.
 
I will admitt I read this problem to fast and jumped the gun... a little :eek:

this is what your code should look like to keep it readable:
Code:
Private Sub cmdFindState_Click()
    On Error GoTo [COLOR="Red"]Err_cmdFindState_Click[/COLOR]
    DoCmd.OpenForm "frmChurchesAll"
    DoCmd.ApplyFilter "qryFindState"
    '-----------------------------------------
    ' 51 Vermont
    '------------------------------------------
    DoCmd.GoToControl "ComboState"
    If ComboState.Text = "VT" Then
        Me.lblStLong.Caption = "Vermont"
        Me.lblStateOf.Visible = True
        Me.lblStLong.Visible = True
    End If
          
[COLOR="Lime"]Exit_cmdFindState:[/COLOR]
   Exit Sub
[COLOR="lime"]Err_cmdFindState:[/COLOR]
  MsgBox "this is a test", , "Access"
          
  lbAllChurches.Visible = False
End Sub
The LABEL in red is causing your problem because you have not defined it, no where does it say "Err_cmdFindState_Click:"
The way you define the "Goto" labels is by having them exactly stated as a label as the green lines. The second green line (probably) should read exactly as the red Label, like so:

Code:
Private Sub cmdFindState_Click()
    On Error GoTo [COLOR="Red"]Err_cmdFindState_Click[/COLOR]
    DoCmd.OpenForm "frmChurchesAll"
    DoCmd.ApplyFilter "qryFindState"
    '-----------------------------------------
    ' 51 Vermont
    '------------------------------------------
    DoCmd.GoToControl "ComboState"
    If ComboState.Text = "VT" Then
        Me.lblStLong.Caption = "Vermont"
        Me.lblStateOf.Visible = True
        Me.lblStLong.Visible = True
    End If
          
Exit_cmdFindState:
   Exit Sub
[COLOR="Red"]Err_cmdFindState_Click:[/COLOR]
  MsgBox "this is a test", , "Access"
          
  lbAllChurches.Visible = False
End Sub

FYI, Goto's are only really acceptable in error handling... avoid them like the plague otherwize.
 
I will admitt I read this problem to fast and jumped the gun... a little :eek:
Thank you very much. You would think for the price for books, they at least would get it right. I check the book at least three times, and even numbered each line of code in the book and VBA and it looked exactly like the book, but didn't work.
If you come to FL look me up and I will treat you to the best Chinese buffet in the US. Best by my taste buds, of course.:D
Dick S
 
I will admitt I read this problem to fast and jumped the gun... a little :eek:

[FONT=&quot]Reading and not seeing is what usually gets me in trouble, which is fatal in VBA. Even after you corrected my code it took me twice before I noticed that I had left off the _click. Googling this I noticed that some error code used numbers. For example in other apps that I have made I have used this code:
[/FONT]
Code:
  [FONT=&quot]Private Sub cmdNext_Click() 'Lets user know he has reached last record[/FONT]
  [FONT=&quot]On Error GoTo err_handler [/FONT]
  [FONT=&quot]   DoCmd.GoToRecord acForm, "frmMain", acNext[/FONT]
  [FONT=&quot]    DoCmd.GoToControl "cmbSal"[/FONT]
  [FONT=&quot]cmdNext_Exit:[/FONT]
  [FONT=&quot]Exit Sub [/FONT]
  [FONT=&quot]err_handler:[/FONT]
  [FONT=&quot]If Err.Number = 2105 Then[/FONT]
  [FONT=&quot]   MsgBox "You have already reached the Last student.", vbApplicationModal, "That's All Folks"[/FONT]
  [FONT=&quot]Else[/FONT]
  [FONT=&quot]   MsgBox Err.Description, vbExclamation, "Error #: " & Err.Number[/FONT]
  [FONT=&quot]End If[/FONT]
  [FONT=&quot]   Resume cmdNext_Exit[/FONT]
  [FONT=&quot] End Sub[/FONT]
[FONT=&quot]But I just copied it and did not know what or why it did what it did. How important is it to use actual error numbers?[/FONT]
 
[FONT=&quot] How important is it to use actual error numbers?[/FONT]

Not important at all unless you want a particular messge to show.

What you have posted will only respond with the warning about it being the end of file if that is the error raised.

Otherwise it just shows the error number and description as per the Else section.
 
Firstly, the location Err_cmdFindState_Click is not defined in the procedure.

However you appear to still be insisting on farting around with data in the code which you were told previously is the wrong way to handle this enitirely.
[FONT=&quot]Now let’s get back to the farthing. I am assuming you are referring to my use of the data from the state field to populate the label. That’s because I never did get it to work with a text field so it was more important to me to learn error handling, as I have always just copied somebodies else error handling and I was determined to learn to write the proper code for myself. Now I can start working on the farting,:D unless, of[/FONT] course something else in Access ticks me off, which is often.
 
Dick.

In your first post you placed this inside a code block for posting:-
"On Error GoTo Err_cmdFindState_Click"
but the above is not a line of code in your program.

You need to be careful; you need to understand that the compiler will not make allowances for some things that people simply breeze over.

"On Error GoTo Err_cmdFindState_Click"
is not a line of code, it is a string literal but
On Error GoTo Err_cmdFindState_Click
is a line of code.

So when you say “When I click cmdFindState it give me "compile error" " label not defined",and stops at: "On Error GoTo Err_cmdFindState_Click"

Then that is false information you have supplied because the compiler did not stop at a string literal but rather stoped at a line of code.

Dick, it is important that you first learn the difference between a line of code and a string literal. With code, you need to be meticulous. It is also important that you supply correct information. One of the reasons for supplying correct information is that in trying to do so you may be able to learn how to solve your own problems.

What you will find is that many problems are solved by asking the exact, relevant, question.

----------

>> How important is it to use actual error numbers?<<
Forget about error handling: just go back and do the basics.

Chris.
 
Dick.

In your first post you placed this inside a code block for posting:-
"On Error GoTo Err_cmdFindState_Click"
but the above is not a line of code in your program.

You need to be careful; you need to understand that the compiler will not make allowances for some things that people simply breeze over.

"On Error GoTo Err_cmdFindState_Click"
is not a line of code, it is a string literal but
On Error GoTo Err_cmdFindState_Click
is a line of code.

So when you say “When I click cmdFindState it give me "compile error" " label not defined",and stops at: "On Error GoTo Err_cmdFindState_Click"

Then that is false information you have supplied because the compiler did not stop at a string literal but rather stoped at a line of code.

Dick, it is important that you first learn the difference between a line of code and a string literal. With code, you need to be meticulous. It is also important that you supply correct information. One of the reasons for supplying correct information is that in trying to do so you may be able to learn how to solve your own problems.

What you will find is that many problems are solved by asking the exact, relevant, question.

----------

>> How important is it to use actual error numbers?<<
Forget about error handling: just go back and do the basics.

Chris.

I did debate if I should use code block for the first "On Error GoTo Err_cmdFindState_Click" as I am doing here. I thought that I would be safe using the code block.

I have studied some basic in some areas, but not in others. I have been doing Access for twenty years, but was more concerned about kicking a program out the door for someone than I was in learning good coding. Never ever using VBA, so now I am going over old apps and revising them. Now I am frustrated when errors pop up so I decided it was about time I figured it out.
As funny as it may sound in my circle, I am the expert. The things I study now are usually the things that bug me the most.
 
Dick.

You appear not to be able to focus on a single point.
If you wish to try and write code then that is one of the first things you will need to learn.

How long you have been using Access is totally irrelevant.
What you have been using Access for is totally irrelevant.
If you happen to be the expert in your circle then that is totally irrelevant.
Whatever excuse you try to put up is totally irrelevant.

You need to learn how to focus on a problem.

Chris.
 
Dick.

You appear not to be able to focus on a single point.
If you wish to try and write code then that is one of the first things you will need to learn.

How long you have been using Access is totally irrelevant.
What you have been using Access for is totally irrelevant.
If you happen to be the expert in your circle then that is totally irrelevant.
Whatever excuse you try to put up is totally irrelevant.

You need to learn how to focus on a problem.

Chris.

Ok, Will take you advise, what should I have been forcing on in these last posts on error handling?
 
Dick.

You need to focus on not making mistakes. We all make mistakes but some people seem to think that other people will make allowances for them. That may be true but a computer is not another person and VBA is not English.

You need to think differently when speaking to a computer; it will make very few allowances for your mistakes.

So that is the first thing to focus on…trying to not make mistakes.
Do not make excuses for your mistakes, try to reduce them.

Chris.
 
Dick.

You need to focus on not making mistakes. We all make mistakes but some people seem to think that other people will make allowances for them. That may be true but a computer is not another person and VBA is not English.

You need to think differently when speaking to a computer; it will make very few allowances for your mistakes.

So that is the first thing to focus on…trying to not make mistakes.
Do not make excuses for your mistakes, try to reduce them.

Chris.

Well of course, I don't want to make mistakes. I don’t think I have ever meet anybody who likes making mistakes. I research my error handling extensively before posting. Why do you think this symbol (:banghead:) is on here for. What sometimes is so very obvious to some, is a blank wall to others.
My comments were not meant to be excuses, they were intended to make anybody who choose (That's the key word) choose to answer, that not everybody has the same goals in life, or VBA. Not everybody has the same abilities, or the same amount of time. (The list goes on) Not everybody is annoyed by inserting the human element in a post. Also as posted on this forum many times, by many people, there are many ways in Access to do the same task. Thanks for answering. My standing offer, come to FL and eat at the US’s best Chinese Buffet, my treat.
 
Dick.

That is exactly what I am trying to get at.

Post #19 has nothing to do with trying to solve your problem.
All it is is just words which do not contribute to your desired solution.

Post #19 is totally irrelevant to the solution. It is being used simply as some social interaction. It is not a desire to find a technical solution.

What on Earth does a ‘Chinese Buffet’ have to do with your problem?
Why should I, or anyone else, have to wade through all those needless words just to get at the problem?

Why do people have to waist their time sifting through all that garbage?

I am not going to waist any more of my time with things like ‘Chinese Buffet’ in the US. :mad:

Chris.
 

Users who are viewing this thread

Back
Top Bottom