Last active
March 8, 2018 15:51
-
-
Save alexkadis/5a2c9199e8bffd85c967113956ff83aa to your computer and use it in GitHub Desktop.
Rubberduck VBA error: "Assignment Without Set" False Positive
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
' Error thrown: | |
' ---------------------------------------------------------------- | |
' 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, [email protected] | |
' http://allenbrowne.com/ser-23a.html | |
' ---------------------------------------------------------------- | |
Public Function LogError(ByVal lngErrNumber As Long, ByVal strErrDescription As String, _ | |
ByVal strCallingProc As String, Optional ByVal vParameters As String, Optional ByVal bShowUser As Boolean = True) As Boolean | |
On Error GoTo Err_LogError | |
' TOFIX: There has to be a way to detect an infinite loop, force a crash | |
Dim strMsg As String ' String for display in MsgBox | |
Dim rst As DAO.Recordset ' The tLogError table | |
Dim ShowUserTxt As Integer | |
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 | |
ShowUserTxt = -1 | |
strMsg = "Error " & lngErrNumber & ": " & strErrDescription | |
MsgBox strMsg, vbExclamation, strCallingProc | |
End If | |
Set rst = CurrentDb.OpenRecordset("tLogError", , dbAppendOnly) | |
'' Using a `with` block | |
With rst | |
.AddNew | |
'''' Error thrown code starts here | |
.Fields("ErrNumber") = lngErrNumber | |
.Fields("ErrDescription") = Left$(strErrDescription, 255) | |
.Fields("ErrDate") = Now() | |
.Fields("CallingProc") = strCallingProc | |
.Fields("UserName") = CurrentUser() | |
.Fields("ShowUser") = ShowUserTxt | |
'''' Error thrown code ends here | |
If Not IsMissing(vParameters) Then | |
.Fields("Parameters") = Left$(vParameters, 255) | |
End If | |
.Update | |
.Close | |
End With | |
'' NOT using a `with` block | |
'' (obviously I wouldn't include both with and not with blocks of code) | |
'''' Error thrown code starts here | |
rst.AddNew | |
rst.Fields("ErrNumber") = lngErrNumber | |
rst.Fields("ErrDescription") = Left$(strErrDescription, 255) | |
rst.Fields("ErrDate") = Now() | |
rst.Fields("CallingProc") = strCallingProc | |
rst.Fields("UserName") = CurrentUser() | |
rst.Fields("ShowUser") = ShowUserTxt | |
'''' Error thrown code ends here | |
If Not IsMissing(vParameters) Then | |
rst.Fields("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()" | |
Debug.Print strMsg | |
Resume Exit_LogError | |
End Function | |
' The errors thrown: | |
' Error: Object variable 'Fields' is assigned without the 'Set' keyword - () AlexDatabase.AlexDatabaseModule, line 250 | |
' Error: Object variable 'Fields' is assigned without the 'Set' keyword - () AlexDatabase.AlexDatabaseModule, line 251 | |
' Error: Object variable 'Fields' is assigned without the 'Set' keyword - () AlexDatabase.AlexDatabaseModule, line 252 | |
' Error: Object variable 'Fields' is assigned without the 'Set' keyword - () AlexDatabase.AlexDatabaseModule, line 253 | |
' Error: Object variable 'Fields' is assigned without the 'Set' keyword - () AlexDatabase.AlexDatabaseModule, line 254 | |
' Error: Object variable 'Fields' is assigned without the 'Set' keyword - () AlexDatabase.AlexDatabaseModule, line 255 | |
' Error: Object variable 'Fields' is assigned without the 'Set' keyword - () AlexDatabase.AlexDatabaseModule, line 257 | |
' Error: Object variable 'Parameters' is assigned without the 'Set' keyword - () AlexDatabase.CamperActiveYears, line 190 | |
' Error: Object variable 'ReadGV' is assigned without the 'Set' keyword - AlexDatabase.SettingsModule, line 36 | |
' Error: Object variable 'Fields' is assigned without the 'Set' keyword - () AlexDatabase.SettingsModule, line 73 | |
' Error: Object variable 'Fields' is assigned without the 'Set' keyword - () AlexDatabase.SettingsModule, line 75 | |
' Error: Object variable 'Fields' is assigned without the 'Set' keyword - () AlexDatabase.SettingsModule, line 81 | |
' Rubberduck's attempt at a fix is: | |
With rst | |
.AddNew | |
.Set Fields("ErrNumber") = lngErrNumber | |
.Set Fields("ErrDescription") = Left$(strErrDescription, 255) | |
.Set Fields("ErrDate") = Now() | |
.Set Fields("CallingProc") = strCallingProc | |
.Set Fields("UserName") = CurrentUser() | |
.Set Fields("ShowUser") = ShowUserTxt | |
If Not IsMissing(vParameters) Then | |
.Fields("Parameters") = Left$(vParameters, 255) | |
End If | |
.Update | |
.Close | |
End With | |
' Which then throws: | |
' Error: Object variable 'Fields' is assigned without the 'Set' keyword - () AlexDatabase.AlexDatabaseModule, line 257 | |
' Error: Object variable 'Parameters' is assigned without the 'Set' keyword - () AlexDatabase.CamperActiveYears, line 190 | |
' Error: Object variable 'ReadGV' is assigned without the 'Set' keyword - AlexDatabase.SettingsModule, line 36 | |
' Error: Object variable 'Fields' is assigned without the 'Set' keyword - () AlexDatabase.SettingsModule, line 73 | |
' Error: Object variable 'Fields' is assigned without the 'Set' keyword - () AlexDatabase.SettingsModule, line 75 | |
' Error: Object variable 'Fields' is assigned without the 'Set' keyword - () AlexDatabase.SettingsModule, line 81 | |
' Error: Variable 'Fields' is used but not assigned - AlexDatabase.AlexDatabaseModule, line 250 | |
' Error: Variable 'Fields' is used but not assigned - AlexDatabase.AlexDatabaseModule, line 251 | |
' Error: Variable 'Fields' is used but not assigned - AlexDatabase.AlexDatabaseModule, line 252 | |
' Error: Variable 'Fields' is used but not assigned - AlexDatabase.AlexDatabaseModule, line 253 | |
' Error: Variable 'Fields' is used but not assigned - AlexDatabase.AlexDatabaseModule, line 254 | |
' Error: Variable 'Fields' is used but not assigned - AlexDatabase.AlexDatabaseModule, line 255 | |
' Error: Local variable 'Fields' is not declared - AlexDatabase.AlexDatabaseModule, line 250 | |
' Warning: Variable 'Fields' is not assigned - AlexDatabase.AlexDatabaseModule, line 250 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment