473,385 Members | 1,357 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Avoiding duplicate records

Hi All,

What i am trying to do is avoid inserting duplicate records into a database. I am inserting the records using a webpage but when i enter a flight number(primary key) that already exists i get an error and system crashes.

What i want to do is if a duplicate record is about to be entered then redirect the user to another page. Is this possible

Thanks in advance

Matthew
Mar 2 '08 #1
2 1548
DrBunchman
979 Expert 512MB
Hi Matthew,

You can check whether a primary exists already easily enough by running a query against the table before insertion. Something like:

Expand|Select|Wrap|Line Numbers
  1.  Dim strSql 
  2. Dim oConn
  3. Dim oRS
  4.  
  5. strSQL = "SELECT COUNT(*) AS RecordCount FROM Table1 WHERE FlightNumber = '" & strFlightNumber & "' "
  6.  
  7. oConn.Open oRS, strSql
  8.  
If the value of oRS("RecordCount") is 0 then no record exists and you can continue with the insert. If it does then you can response.redirect the user to another page as required.

I presume you are not using an automatically generated primary key (a GUID or IDENTITY field) as if you were there would be no chance of a duplicate primary key occurring

Hope this helps,

Dr B
Mar 3 '08 #2
jhardman
3,406 Expert 2GB
I use a similar approach to Dr B. I Open a recordset with the exact criteria I am looking for - If the recordset is empty, I add a new record with that info, otherwise I don't. The code looks something like this:
Expand|Select|Wrap|Line Numbers
  1. query = "SELECT * FROM myTable WHERE firstName = '"
  2. query = query & fname & "' AND lastName = '"
  3. query = query & lname & "' AND phoneNumber = "
  4. query = query & phNumber
  5.  
  6. objRS.Open query, objConn, AdOpenDynamic, adLockOptimistic
  7.  
  8. if objRS.eof then
  9.    objRS.addNew
  10.    objRS("firstName") = fname
  11.    objRS("lastName") = lname
  12.    objRS("phoneNumber") = phNumber
  13.    objRS.update
  14. else
  15.    response.write "Hey, Idiot!  This record is already in the db."
  16. end if
Mar 5 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: ms | last post by:
Access 2000: I am trying to delete duplicate records imported to a staging table leaving one of the duplicates to be imported into the live table. A unique record is based on a composite key of 3...
12
by: google_groups3 | last post by:
Hi all. I currently have 2 text files which contain lists of file names. These text files are updated by my code. What I want to do is be able to merge these text files discarding the...
2
by: Carroll | last post by:
I'm looking for a way in SQL to find duplicate records in a single table, that are the same based on 3 columns, regardless of what is in the other columns in the duplicate records. I would like to...
0
by: B.N.Prabhu | last post by:
Hi, I have a DataTable with several rows. Its having 20 Columns. when i click the Insert button then i have to check the Database Rows. Whether these new rows are already available in the...
2
by: nethravathy | last post by:
Hi, The following table namely elcbtripselect contains 5147 records.I want to know wether this table contains duplicate records or not. I tried with following query 1)SELECT...
4
by: Thomas Arthur Seidel | last post by:
Hello to all, I have a small or big problem with a customer data base, where during a change of system we might have created duplicate records. This should be easy to find, you might think, but,...
2
by: nomvula | last post by:
hi guys i need some help to duplicate records on my form datasheet: here's the example of my form results: ClientLookup DateCaptured ForecastDate Description ForecastQuantity Forecast Actual UJ...
6
by: Dilip1983 | last post by:
Hi All, I want to delete duplicate records from a large table. There is one index(INDEX_U1) on 4 columns(col1,col2,col3,col4) which is in unusable state. First of all when i tried to rebuild...
1
by: xraive | last post by:
I have a problem with this. Currently I am trying Allen's code and i am not successful. Current Design Table1 (Main Form) TravelID (PK) ApprovedBY EntreredBy BudgetCode ExpenseCode
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.