"Arno R" <ar***********@tiscali.nl> wrote in
news:42*********************@dreader2.news.tiscali .nl:
"BerkshireGuy" <bd*****@yahoo.com> schreef in bericht
news:11*********************@g43g2000cwa.googlegro ups.com... Hello everyone,
I have a bond form that a user uses to enter data.
One of my fields, is PolicyNumber. I added some code on the
Before Update event of txtPolicyNumber that checks to see if that
policy number is in the system when entering a new record. If it
is, I want it to display a message and go back to the policy
number field.
I've tried this code on a policy number that is not in the table
and
it keeps informing me that it is in the system.
Here is the code:
Private Sub txtPolicyNumber_BeforeUpdate(Cancel As Integer)
Dim db As DAO.Database, rs As DAO.Recordset
Dim intnewrec As Integer
Set db = CurrentDb()
Set rs = db.OpenRecordset("Select PolicyNumber FROM
tblClientSurveys WHERE Policynumber ='" & Me.txtPolicyNumber &
"'")
intnewrec = Me.NewRecord
If rs.NoMatch = False And intnewrec = True Then
MsgBox "Policy Number Already Exists!"
Cancel = True
Me.Undo
End If
End Sub
NoSeek = NoFind = NoMatch ???? (Look up NoMatch in Help)
Why not just check rs.RecordCount. If it's 0 then there aren't any
rcords, and you can cance:
Private Sub txtPolicyNumber_BeforeUpdate(Cancel As Integer)
Dim db As DAO.Database, rs As DAO.Recordset
Set db = CurrentDb()
Set rs = db.OpenRecordset("Select PolicyNumber _
FROM tblClientSurveys _
WHERE Policynumber ='" & Me!txtPolicyNumber & "'")
If rs.RecordCount <> 0 And Me.Newrecord Then
MsgBox "Policy Number Already Exists!"
Cancel = True
Me.Undo
End If
End Sub
In fact, what I'd do is enable the editing of txtPolicyNumber only
when creating the new record, then you don't have to check the
value.
Of course, if you want to check the value, and the policy number is
the primary key, then you're stuck checking for Me.NewRecord.
However, I would definitely argue against using the policy number as
the primary key. If you use an Autonumber that the user never sees,
you then could use this as your test SQL:
SELECT PolicyNumberID _
FROM tblClientSurveys _
WHERE Policynumber ='" & Me!txtPolicyNumber & "'" _
AND PolicyNumberID <> " & Me!PolicyNumberID
That would exclude the current record from your test for cases where
you're changing the policy number of an existing record.
If the policy number can be changed, then I think that's yet another
reason why you should not use it as a PK.
--
David W. Fenton
http://www.bway.net/~dfenton
dfenton at bway dot net
http://www.bway.net/~dfassoc