F
FiscalMike
I am using a form for data entry to a table. Key is social security number.
The majority of the time the data entered will be a new record and will be
added to the table - no problem. However, occasionally the data to be
entered is actually an update to an existing record (data entry person cannot
distinguish between new or update). I would like to use the SSN field
(primary key) as soon as it is entered, to determine if the record already
exists, and , if so, populate the record's fields on the form. If the key
doesn't already exist, then allow data entry to continue normally.
I can successfully use DLOOKUP to interrogate the table, but have been
unsuccessful finding a way to tell Access to not attempt to add the record.
Access will not allow the update because it will create a duplicate key. I
have tried executing the code on the "before update" and the "on exit"
events but with no success. My code follows.
Suggestions appreciated!
Private Sub SearchSSN_Exit(Cancel As Integer)
Dim x As Variant
x = DLookup("[Sender SSN]", "SAF_Sender", "[Sender SSN]= '" _
& Forms!SAF_Sender_T!searchSSN & "'")
On Error GoTo CustID_Err
If Not IsNull(x) Then
Beep
MsgBox "The Sender has already been added. "
vbOKCancel , "Sender Already Entered"
DoCmd.CancelEvent
DoCmd.FindRecord x
End If
CustID_Exit:
Exit Sub
CustID_Err:
MsgBox Error$
Resume CustID_Exit
End Sub
The majority of the time the data entered will be a new record and will be
added to the table - no problem. However, occasionally the data to be
entered is actually an update to an existing record (data entry person cannot
distinguish between new or update). I would like to use the SSN field
(primary key) as soon as it is entered, to determine if the record already
exists, and , if so, populate the record's fields on the form. If the key
doesn't already exist, then allow data entry to continue normally.
I can successfully use DLOOKUP to interrogate the table, but have been
unsuccessful finding a way to tell Access to not attempt to add the record.
Access will not allow the update because it will create a duplicate key. I
have tried executing the code on the "before update" and the "on exit"
events but with no success. My code follows.
Suggestions appreciated!
Private Sub SearchSSN_Exit(Cancel As Integer)
Dim x As Variant
x = DLookup("[Sender SSN]", "SAF_Sender", "[Sender SSN]= '" _
& Forms!SAF_Sender_T!searchSSN & "'")
On Error GoTo CustID_Err
If Not IsNull(x) Then
Beep
MsgBox "The Sender has already been added. "
vbOKCancel , "Sender Already Entered"
DoCmd.CancelEvent
DoCmd.FindRecord x
End If
CustID_Exit:
Exit Sub
CustID_Err:
MsgBox Error$
Resume CustID_Exit
End Sub