Hi Allan,
I'm using a nifty piece of code you put on here some time back to do a
duplicate entry check as below. I'm using to check for duplicate names.
However I am getting an error message on this line: Set rs =
db.OpenRecordse t("SELECT ID FROM Contacts WHERE (" & sWhere & ");")
Contacts being the main table. I am using access 2003
The error message states that there are; Too few parameters. Expected 1
I have no idea how to resolve this. Any help would be appreciated. Many
thanks in advance
Iona
Dim sWhere As String
Dim bWarn As Boolean
Dim sMsg As String
Dim iLen As Integer
Dim db As Database
Dim rs As Recordset
Const SEP = "; "
'FistName field.
If IsNull(Me.First Name) Then
bWarn = True
sMsg = "FirstName is blank" & vbCrLf
Else
sWhere = sWhere & "(FirstName = """ & Me.FirstName & """) AND "
End If
'LastName field.
If IsNull(Me.LastN ame) Then
bWarn = True
sMsg = "LastName is blank" & vbCrLf
Else
sWhere = sWhere & "(LastName = """ & Me.LastName & """) AND "
End If
If bWarn Then
sMsg = sMsg & vbCrLf & "Proceed anyway?"
If MsgBox(sMsg, vbYesNo + vbDefaultButton 2) <vbYes Then
Cancel = True
End If
End If
If Not Cancel Then
'Existing record is not a duplicate of itself.
If Not Me.NewRecord Then
sWhere = sWhere & "(ID <" & Me.ContactsID & ") AND "
End If
iLen = Len(sWhere) - 5 'Without trailing " AND ".
If iLen 0 Then
sWhere = Left$(sWhere, iLen)
sMsg = vbNullString
Set db = CurrentDb()
'Open a recordset of duplicates, and loop through them.
Set rs = db.OpenRecordse t("SELECT ID FROM Contacts WHERE (" &
sWhere & ");")
With rs
If .RecordCount 0 Then
Do While Not .EOF
sMsg = sMsg & !ID & SEP
.MoveNext
Loop
'Ask the user if these are duplicates.
sMsg = "Record:" & vbCrLf & Len(sMsg) - Len(SEP) & vbCrLf &
"Continue anyway?"
If MsgBox(sMsg, vbYesNo + vbDefaultButton 2, "Possible
Duplicate") <vbYes Then
Cancel = True
End If
End If
End With
End If
End If
Set rs = Nothing
Set db = Nothing
End Sub
I'm using a nifty piece of code you put on here some time back to do a
duplicate entry check as below. I'm using to check for duplicate names.
However I am getting an error message on this line: Set rs =
db.OpenRecordse t("SELECT ID FROM Contacts WHERE (" & sWhere & ");")
Contacts being the main table. I am using access 2003
The error message states that there are; Too few parameters. Expected 1
I have no idea how to resolve this. Any help would be appreciated. Many
thanks in advance
Iona
Dim sWhere As String
Dim bWarn As Boolean
Dim sMsg As String
Dim iLen As Integer
Dim db As Database
Dim rs As Recordset
Const SEP = "; "
'FistName field.
If IsNull(Me.First Name) Then
bWarn = True
sMsg = "FirstName is blank" & vbCrLf
Else
sWhere = sWhere & "(FirstName = """ & Me.FirstName & """) AND "
End If
'LastName field.
If IsNull(Me.LastN ame) Then
bWarn = True
sMsg = "LastName is blank" & vbCrLf
Else
sWhere = sWhere & "(LastName = """ & Me.LastName & """) AND "
End If
If bWarn Then
sMsg = sMsg & vbCrLf & "Proceed anyway?"
If MsgBox(sMsg, vbYesNo + vbDefaultButton 2) <vbYes Then
Cancel = True
End If
End If
If Not Cancel Then
'Existing record is not a duplicate of itself.
If Not Me.NewRecord Then
sWhere = sWhere & "(ID <" & Me.ContactsID & ") AND "
End If
iLen = Len(sWhere) - 5 'Without trailing " AND ".
If iLen 0 Then
sWhere = Left$(sWhere, iLen)
sMsg = vbNullString
Set db = CurrentDb()
'Open a recordset of duplicates, and loop through them.
Set rs = db.OpenRecordse t("SELECT ID FROM Contacts WHERE (" &
sWhere & ");")
With rs
If .RecordCount 0 Then
Do While Not .EOF
sMsg = sMsg & !ID & SEP
.MoveNext
Loop
'Ask the user if these are duplicates.
sMsg = "Record:" & vbCrLf & Len(sMsg) - Len(SEP) & vbCrLf &
"Continue anyway?"
If MsgBox(sMsg, vbYesNo + vbDefaultButton 2, "Possible
Duplicate") <vbYes Then
Cancel = True
End If
End If
End With
End If
End If
Set rs = Nothing
Set db = Nothing
End Sub
Comment