473,725 Members | 2,248 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(C ancel As Integer)
Dim db As DAO.Database, rs As DAO.Recordset
Dim intnewrec As Integer

Set db = CurrentDb()
Set rs = db.OpenRecordse t("Select PolicyNumber FROM tblClientSurvey s
WHERE Policynumber ='" & Me.txtPolicyNum ber & "'")

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 23073

"BerkshireG uy" <bd*****@yahoo. com> schreef in bericht news:11******** *************@g 43g2000cwa.goog legroups.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(C ancel As Integer)
Dim db As DAO.Database, rs As DAO.Recordset
Dim intnewrec As Integer

Set db = CurrentDb()
Set rs = db.OpenRecordse t("Select PolicyNumber FROM tblClientSurvey s
WHERE Policynumber ='" & Me.txtPolicyNum ber & "'")

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(C ancel As Integer)
Dim db As DAO.Database, rs As DAO.Recordset
Dim intnewrec As Integer

Set db = CurrentDb()
Set rs = db.OpenRecordse t("Select PolicyNumber FROM tblClientSurvey s
WHERE Policynumber ='" & Me.txtPolicyNum ber & "'")

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("*","tbl ClientSurveys", "policynumber=' "&txtpolicynumb er
&"'")>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******** *************@d reader2.news.ti scali.nl:

"BerkshireG uy" <bd*****@yahoo. com> schreef in bericht
news:11******** *************@g 43g2000cwa.goog legroups.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(C ancel As Integer)
Dim db As DAO.Database, rs As DAO.Recordset
Dim intnewrec As Integer

Set db = CurrentDb()
Set rs = db.OpenRecordse t("Select PolicyNumber FROM
tblClientSurvey s WHERE Policynumber ='" & Me.txtPolicyNum ber &
"'")

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(C ancel As Integer)
Dim db As DAO.Database, rs As DAO.Recordset

Set db = CurrentDb()
Set rs = db.OpenRecordse t("Select PolicyNumber _
FROM tblClientSurvey s _
WHERE Policynumber ='" & Me!txtPolicyNum ber & "'")

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 tblClientSurvey s _
WHERE Policynumber ='" & Me!txtPolicyNum ber & "'" _
AND PolicyNumberID <> " & Me!PolicyNumber ID

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
"BerkshireG uy" <bd*****@yahoo. com> wrote in
news:11******** *************@g 49g2000cwa.goog legroups.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!txtPolicyNum ber.Locked = False

Then, in the form's AfterUpdate event:

Me!txtPolicyNum ber.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
2833
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 Contact_telephone___BeforeUpdate(Cancel As Integer) Dim rs As DAO.Recordset Dim iAns As Integer Set rs = Me.RecordsetClone rs.FindFirst " = '" & Me! & "'" If Not rs.NoMatch Then
7
1804
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 tables being used are tblPersonal and tblListData. tblPersonal contains names, SSNs, etc. SSN is the PrimaryKey. tblListData is keyed on the combination of SSN and ExamNum. In tblListData, an SSN can be paired with more than one ExamNum, but the
6
1868
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 system when adding a new record? Preferably straight after entering forename & surname so user does not waste time entering the rest of the info. Thanks
6
2731
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 primary key violation and then handle it
6
9840
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 that information already exists in the database and cancels the record. Essentially: After admin enters FirstName, LastName and DOB, the database checks to see if all this information is unique to that one record. If they are, then proceed,...
2
2118
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; mysql_connect($host,$username,$password); @mysql_select_db($database) or die( "Unable to select database");
1
3574
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; mysql_connect($host,$username,$password); @mysql_select_db($database) or die( "Unable to select database");
2
3119
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 record. My code inserts records one by one using addnew-updatebatch. If there is a duplicate in the db, it will display "already exists" and if it is not in the db it will display "record added". Below is my asp code I found on the net and...
1
1747
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 issues checking if the number of rows returned from my Select is equal 0? Also i'm wondering should i be checking for an exception and open/closing my connections each time i make a call to my DB or is the way i have it coded below OK?? Any input...
0
8889
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9401
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9257
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9179
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9116
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6011
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4784
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2637
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2157
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.