HillTJ
To train a dog, first know more than the dog..
- Local time
- Today, 03:43
- Joined
- Apr 1, 2019
- Messages
- 731
So, I have the following code that worked fine when the rs,findfirst line was hard coded (now commented out). I wish to make the function more universal by passing the 'find' part as a strwhere that I've built up. But, it doesn't work & returns an invalid argument error 3001. Clearly, there is something wrong with my syntax. Appreciate any help.
Code:
Option Compare Database
Option Explicit
Public Function DoesRecordExist(RecordID As Long, TableName As String, FieldtoMatch As String) As Boolean
'On Error GoTo MyErrorProc:
Dim db As Database
Dim rs As DAO.Recordset
Dim strWhere As String
If IsNull(RecordID) Then
Exit Function
Else
Set db = CurrentDb()
Set rs = db.OpenRecordset(TableName, dbOpenDynaset)
strWhere = FieldtoMatch & " = " & RecordID
Debug.Print strWhere 'to test what the string looks like
Debug.Print "InspectionSectionID = " & RecordID 'to test what the original string looks like
'rs.FindFirst "InspectionSectionID = " & RecordID 'the original expression that works
rs.FindFirst Chr(34) & strWhere & Chr(34) 'invalid argument error 3001
If Not rs.NoMatch Then
DoesRecordExist = True
Else
DoesRecordExist = False
End If
End If
ExitError:
Set rs = Nothing
Set db = Nothing
On Error Resume Next
Exit Function
MyErrorProc:
ErrorHandler:
Call DisplayErrorMessage(Err.Number, "DoesRecordExist")
Resume ExitError
End Function