An Error while running Error Handling Code

S

SteelFire

So, on my forms I have some On Error code, but for some reason, its having an
error. The code looks like this:
Private Sub cmdOK_Click()
On Error GoTo Err_Handler
'<The code for my function>
Exit_Handler:
Exit Sub
Err_Handler:
Call LogError(Err.Number, Err.Description, "cmdOK_Click()")
Resume Exit_Handler
End Sub

I also have a Module that is a Copy/Paste from Allen Browne, which goes like
this:
Option Compare Database

Function LogError(ByVal lngErrNumber As Long, ByVal strErrDescription As
String, _
strCallingProc As String, Optional vParameters, Optional bShowUser As
Boolean = True) As Boolean
On Error GoTo Err_LogError
' Purpose: Generic error handler.
' Logs errors to table "tLogError".
' Arguments: lngErrNumber - value of Err.Number
' strErrDescription - value of Err.Description
' strCallingProc - name of sub|function that generated the error.
' vParameters - optional string: List of parameters to record.
' bShowUser - optional boolean: If False, suppresses display.
' Author: Allen Browne, (e-mail address removed)

Dim strMsg As String ' String for display in MsgBox
Dim rst As DAO.Recordset ' The tLogError table

Select Case lngErrNumber
Case 0
Debug.Print strCallingProc & " called error 0."
Case 2501 ' Cancelled
'Do nothing.
Case 3314, 2101, 2115 ' Can't save.
If bShowUser Then
strMsg = "Record cannot be saved at this time." & vbCrLf & _
"Complete the entry, or press <Esc> to undo."
MsgBox strMsg, vbExclamation, strCallingProc
End If
Case Else
If bShowUser Then
strMsg = "Error " & lngErrNumber & ": " & strErrDescription
MsgBox strMsg, vbExclamation, strCallingProc
End If
Set rst = CurrentDb.OpenRecordset("tLogError", , dbAppendOnly)
rst.AddNew
rst![ErrNumber] = lngErrNumber
rst![ErrDescription] = Left$(strErrDescription, 255)
rst![ErrDate] = Now()
rst![CallingProc] = strCallingProc
rst![UserName] = CurrentUser()
rst![ShowUser] = bShowUser
If Not IsMissing(vParameters) Then
rst![Parameters] = Left(vParameters, 255)
End If
rst.Update
rst.Close
LogError = True
End Select

Exit_LogError:
Set rst = Nothing
Exit Function

Err_LogError:
strMsg = "An unexpected situation arose in your program." & vbCrLf & _
"Please write down the following details:" & vbCrLf & vbCrLf & _
"Calling Proc: " & strCallingProc & vbCrLf & _
"Error Number " & lngErrNumber & vbCrLf & strErrDescription & vbCrLf
& vbCrLf & _
"Unable to record because Error " & Err.Number & vbCrLf & Err.
Description
MsgBox strMsg, vbCritical, "LogError()"
Resume Exit_LogError
End Function

But every time that I run the cmdOK, I get this error:
Compile error:

Expected variable or procedure, not module

Is there something that is wrong in the On Error code?
 
J

Jack Leach

Compile error:
Expected variable or procedure, not module

It sounds like your module has the same name as a function/sub. Standard
conventions prefix a module name with 'mod' to avoid this.

So, I'm going to guess that you can change the module name from LogError to
modLogError, and that should do it.

hth


--
Jack Leach
www.tristatemachine.com

"I haven''t failed, I''ve found ten thousand ways that don''t work."
-Thomas Edison (1847-1931)



SteelFire said:
So, on my forms I have some On Error code, but for some reason, its having an
error. The code looks like this:
Private Sub cmdOK_Click()
On Error GoTo Err_Handler
'<The code for my function>
Exit_Handler:
Exit Sub
Err_Handler:
Call LogError(Err.Number, Err.Description, "cmdOK_Click()")
Resume Exit_Handler
End Sub

