S
Sreedhar
I want to check whether the user enters a unique ID (such as CustomerID) each
time he enters new data.
I want to create a public function that accepts the Id to check and returns
true or false, I attempted one but it is not working. Here it is.
Can someone please help ?
Function CheckDuplicate(FieldToCheck As String, WhereToCheck As_
String,NewField As String) As Boolean
On Error GoTo Err_CheckDuplicate
Dim db As DAO.Database
Dim rst As DAO.Recordset
Dim strSQL As String
Dim strWhere As String
CheckDuplicate = True
strSQL = SQLBuilderSelect(WhereToCheck, FieldToCheck)
strWhere = FieldToCheck & "=" & NewField
Set db = CurrentDb
Set rst = db.OpenRecordset(WhereToCheck, dbOpenDynaset)
With rst
Do While Not .EOF
.FindFirst strWhere
If .NoMatch Then
CheckDuplicate = True
Else
CheckDuplicate = False
End If
.MoveNext
Loop
End With
rst.Close
' Clear the objects
Set rst = Nothing
Set db = Nothing
Exit_CheckDuplicate:
Exit Function
Err_CheckDuplicate:
MsgBox err & " : " & Error
Resume Exit_CheckDuplicate
End Function
time he enters new data.
I want to create a public function that accepts the Id to check and returns
true or false, I attempted one but it is not working. Here it is.
Can someone please help ?
Function CheckDuplicate(FieldToCheck As String, WhereToCheck As_
String,NewField As String) As Boolean
On Error GoTo Err_CheckDuplicate
Dim db As DAO.Database
Dim rst As DAO.Recordset
Dim strSQL As String
Dim strWhere As String
CheckDuplicate = True
strSQL = SQLBuilderSelect(WhereToCheck, FieldToCheck)
strWhere = FieldToCheck & "=" & NewField
Set db = CurrentDb
Set rst = db.OpenRecordset(WhereToCheck, dbOpenDynaset)
With rst
Do While Not .EOF
.FindFirst strWhere
If .NoMatch Then
CheckDuplicate = True
Else
CheckDuplicate = False
End If
.MoveNext
Loop
End With
rst.Close
' Clear the objects
Set rst = Nothing
Set db = Nothing
Exit_CheckDuplicate:
Exit Function
Err_CheckDuplicate:
MsgBox err & " : " & Error
Resume Exit_CheckDuplicate
End Function