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
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