I have an error message on a form that checks for an existing SSN on the BeforeUpdate event of a text box. If it already exists in the database, the user is given a message box to this effect, and has the option to go to the existing record. I'm using this example code here. My code is below.
Private Sub txtSocialSecurityNumber_BeforeUpdate(Cancel As Integer)
'check for existing SSN
Dim SSN As String
Dim strLinkCriteria As String
Dim rsc As Recordset
Set rsc = Me.RecordsetClone
SSN = Me.txtSocialSecurityNumber.Value
strLinkCriteria = "[SocialSecurityNumber] = " & "'" & SSN & "'"
'look for duplicates
If DCount("SocialSecurityNumber", "Person", LinkCriteria) > 0 Then
'Undo duplicate entry
Me.Undo
'error message
intResponse = MsgBox("Social Security Number " & SSN & " already exists in database." & _
vbCrLf & vbCrLf & "Would you like to view the record?", vbYesNo, "Duplicate SSN")
If intResponse = vbYes Then
'go to record
rsc.FindFirst strLinkCriteria
Me.Bookmark = rsc.Bookmark
ElseIf intResponse = vbNo Then
Exit Sub
End If
End If
Set rsc = Nothing
End Sub
According to this code, and several other examples I have looked up, it seems like I'm doing everything right, but I must not be because when I try to run the code and go to the existing record, I get the error "Run-time error '424': Object Required". When I debug, the row Thank you to Sergey S. for pointing out the spelling error that fixed this section.rst.FindFirst strLinkCriteria
is highlighted, and hovering over it gives me the text strLinkCriteria = "[SocialSecurityNumber] = '123456789'"
(123456789 is a known sample SSN in my database).
When I tell the message box to go to the record with the existing SSN, it gives me Run-time error '3021': No current record
. Debug highlights the line Me.Bookmark = rsc.Bookmark
with the message rsc.Bookmark = <No current record.>
. So it sounds like I'm not assigning the bookmark correctly.
I've never used bookmarks before, so I'm not really sure what I'm doing wrong here. Any help would be appreciated.