473,387 Members | 1,504 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,387 software developers and data experts.

how to prevent duplicate records?

i have created a function to input data from four textboxes into a table. how do i prevent duplicate records of the same mth/Yr into the table? editing of the data in the table with reference to the mt/Yr is allowed, but how to prevent duplicate records?

Expand|Select|Wrap|Line Numbers
  1. Public Sub PutInMonthlyRecords()
  2.  
  3.     Dim sql As String
  4.     Dim Db As DAO.Database
  5.     Dim rs As DAO.Recordset
  6.     Dim f As Form
  7.     Dim MthYr As String
  8.  
  9.     Set f = Forms!frmQpi
  10.  
  11.     MthYr = f("txtMthYr")
  12.  
  13.     sql = "SELECT * FROM [tblQpiMonthly] WHERE (([tblQpiMonthly].[Input MthYr] = 'MthYr' ));"
  14.  
  15.     Set Db = CurrentDb()
  16.     Set rs = Db.OpenRecordset(sql)
  17.  
  18.  
  19.         If rs.RecordCount = 0 Then
  20.             If (f!txtTotalPF) = 0 Then
  21.                 Exit Sub
  22.             End If
  23.  
  24.             rs.AddNew
  25.                 rs![Input MthYr] = f("txtMthYr")
  26.                 rs![Monthly TotalPF] = f("txtTotalPF")
  27.                 rs![Monthly Rejection] = f("txtTotalAvoid")
  28.                 rs![Monthly TotalAvoid] = f("txtRejection")
  29.  
  30.             rs.Update
  31.  
  32.       Else
  33.             If (f!txtTotalPF) = 0 Then
  34.                 rs.Delete
  35.  
  36.                 Exit Sub
  37.               End If
  38.  
  39.  
  40.             rs.Delete
  41.             rs.AddNew
  42.                 rs![Input MthYr] = f("txtMthYr")
  43.                 rs![Monthly TotalPF] = f("txtTotalPF")
  44.                 rs![Monthly Rejection] = f("txtTotalAvoid")
  45.                 rs![Monthly TotalAvoid] = f("txtRejection")
  46.  
  47.             rs.Update
  48.  
  49.         End If
  50.  
  51.  
  52. Db.Close
  53.  
  54.  
Jan 13 '07 #1
11 5313
ADezii
8,834 Expert 8TB
i have created a function to input data from four textboxes into a table. how do i prevent duplicate records of the same mth/Yr into the table? editing of the data in the table with reference to the mt/Yr is allowed, but how to prevent duplicate records?

Expand|Select|Wrap|Line Numbers
  1. Public Sub PutInMonthlyRecords()
  2.  
  3.     Dim sql As String
  4.     Dim Db As DAO.Database
  5.     Dim rs As DAO.Recordset
  6.     Dim f As Form
  7.     Dim MthYr As String
  8.  
  9.     Set f = Forms!frmQpi
  10.  
  11.     MthYr = f("txtMthYr")
  12.  
  13.     sql = "SELECT * FROM [tblQpiMonthly] WHERE (([tblQpiMonthly].[Input MthYr] = 'MthYr' ));"
  14.  
  15.     Set Db = CurrentDb()
  16.     Set rs = Db.OpenRecordset(sql)
  17.  
  18.  
  19.         If rs.RecordCount = 0 Then
  20.             If (f!txtTotalPF) = 0 Then
  21.                 Exit Sub
  22.             End If
  23.  
  24.             rs.AddNew
  25.                 rs![Input MthYr] = f("txtMthYr")
  26.                 rs![Monthly TotalPF] = f("txtTotalPF")
  27.                 rs![Monthly Rejection] = f("txtTotalAvoid")
  28.                 rs![Monthly TotalAvoid] = f("txtRejection")
  29.  
  30.             rs.Update
  31.  
  32.       Else
  33.             If (f!txtTotalPF) = 0 Then
  34.                 rs.Delete
  35.  
  36.                 Exit Sub
  37.               End If
  38.  
  39.  
  40.             rs.Delete
  41.             rs.AddNew
  42.                 rs![Input MthYr] = f("txtMthYr")
  43.                 rs![Monthly TotalPF] = f("txtTotalPF")
  44.                 rs![Monthly Rejection] = f("txtTotalAvoid")
  45.                 rs![Monthly TotalAvoid] = f("txtRejection")
  46.  
  47.             rs.Update
  48.  
  49.         End If
  50.  
  51.  
  52. Db.Close
  53.  
  54.  
