473,545 Members | 1,744 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to prevent duplicate records?

6 New Member
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 5328
ADezii
8,834 Recognized Expert Expert
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 PutInMonthlyRec ords() 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,563 Recognized Expert Moderator MVP
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
ariel81
6 New Member
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,563 Recognized Expert Moderator MVP
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 Recognized Expert Expert
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 Recognized Expert Expert
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
ariel81
6 New Member
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, 433 views)
Jan 15 '07 #8
ariel81
6 New Member
the form looks this way (see attachment0
Jan 15 '07 #9
ariel81
6 New Member
the form looks this way (see attachment)
Attached Images
File Type: jpg form1.JPG (31.4 KB, 336 views)
Jan 15 '07 #10

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

Similar topics

9
14562
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 FirstName/LastName/email? Or is it possible to allow this but to throw up an alert message? Warning that this person is probably already in the database?...
2
4978
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 fields (vehicleID, BattID, and ChgHrs). VehicleID and BattID are a TEXT datatype and ChrHrs are a number(long int.) datatype. Since records to be...
2
28866
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 keep both records (or it could be more than 2 as well) where duplicate records are found. Also, I am interested in selecting all columns from the...
0
2097
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 Database. If its there, then i need to seperate the Duplicate Records Based upon 4 columns(EmployeeID, ProjectName, ProjectType, StartTime -- should be...
2
2053
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 elcbtripselect.ELCBTRIP_voltsMIN, elcbtripselect.ELCBTRIP_voltsMAX, elcbtripselect.ELCBTrip_is_partwinding, elcbtripselect.ELCBTrip_is_ydelta,...
6
4530
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", "orcl"); if ( ! $c ) { echo "Unable to connect: " . var_dump( OCIError() );
4
2148
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 for some unknown reason. I've been having a problem recently where one table (Project Info) keeps auto-incrementing when I try to create a new record...
2
3999
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 18-Apr-08 01-Mar-08 Fees: Asset 1 R 31,200.00 R 31,200.00 NMBM 22-Apr-08 23-Mar-08 P-MI (E) 07/2006 3 R 47,485 R 38,849 i have 200 records deplayed...
1
7252
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
7396
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7805
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...
1
7413
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...
0
7751
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...
1
5323
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3449
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3440
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1012
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
700
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...

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.