"Label Not Defined" Error

HillTJ

To train a dog, first know more than the dog..
Local time
Today, 08:50
Joined
Apr 1, 2019
Messages
731
Hi,

I'm getting more ambitious. I copied a module from Microsoft that calculates a persons date. Works fine, so I thought I'd add a "Generic" error handler that can be called up from other functions that I'm to write.

Got the code from Allen Browne. It records the error in a table. Seems pretty good. But upon compilation it stalls @ "On Error Goto ErrorHandler" says label not defined. See full text of routine below. I have copied "LogError code as a module too.

As usual any advice would be great.

Option Compare Database
Option Explicit

'*************************************************************
' FUNCTION NAME: Age()
'
' PURPOSE:
' Calculates age in years from a specified date to today's date.
'
' INPUT PARAMETERS:
' varBirthDate: a birth date.
'
' RETURN
' Age in years.
'
'*************************************************************
Function Age(varBirthDate As Variant) As Integer
Dim varAge As Variant

On Error GoTo ErrorHandler

If IsNull(varBirthDate) Then Age = 0: Exit Function

varAge = DateDiff("yyyy", varBirthDate, Now)
If Date < DateSerial(Year(Now), Month(varBirthDate), _
Day(varBirthDate)) Then
varAge = varAge - 1
End If
Age = CInt(varAge)
End Function

'*************************************************************
' FUNCTION NAME: AgeMonths()
'
' PURPOSE:
' Compliments the Age() function by calculating the number of months
' that have expired since the last month supplied by the specified date.
' If the specified date is a birthday, the function returns the number of
' months since the last birthday.
'
' INPUT PARAMETERS:
' varBirthDate: a birth date.
'
' RETURN
' Months since the last birthday.
'*************************************************************
Function AgeMonths(ByVal varBirthDate As Variant) As Integer

If IsNull(varBirthDate) Then AgeMonths = 0: Exit Function

Dim tAge As Double
tAge = (DateDiff("m", varBirthDate, Now))
If (DatePart("d", varBirthDate) > DatePart("d", Now)) Then
tAge = tAge - 1
End If
If tAge < 0 Then
tAge = tAge + 1
End If

AgeMonths = CInt(tAge Mod 12)

ExitError:

Exit Function

ErrorHandler:
Select Case Err.Number
Case 9999
Resume Next
Case 999
Resume Exit_Age
Case Else
Call LogError(Err.Number, Err.Description, "Age()")
Resume ExitError
End Select

End Function
 
Hi. You're getting the error because you are missing a label named "ErrorHandler." When you write in your code GoTo ErrorHandler, then you'll need to have a label like ErrorHandler: somewhere in your code (within the same code stub).
 
The dbguy, thanks for the prompt response, can you show me how to add the label. Sorry, but i'm still a gumby when it comes to coding.

Appreciate it.
 
Without checking the rest of the code, comment out the offending line and it should all work.

If you want to keep the
Code:
On Error GoTo ErrorHandler
you have to add the error routine, it's highlighted in blue.
Code:
Function Age(varBirthDate As Variant) As Integer
   Dim varAge As Variant

   On Error GoTo ErrorHandler

   If IsNull(varBirthDate) Then Age = 0: Exit Function

      varAge = DateDiff("yyyy", varBirthDate, Now)
      If Date < DateSerial(Year(Now), Month(varBirthDate), _
         Day(varBirthDate)) Then
         varAge = varAge - 1
      End If
     Age = CInt(varAge)

[COLOR="Blue"]ExitError:
   Exit sub

ErrorHandler:
   msgbox "Error : " &  Err.Number & " " & Err.Description
   Age = -1    ' obviously not correct
   resume ExitError
End Function[/COLOR]

Somehow, in your copying of the functions things have gone astray. The above is just a pure basic error routine.

In your second function, the error checking routine would never be checked as there is no
Code:
On Error Goto Errorhandler
at the start of the function.
 
Last edited:
Cheers, will give it a go & let yoy know how it went. Thanks for your time with this.
 
EssayTee,

Got it. Thanks.
 

Users who are viewing this thread

Back
Top Bottom