Since you are allowing editing at the Table level, I feel as though your best option may be : in tblOptMonthly, set the Indexed property of the [Input MthY] Field to Indexed = Yes(No Duplicates). In the PutInMonthlyRecords() Routine, trap the specific Duplicate Record Error that will now be generated if you try to add a Duplicate Record on the [Input MthY] Field via VCBA code, and Exit the Sub-Routine. The Record should not be Appended since rs.Update will not be successful in this scenario.

You are now covered in both situations:
1) Editing a Record and creating a Duplicate Record based on the [Input MthY] Field.
2) Adding a Record via the Sub-Routinre and creating a Duplicate Record on the [Input MthY] Field.
Jan 13 '07 #2
NeoPa
32,556 Expert Mod 16PB
Nice answer ADezii.
Ariel,
If you feel that any part of this resolution is too complicated for you to implement - don't worry, just post back explaining which parts you find complicated and ADezii (or someone else even if he's too busy) will be able to help you through it.
Error handling is a very important part of VBA code but some people avoid using it. Don't let that frighten you off this solution.
Best of luck.

MODERATOR.
Jan 13 '07 #3
Nice answer ADezii.
Ariel,
If you feel that any part of this resolution is too complicated for you to implement - don't worry, just post back explaining which parts you find complicated and ADezii (or someone else even if he's too busy) will be able to help you through it.
Error handling is a very important part of VBA code but some people avoid using it. Don't let that frighten you off this solution.
Best of luck.

MODERATOR.
thank you. i am new to programming...i don't understand a single thing. any similiar sample programs where i can get ideas from?
Jan 14 '07 #4
NeoPa
32,556 Expert Mod 16PB
Ariel,
Please reread my earlier post. The wording was not random.
Unless I/we have a particular point to help with, we are looking at saying that the answer is not good enaough and starting again.
That is certainly not the case here, and you have a choice to use it or not. We can help you but I am certainly not prepared to look elsewhere until I'm sure you've given the answer a fair try. As I say, we can help with that. I am certainly not offering to do work for you, simply to help you do the work yourself.

MODERATOR.
Jan 14 '07 #5
Killer42
8,435 Expert 8TB
From the original post...
Expand|Select|Wrap|Line Numbers
  1.     Set f = Forms!frmQpi
  2.     MthYr = f("txtMthYr")
  3.     sql = "SELECT * FROM [tblQpiMonthly] WHERE (([tblQpiMonthly].[Input MthYr] = 'MthYr' ));"
  4.  
Can someone please explain this to me? Some of the syntax used in this routine is unfamiliar to me, so don't think that I'm telling you anything is wrong - I just didn't quite follow it.

The main problem I have is with the underlined sections. The way I read it, this is supposed to be matching on a month and year value found in textbox txtMthYr, but is in fact searching for the literal string "MthYr".

