Asked
7 years, 11 months ago
Viewed
39k times
I have a VBA macro which gave me that error message.
Sub Function1()
' Give the user macro options based on how fast or slow the computer
' is using advanced conditional compiling
vuserChoice = MsgBox("This macro by default treats all numbers as decimals for maximum precision. If you are running this macro on an old computer, you may want to declare numbers as singles, to speed up the macro.")
MsgBox ("Decimal: recommended for maximum precision. Also slower." & vbNewLine & "Long: not recommended. Rounds to nearest integer." & vbNewLine & "Single: not recommended. A lightweight double." & vbNewLine & "Integer: not recommended. Quick and low-precision.")
If vuserChoice = "Decimal" Or "decimal" Then
GoTo FunctionDecimal
ElseIf vuserChoice = "Double" Or "double" Then
GoTo FunctionDouble
ElseIf vuserChoice = "Single" Or "single" Then
GoTo FunctionSingle
ElseIf vuserChoice = "Long" Or "long" Then
GoTo FunctionLong
Else
GoTo FunctionNotValidVarType
End If
' MEeff = measure of efflux due to crudely purified HDL in scintillation
MsgBox "For additional information about this macro:" & vbNewLine & "1. Go to tab Developer" & vbNewLine & "2. Select Visual Basic or Macro." & vbNewLine & "See the comments or MsgBoxes (message boxes)."
End Sub
The offending line is:
GoTo FunctionNotValidVarType
I have the function FunctionNotValidVarType
below this code. I have it as:
Public Sub FunctionNotValidVarType()
MsgBox "VarType " & VarType & " is not supported. Please check spelling."
End Sub
What do I need to do to let the first function recognize FunctionNotValidVarType
? Thanks.
asked Jun 24, 2015 at 15:05
GoTo
will try and transfer the code execution to a different position in the current Subroutine with the given label.
Specifically, GoTo FunctionNotValidVarType
will try and execute the line:
FunctionNotValidVarType: 'Do stuff here
which doesn’t exist in your current code.
If you want to call another function use Call FunctionNotValidVarType
answered Jun 24, 2015 at 15:09
kaybee99kaybee99
4,5282 gold badges32 silver badges42 bronze badges
6
Remove the word GoTo
GoTo
tells the code to jump to a label, you want it to enter a new procedure, not go to a label
answered Jun 24, 2015 at 15:14
SierraOscarSierraOscar
17.5k6 gold badges40 silver badges68 bronze badges
Remove Goto
from the call to your Sub()
If you really wanted to use a Goto
(and you shouldn’t), you would
goto Label
Label:
where the label is defined by the trailing colon :
answered Jun 24, 2015 at 15:10
FreeManFreeMan
5,6201 gold badge27 silver badges53 bronze badges
3
GoTo
transitions to a label, a label is defined with :
For example:
Sub G()
On Error GoTo err_handling
a=1/0
Exit Sub
err_handling:
MsgBox "Holy Shit, an error occurred !"
End Sub
To apply GoTo
on a Sub
you need call it and exit:
Call FunctionNotValidVarType
Exit Sub
(Technically, it is not the same as GoTo
if you take the call stack into consideration, but the end result is the same)
GoTo
is not considered a good practice, but if that doesn’t concern you, take a look also at GoSub
at the official docs.
answered Jun 24, 2015 at 16:29
Uri GorenUri Goren
13.2k6 gold badges57 silver badges109 bronze badges
I keep getting this msg
VBA
Compile Error:
Label not defined
Yes, Label starts with a letter.
Label Has No spaces
Label Has Nothing but letters.
Label’s First letter is in column 1 ( per the editor in Access VBA code editor )
Label Does end with a “ : “ at end.
Not with the extra spaces or quotes.
i Did comment the line out with the Label, ( only label on the line). On a new line I retyped the same label.
i Did save, do a RefreshAll, did a close of VBA editor, then restarted VBA editor, clicked on reset, then debug got same msg.
i did this between every time i tried a different way.
I used GoTo ErrHandler
then added On Error GoTo ErrHander
I copied the label from the Goto line, then pasted it at spot I want, with first letter in column 1, added “
: “ at the end, so no way I could have misspelled the label.
I also did the same using 57
as the label, I did put label on line 57 just in case. as in goto a line number.
Every time I get the same msg.
Only thing I noticed that was different is in Tools / References I see under Available References the below
EditionUpgradeHelperLib
Location C:WindowsSystems32 EditionUpgradeHelperLib.dll
Language: Standard
That is the only reference I do NOT ever recall seeing before. If definitely was no picked before.
I did search net, only info I got came to this, download third party app(s) that is supposed to check all “dll” files , fix them, and remove them if you want to, but that doc had a LONG list of things to do before run program, and hope fixes, not make worse.
and how to reinstall the dll if it turns out you MUST have it.
Was never able to find out what that file is supposed to do.
Never before had this msg, unless I made a typo.
I do have under Available references checked
Visula Basic For Applications
Microsoft Access 16.0 Object Library
OLE Automation
Microsoft Office 16.0 Access database engine Object Library
Microsoft Office 16.0 Object Library
i thank you for any help you can give.
mark J
Mark J
Permalink
Cannot retrieve contributors at this time
title | keywords | f1_keywords | ms.prod | ms.assetid | ms.date | ms.localizationpriority |
---|---|---|---|---|---|---|
Label not defined |
vblr6.chm1011207 |
vblr6.chm1011207 |
office |
0420c1d7-d2d3-2df3-2157-63834d3ac0d2 |
06/08/2017 |
medium |
This error has the following cause and solution:
-
A line label or line number is referred to (for example in a GoTo statement), but doesn’t occur within the scope of the reference.
The label must be within the procedure that contains the reference. Line labels are visible only in their own procedures.
For additional information, select the item in question and press F1 (in Windows) or HELP (on the Macintosh).
[!includeSupport and feedback]
vet2810 0 / 0 / 0 Регистрация: 13.05.2015 Сообщений: 7 |
||||
1 |
||||
19.02.2016, 08:01. Показов 7933. Ответов 4 Метки нет (Все метки)
Здравствуйте, помогите, пожалуйста, урок из книжки. Пишу код, а он мне выдает Compile error: Label not defined
0 |
Заблокирован |
|
19.02.2016, 08:20 |
2 |
vet2810, LasLine<>LastLine
1 |
0 / 0 / 0 Регистрация: 13.05.2015 Сообщений: 7 |
|
19.02.2016, 08:27 [ТС] |
3 |
sorry, что не по теме, сразу не нашел подходящую.
0 |
Заблокирован |
|
19.02.2016, 08:29 |
4 |
LasLine<>LastLine Экранную лупу включите.
1 |
5590 / 1580 / 406 Регистрация: 23.12.2010 Сообщений: 2,366 Записей в блоге: 1 |
|
19.02.2016, 09:12 |
5 |
Выдает ошибку, потому что пропущена буква t в названии метки.
0 |
-
#1
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
-
#2
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.
-
#3
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!
-
#4
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.
-
#5
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
-
#6
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.
-
#7
I will admitt I read this problem to fast and jumped the gun… a little
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.
-
#8
I will admitt I read this problem to fast and jumped the gun… a little
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.
Dick S
-
#9
I will admitt I read this problem to fast and jumped the gun… a little
[FONT="]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="]Private Sub cmdNext_Click() 'Lets user know he has reached last record[/FONT]
[FONT="]On Error GoTo err_handler [/FONT]
[FONT="] DoCmd.GoToRecord acForm, "frmMain", acNext[/FONT]
[FONT="] DoCmd.GoToControl "cmbSal"[/FONT]
[FONT="]cmdNext_Exit:[/FONT]
[FONT="]Exit Sub [/FONT]
[FONT="]err_handler:[/FONT]
[FONT="]If Err.Number = 2105 Then[/FONT]
[FONT="] MsgBox "You have already reached the Last student.", vbApplicationModal, "That's All Folks"[/FONT]
[FONT="]Else[/FONT]
[FONT="] MsgBox Err.Description, vbExclamation, "Error #: " & Err.Number[/FONT]
[FONT="]End If[/FONT]
[FONT="] Resume cmdNext_Exit[/FONT]
[FONT="] End Sub[/FONT]
[FONT="]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]
-
#10
[FONT="] 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.
-
#11
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="]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, unless, of[/FONT] course something else in Access ticks me off, which is often.
-
#12
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.
-
#13
Not important at all unless you want a particular messge to show.
PS #10 came in just as I was sending #11
-
#14
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.
-
#15
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.
-
#16
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?
-
#17
Ok, Will take you advise, what should I have been forcing on in these last posts on error handling?
sorry focusing
-
#18
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.
-
#19
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.
-
#20
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.
Chris.