472,364 Members | 1,575 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,364 software developers and data experts.

Checking if record exists

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
TIA,
Brian

Nov 13 '05 #1
5 22788

"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)

You need to look for rs.EOF here...

Also I would restructure the code if you want this test for new records only ...
If Me.NewRecord then
'only then open a recordset to look if the value exists
End if

You don't need the Me.undo because the 'cancel = true' 'locks' the user in the field ...
Also: what to do when a user edits an existing record?

Arno R
Nov 13 '05 #2
BerkshireGuy wrote:
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


NoMatch becomes valid only after a Find issued to the recordset.

In this case I'd skip the recordset altogether, and try

if dcount("*","tblClientSurveys","policynumber='"&txt policynumber
&"'")>0 then
'your things
end if

Oh, and about the BeforeUpdate thing: would it be wise to forbid edits
on the policynumber when not in a new record? In that case it folds
together nicely:

if newrecord then
if dcount(that thing)>0 then
msgbox "This number is already in use."
cancel = true
end if
else
cancel=true
msgbox "You shall not edit an existing policy number!"
endif
--
Bas Cost Budde, Holland
http://www.heuveltop.nl/BasCB/msac_index.html

Nov 13 '05 #3
Bas,

Ok I have that code, but there is a little issue:

If I go to an existing record and try to change the policy number, it
says "You shall not edit an existing policy number." Ok all good. But
if I now try to add a NEW record, I get the same message.

Any ideas?

-Brian

Nov 13 '05 #4
"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
Nov 13 '05 #5
"BerkshireGuy" <bd*****@yahoo.com> wrote in
news:11*********************@g49g2000cwa.googlegro ups.com:
Ok I have that code, but there is a little issue:

If I go to an existing record and try to change the policy number,
it says "You shall not edit an existing policy number." Ok all
good. But if I now try to add a NEW record, I get the same
message.


I have a philosophical problem with allowing you to edit the field
in the first place, only to then pop up a message telling you that
you can't. I would recommend that if you can't change a policy
number that the field be entirely locked when not adding a new
record.

Two possibilities:

If you've got a command button to create a new record, add to it:

Me!txtPolicyNumber.Locked = False

Then, in the form's AfterUpdate event:

Me!txtPolicyNumber.Locked = True

If you don't have a dedicated command button for adding a new
record, then use the form's OnCurrent event to unlock the policy
number.

You may also be able to lock the control in the control's own
AfterUpdate event, or, you could do it in the code for a SAVE RECORD
button.

But I htink it's better design to not allow people the possibility
of editing a policy number in the first place. That way, your
BeforeUpdate code can be much simpler.

Also, see my reply to Arno for some considerations about choosing
your Primary Key.

--
David W. Fenton http://www.bway.net/~dfenton
dfenton at bway dot net http://www.bway.net/~dfassoc
Nov 13 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: R Bolling | last post by:
I am using a routine to check to see if a phone number (PK) has alread been entered, and takes the user to that record if it is found -- as follows: Private Sub...
7
by: RBohannon | last post by:
I'm using A2K. I'm inputing data from a text file into my DB, and I need to check for the data already existing in the DB. If it's already in the DB, I don't want to reenter it. The two...
6
by: John | last post by:
Hi We have a staff database and need to make sure that the same staff is not entered twice. Is there a way for Access to flag if combination of forename and surname fields is already in the...
6
by: Joey Lee | last post by:
Hi, I was wondering which is the best way of checking if a record exist in the database before inserting. There are quite a few ways I have seen. Here are some... 1> Insert and hit a...
6
by: TonyMacaroni | last post by:
Hi everyone, I have a database I'm helping set up for a Charity - I'd like to create a query where if the admin team tries to enter a member twice, a pop up box will inform them that a member with...
2
by: mookid | last post by:
Hello, I am new to PHP so I have done a research on how to check if an entry exists on the table. I came up with the following code: include("dbinfo.inc.php"); $Name=$_POST; $Code=$_POST;...
1
by: Edwina Rothschild | last post by:
Hello, I am new to PHP so I have done a research on how to check if an entry exists on the table. I came up with the following code: include("dbinfo.inc.php"); $Name=$_POST; $Code=$_POST;...
2
by: hakkatil | last post by:
Hi to all, I have a page that inserts excel sheet to access database. I am using asp. What I want to do is to check the inserting record if it is in the database. Basicly checking the dublicate...
1
by: Orbie | last post by:
Hi All, I'm new to VB.NET and i'm looking for some help with my Windows Form. I need to check if a Commodity entered into (TextBox1.Text) already exists on my table before i insert it. I'm having...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
1
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.