Am I misreading this?
Jan 15 '07 #6
Killer42
8,435 Expert 8TB
It took me a while to realise something. In hindsight, if the WHERE clause is wrong, this might never find a match, and hence give rise to the whole issue of creating duplicates.
Jan 15 '07 #7
here is the function which i re-edited. the codes that were not affecting the program have been deleted.
Expand|Select|Wrap|Line Numbers
  1. Public Sub PutInMonthlyRecords()
  2.  
  3.     Dim sql As String
  4.     Dim Db As DAO.Database
  5.     Dim rs As DAO.Recordset
  6.     Dim f As Form
  7.  
  8.  
  9.     Set f = Forms!frmQpi
  10.     Set Db = CurrentDb()
  11.  
  12.  
  13.     sql = "SELECT * FROM [tblQpiMonthly];"
  14.  
  15.     Set rs = Db.OpenRecordset(sql)
  16.  
  17.             If (f!txtTotalPF) = 0 Then
  18.                Exit Sub
  19.             End If
  20.  
  21.             If (f!txtTotalPF) = 0 Then
  22.                   Exit Sub
  23.             End If
  24.  
  25.             If (f!txtTotalPF) = 0 Then
  26.                  Exit Sub
  27.              End If
  28.  
  29.                 rs.AddNew
  30.  
  31.                 rs![Input MthYr] = f("txtMthYr")
  32.                 rs![Monthly TotalPF] = f("txtTotalPF")
  33.                 rs![Monthly Rejection] = f("txtTotalAvoid")
  34.                 rs![Monthly TotalAvoid] = f("txtRejection")
  35.  
  36.             rs.Update
  37.  
  38.  
  39. Db.Close
  40.  
  41. End Sub
  42.  
Basically, when the record is not found in the table, it will add the new record. if the record is found in the table, it should edit the record instead of adding the same record into the table.
Attached Images
File Type: jpg table.JPG (38.2 KB, 432 views)
Jan 15 '07 #8
the form looks this way (see attachment0
Jan 15 '07 #9
the form looks this way (see attachment)
Attached Images
File Type: jpg form1.JPG (31.4 KB, 335 views)
Jan 15 '07 #10
NeoPa
32,556 Expert Mod 16PB
Ariel,
You need to look again at ADezii's post (#2) and try to work your way through it. We can help with specific questions but not in the form of "I'm lost - please tell me the whole thing again in easier words."
You should do what you can then, when you get stuck, post exactly what your problems is and we can help you through it - with reference to post #2 that we can all see.
Does this make sense to you?
If you abort from an answer because it's a little complicated to understand, and try a different tack, then we could be here forever as you keep chopping and changing. This is unfair on our experts so I don't want to see it happen.
If you can go through it as I've described above, then we can help you.

MODERATOR.
Jan 15 '07 #11
here is the code for the statement from this:
Expand|Select|Wrap|Line Numbers
  1. rs.AddNew
  2.  
  3.                 rs![Input MthYr] = f("txtMthYr")
  4.                 rs![Monthly TotalPF] = f("txtTotalPF")
  5.                 rs![Monthly Rejection] = f("txtTotalAvoid")
  6.                 rs![Monthly TotalAvoid] = f("txtRejection")
  7.  
  8.                   rs.Update
  9.  
to this:
Expand|Select|Wrap|Line Numbers
  1. While Not rs.EOF
  2.                  If Not rs.NoMatch Then
  3.                    Exit Sub
  4.  
  5.                 rs.AddNew
  6.  
  7.                 rs![Input MthYr] = f("txtMthYr")
  8.                 rs![Monthly TotalPF] = f("txtTotalPF")
  9.                 rs![Monthly Rejection] = f("txtTotalAvoid")
  10.                 rs![Monthly TotalAvoid] = f("txtRejection")
  11.  
  12.                   rs.Update
  13.               Next
  14.                   rs.MoveNext
  15.              Wend
  16.  
Jan 15 '07 #12

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

Similar topics

9
by: Catherine Jo Morgan | last post by:
Can I set it up so that a certain combination of fields can't contain the same entries, on another record? e.g. a combination of FirstName/LastName/address? Or FirstName/LastName/phone? Or...
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...
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...
6
by: teser3 | last post by:
I have my PHP inserting into Oracle 9i. But how do I prevent duplicate record entries? I only have 3 fields in the insert in the action page: CODE <?php $c=OCILogon("scott", "tiger",...
4
by: jbrumbau | last post by:
Hello, I have been successfully using a database I've created for several months to populate an equipment list for a project we've been working on. However, the form has recently stopped working...
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...
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
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...

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.