Hi All,
I have a generic function I use to validate if a value is within a drop down list. I recently decided to make it error within the function rather than passing it back to the calling proc, and I decided to do this by raising a custom error. This works as expected the first time it is invoked, but second and subsequent times produce an Automation error on the err.raise line. The complete sequence of events are:
1. user presses "Validate" button to start validation code
2. A user entered value fails the lookup and the expected error msgbox in the error handler is displayed
3. Usr clicks OK in msgbox and validation ends
4. User corrects the error (or not) and then again presses the validate button
5. Either the same value or another value fails the lookup function, but this time a "Run-time error '-2147220503 (800403e9)' Automation error is thrown
6. pressing debg takes me to the err.raise line
Full procedure shown below - Any advice welcome
Public Function DMLookup(LookupName As String, SearchValue As String, SearchCol As Integer, ResultCol As Integer, Optional IsCaseSensitive As Variant)
Dim LoopCounter As Long
Dim LookupFound As Boolean
Dim LookupToBeUsed As Variant
Dim result As String
If IsMissing(IsCaseSensitive) Then
IsCaseSensitive = False
End If
If gEnableErrorHandling Then On Error GoTo DMLookup_Error
LookupFound = False
LookupToBeUsed = Application.Names(LookupName).RefersToRange.Value
For LoopCounter = 1 To UBound(LookupToBeUsed, SearchCol)
'If its not a case sensitive check, wrap the check in UCASe to convert both values to upper case
If IsCaseSensitive = False Then
If UCase(LookupToBeUsed(LoopCounter, SearchCol)) = UCase(SearchValue) Then
result = LookupToBeUsed(LoopCounter, ResultCol)
LookupFound = True
Exit For
End If
Else
'If it is a case sensitive search, do a direct compare of the values
If LookupToBeUsed(LoopCounter, SearchCol) = SearchValue Then
result = LookupToBeUsed(LoopCounter, ResultCol)
LookupFound = True
Exit For
End If
End If
Next LoopCounter
If LookupFound = False Then
Err.Raise (vbObjectError + 1001)
End If
DMLookup = result
DMLookup_Exit:
On Error GoTo 0
Exit Function
DMLookup_Error:
If Application.ScreenUpdating = False Then Application.ScreenUpdating = True
Select Case Err.Number
Case vbObjectError + 1001
MsgBox "The lookup for " & SearchValue & "withing the lookup name " & LookupName & _
" has failed - This is often due to a copy and paste error where 1 lookup has been overwritten by another " & _
" and so the validation stage passes initially, but is detected in the generation stage and so fails." & _
vbCrLf & vbCrLf & "This is a critical error and any further processing will stop - Please correct the " & _
"error before retrying", _
vbCritical, "Error in function DMLookup of Module DeveloperToolKit"
Case Else
MsgBox "An unexpected error has occured, please contact Design with the below error details." & _
vbCrLf & "Module = Module DeveloperToolKit" & _
vbCrLf & "Procedure = DMLookup" & _
vbCrLf & "Line = " & Erl & _
vbCrLf & "Error Code = " & Str$(Err.Number) & _
vbCrLf & "Error Text = " & Err.Description, vbCritical, "Error in in procedure DMLookup of Module DeveloperToolKit"
End Select
End 'Stop everything
End Function
Bookmarks