I also have a Module that is a Copy/Paste from Allen Browne, which goes like
this:
Option Compare Database

Function LogError(ByVal lngErrNumber As Long, ByVal strErrDescription As
String, _
strCallingProc As String, Optional vParameters, Optional bShowUser As
Boolean = True) As Boolean
On Error GoTo Err_LogError
' Purpose: Generic error handler.
' Logs errors to table "tLogError".
' Arguments: lngErrNumber - value of Err.Number
' strErrDescription - value of Err.Description
' strCallingProc - name of sub|function that generated the error.
' vParameters - optional string: List of parameters to record.
' bShowUser - optional boolean: If False, suppresses display.
' Author: Allen Browne, (e-mail address removed)

Dim strMsg As String ' String for display in MsgBox
Dim rst As DAO.Recordset ' The tLogError table

Select Case lngErrNumber
Case 0
Debug.Print strCallingProc & " called error 0."
Case 2501 ' Cancelled
'Do nothing.
Case 3314, 2101, 2115 ' Can't save.
If bShowUser Then
strMsg = "Record cannot be saved at this time." & vbCrLf & _
"Complete the entry, or press <Esc> to undo."
MsgBox strMsg, vbExclamation, strCallingProc
End If
Case Else
If bShowUser Then
strMsg = "Error " & lngErrNumber & ": " & strErrDescription
MsgBox strMsg, vbExclamation, strCallingProc
End If
Set rst = CurrentDb.OpenRecordset("tLogError", , dbAppendOnly)
rst.AddNew
rst![ErrNumber] = lngErrNumber
rst![ErrDescription] = Left$(strErrDescription, 255)
rst![ErrDate] = Now()
rst![CallingProc] = strCallingProc
rst![UserName] = CurrentUser()
rst![ShowUser] = bShowUser
If Not IsMissing(vParameters) Then
rst![Parameters] = Left(vParameters, 255)
End If
rst.Update
rst.Close
LogError = True
End Select

Exit_LogError:
Set rst = Nothing
Exit Function

Err_LogError:
strMsg = "An unexpected situation arose in your program." & vbCrLf & _
"Please write down the following details:" & vbCrLf & vbCrLf & _
"Calling Proc: " & strCallingProc & vbCrLf & _
"Error Number " & lngErrNumber & vbCrLf & strErrDescription & vbCrLf
& vbCrLf & _
"Unable to record because Error " & Err.Number & vbCrLf & Err.
Description
MsgBox strMsg, vbCritical, "LogError()"
Resume Exit_LogError
End Function

But every time that I run the cmdOK, I get this error:
Compile error:

Expected variable or procedure, not module

Is there something that is wrong in the On Error code?
 
J

Jack Leach

Also, I would most certainly include Option Explicit in the module declaration
Option Compare Database
Option Explicit <------ ADD THAT
Function LogError(ByVal lngErrNumber As Long, ByVal strErrDescription As
String, _
strCallingProc As String, Optional vParameters, Optional bShowUser As
Boolean = True) As Boolean
....
....


--
Jack Leach
www.tristatemachine.com

"I haven''t failed, I''ve found ten thousand ways that don''t work."
-Thomas Edison (1847-1931)



SteelFire said:
So, on my forms I have some On Error code, but for some reason, its having an
error. The code looks like this:
Private Sub cmdOK_Click()
On Error GoTo Err_Handler
'<The code for my function>
Exit_Handler:
Exit Sub
Err_Handler:
Call LogError(Err.Number, Err.Description, "cmdOK_Click()")
Resume Exit_Handler
End Sub

I also have a Module that is a Copy/Paste from Allen Browne, which goes like
this:
Option Compare Database

Function LogError(ByVal lngErrNumber As Long, ByVal strErrDescription As
String, _
strCallingProc As String, Optional vParameters, Optional bShowUser As
Boolean = True) As Boolean
On Error GoTo Err_LogError
' Purpose: Generic error handler.
' Logs errors to table "tLogError".
' Arguments: lngErrNumber - value of Err.Number
' strErrDescription - value of Err.Description
' strCallingProc - name of sub|function that generated the error.
' vParameters - optional string: List of parameters to record.
' bShowUser - optional boolean: If False, suppresses display.
' Author: Allen Browne, (e-mail address removed)

