J_Fo
New member
- Local time
- Today, 13:05
- Joined
- Jan 3, 2024
- Messages
- 6
Hello -
I am attempting to add records to a table only if the record does not exist based on the "Fingerprint" field. When I step through the code, in the fuction FindRecord at the bottom of my code snippet, when it gets to FindRecord = Not rsDestination.NoMatch I get a rsDestination.NoMatch True or False, depending if the record is found. The record is still being added when a match is found. Any help in resolving what I'm doing wrong would be appreciated.
Destination Table:
ID "Fingerprint" FP_ID
75 J002-601-18-17 10001
76 J002-601-18-30 10002
77 J002-601-18-99 10003
78 J002-601-18-19 10004
79 J002-601-18-30 10005
80 J002-601-18-9 10006
I am attempting to add records to a table only if the record does not exist based on the "Fingerprint" field. When I step through the code, in the fuction FindRecord at the bottom of my code snippet, when it gets to FindRecord = Not rsDestination.NoMatch I get a rsDestination.NoMatch True or False, depending if the record is found. The record is still being added when a match is found. Any help in resolving what I'm doing wrong would be appreciated.
Destination Table:
ID "Fingerprint" FP_ID
75 J002-601-18-17 10001
76 J002-601-18-30 10002
77 J002-601-18-99 10003
78 J002-601-18-19 10004
79 J002-601-18-30 10005
80 J002-601-18-9 10006
Code:
Option Compare Database
Option Explicit
Public Sub tryagain()
Dim db As DAO.Database
Dim rsSource As DAO.Recordset
Dim rsDestination As DAO.Recordset
Dim newNumber As Long
' Open the source recordset from the select query
Set db = CurrentDb
Set rsSource = db.OpenRecordset("scheduleID", dbOpenDynaset)
' Open the destination recordset
Set rsDestination = CurrentDb.OpenRecordset("ListScheduleIDtbl", dbOpenDynaset, dbSeeChanges)
' If not found, proceed to add the new record
' Loop through the records in rsSource
Do While Not rsSource.EOF
' 'Check if the record already exists based on "Fingerprint"
If FindRecord(rsDestination, "Fingerprint", rsSource.Fields("Fingerprint").value) Then
'Record already exists, do not add a new one
'Exit Sub
End If
'Set Fingerprint ID field
newNumber = Nz(DMax("FP_ID", "ListScheduleIDtbl"), 10000) + 1
' Add the record to the destination table
rsDestination.AddNew
'For Each fld In rsSource.Fields
'rsDestination.Fields(fld.Name).Value = fld.Value
'Next fld
'Assign claim import fields
rsDestination.Fields("Fingerprint").value = rsSource.Fields("Fingerprint").value
rsDestination.Fields("FP_ID").value = newNumber
rsDestination.Update
'End If
rsSource.MoveNext
Loop
' Clean up
rsDestination.Close
rsSource.Close
Set rsDestination = Nothing
Set rsSource = Nothing
Set db = Nothing
Exit Sub
End Sub
Function FindRecord(rsDestination As DAO.Recordset, Fingerprint As String, value As Variant) As Boolean
' Check if there are any records
If Not rsDestination.EOF Then
rsDestination.FindFirst "Fingerprint" & " = '" & value & "'"
FindRecord = Not rsDestination.NoMatch
Else
' No records found
FindRecord = False
End If
End Function