472,780 Members | 1,364 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,780 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 5261
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,534 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,534 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, 430 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, 330 views)
Jan 15 '07 #10
NeoPa
32,534 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: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth

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.