Dim strMsg As String ' String for display in MsgBox
Dim rst As DAO.Recordset ' The tLogError table

Select Case lngErrNumber
Case 0
Debug.Print strCallingProc & " called error 0."
Case 2501 ' Cancelled
'Do nothing.
Case 3314, 2101, 2115 ' Can't save.
If bShowUser Then
strMsg = "Record cannot be saved at this time." & vbCrLf & _
"Complete the entry, or press <Esc> to undo."
MsgBox strMsg, vbExclamation, strCallingProc
End If
Case Else
If bShowUser Then
strMsg = "Error " & lngErrNumber & ": " & strErrDescription
MsgBox strMsg, vbExclamation, strCallingProc
End If
Set rst = CurrentDb.OpenRecordset("tLogError", , dbAppendOnly)
rst.AddNew
rst![ErrNumber] = lngErrNumber
rst![ErrDescription] = Left$(strErrDescription, 255)
rst![ErrDate] = Now()
rst![CallingProc] = strCallingProc
rst![UserName] = CurrentUser()
rst![ShowUser] = bShowUser
If Not IsMissing(vParameters) Then
rst![Parameters] = Left(vParameters, 255)
End If
rst.Update
rst.Close
LogError = True
End Select

Exit_LogError:
Set rst = Nothing
Exit Function

Err_LogError:
strMsg = "An unexpected situation arose in your program." & vbCrLf & _
"Please write down the following details:" & vbCrLf & vbCrLf & _
"Calling Proc: " & strCallingProc & vbCrLf & _
"Error Number " & lngErrNumber & vbCrLf & strErrDescription & vbCrLf
& vbCrLf & _
"Unable to record because Error " & Err.Number & vbCrLf & Err.
Description
MsgBox strMsg, vbCritical, "LogError()"
Resume Exit_LogError
End Function

But every time that I run the cmdOK, I get this error:
Compile error:

Expected variable or procedure, not module

Is there something that is wrong in the On Error code?
 
S

SteelFire via AccessMonster.com

Wow, I can't believe that it was that easy of a fix. It works just fine now,
and the error now makes sense. Well, hien-site and all that. Thanks.

Jack said:
Compile error:

Expected variable or procedure, not module

It sounds like your module has the same name as a function/sub. Standard
conventions prefix a module name with 'mod' to avoid this.

So, I'm going to guess that you can change the module name from LogError to
modLogError, and that should do it.

hth
So, on my forms I have some On Error code, but for some reason, its having an
error. The code looks like this:
[quoted text clipped - 83 lines]
Is there something that is wrong in the On Error code?
 
J

Jack Leach

Wow, I can't believe that it was that easy of a fix

Too bad they aren't all that easy <g>

Naming conventions are a programmers very very very bestest friend in the
whole wide world. They divert errors like this ranging from annoying to
catastrophic. Google "Reddick's Naming Conventions" for more info. :)

--
Jack Leach
www.tristatemachine.com

"I haven''t failed, I''ve found ten thousand ways that don''t work."
-Thomas Edison (1847-1931)



SteelFire via AccessMonster.com said:
Wow, I can't believe that it was that easy of a fix. It works just fine now,
and the error now makes sense. Well, hien-site and all that. Thanks.

Jack said:
Compile error:

Expected variable or procedure, not module

It sounds like your module has the same name as a function/sub. Standard
conventions prefix a module name with 'mod' to avoid this.

So, I'm going to guess that you can change the module name from LogError to
modLogError, and that should do it.

hth
So, on my forms I have some On Error code, but for some reason, its having an
error. The code looks like this:
[quoted text clipped - 83 lines]
Is there something that is wrong in the On Error code?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top