473,404 Members | 2,178 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,404 software developers and data experts.

Update duplicate record (vba)

283 100+
Hello all,

Im looking for some help with duplicate records. I have a table that gets updated on a daily basis but each day the same record could show up on the table creating a duplicate.

On the table i have a load date field so i can tell which record is old and new. What im trying to do is carry over the info from the new record to the old one keeping the original load date and then delete the new duplicated record. I have writen some code that can remove the duplicates with no problem but i am having trouble getting it to edit the old record. I can get it to work with static data but having trouble getting it to look at the table data.

any help would be greatly appreciated.

thanks in advance,

Slen

here is what i have so far
Expand|Select|Wrap|Line Numbers
  1.  
  2. Dim myDB As DAO.Database
  3. Dim rst As DAO.Recordset
  4. Dim strFind As String
  5.  
  6. Set myDB = CurrentDb
  7. Set rst = myDB.OpenRecordset("tableName", dbOpenDynaset)
  8.  
  9. With rst
  10.   Do While Not .EOF
  11.     If Not IsNull(![ID]) And ![Status] = "Open" Then  'Check for Duplication
  12.         strFind = DCount("*", "tableName", "[Status] ='" & "Open" & "' AND [ID] = '" & ![ID] & "' And [Status2] = '" & ![Status2] & "' And [LoadDate] <=#" & ![LoadDate] & "#") > 1
  13.        If strFind Then
  14.            .Edit
  15.              'here is where im trying to figure out what to insert
  16.            .Update
  17.       End If
  18.     End If
  19.      .MoveNext
  20.   Loop
  21. End With
  22.  
  23. rst.Close
  24. Set rst = Nothing
  25. Set myDB = Nothing
  26.  
  27.  
Mar 27 '13 #1

✓ answered by Seth Schrock

This is totally air code and untested.

Expand|Select|Wrap|Line Numbers
  1. Dim myDB As DAO.Database
  2. Dim rst As DAO.Recordset
  3. Dim intRecCount As Integer
  4. Dim strCriteria As String
  5. Dim varBookmark As Variant
  6. Dim strID As String
  7. Dim strStatus As String
  8. Dim dteLoadDate as Date
  9.  
  10. Set myDB = CurrentDb
  11. Set rst = myDB.OpenRecordset("tableName", dbOpenDynaset)
  12.  
  13. With rst
  14.     Do While Not .EOF
  15.         If Not IsNull(![ID]) And ![Status] = "Open" Then  'Check for Duplication
  16.             strCriteria = "[Status] ='" & "Open" & "' AND [ID] = '" & ![ID] & _
  17.             "' And [Status2] = '" & ![Status2] & "' And [LoadDate] <=#" & ![LoadDate] & "#"
  18.  
  19.             intRecCount = DCount("*", "tableName", strCriteria)
  20.             If intRecCount > 1 Then
  21.                 varBookmark = .Bookmark
  22.                 .FindNext strCriteria
  23.  
  24.                 strID = !ID
  25.                 strStatus = !Status2
  26.                 dteLoadDate = !LoadDate
  27.  
  28.                 .Delete
  29.                 .Bookmark = varBookmark
  30.                .Edit
  31.                     !ID = strID
  32.                     !Status2 = strStatus
  33.                     !LoadDate = dteLoadDate
  34.                .Update
  35.             End If
  36.         End If
  37.         .MoveNext
  38.     Loop
  39. End With
  40.  
  41. rst.Close
  42. Set rst = Nothing
  43. Set myDB = Nothing
I changed the way your criteria worked in your lines 12 & 13 to make it a little easier if you would need to loop through the copying code more than once (would be needed if if were possible for there to be three or more records that were the same to delete two records after copying them to the "master" record). The main part of my code bookmarks the current record, goes to the duplicate record, gets its values, deletes the record, goes back to the original record and then makes the value changes.

12 5538
Seth Schrock
2,965 Expert 2GB
This is totally air code and untested.

Expand|Select|Wrap|Line Numbers
  1. Dim myDB As DAO.Database
  2. Dim rst As DAO.Recordset
  3. Dim intRecCount As Integer
  4. Dim strCriteria As String
  5. Dim varBookmark As Variant
  6. Dim strID As String
  7. Dim strStatus As String
  8. Dim dteLoadDate as Date
  9.  
  10. Set myDB = CurrentDb
  11. Set rst = myDB.OpenRecordset("tableName", dbOpenDynaset)
  12.  
  13. With rst
  14.     Do While Not .EOF
  15.         If Not IsNull(![ID]) And ![Status] = "Open" Then  'Check for Duplication
  16.             strCriteria = "[Status] ='" & "Open" & "' AND [ID] = '" & ![ID] & _
  17.             "' And [Status2] = '" & ![Status2] & "' And [LoadDate] <=#" & ![LoadDate] & "#"
  18.  
  19.             intRecCount = DCount("*", "tableName", strCriteria)
  20.             If intRecCount > 1 Then
  21.                 varBookmark = .Bookmark
  22.                 .FindNext strCriteria
  23.  
  24.                 strID = !ID
  25.                 strStatus = !Status2
  26.                 dteLoadDate = !LoadDate
  27.  
  28.                 .Delete
  29.                 .Bookmark = varBookmark
  30.                .Edit
  31.                     !ID = strID
  32.                     !Status2 = strStatus
  33.                     !LoadDate = dteLoadDate
  34.                .Update
  35.             End If
  36.         End If
  37.         .MoveNext
  38.     Loop
  39. End With
  40.  
  41. rst.Close
  42. Set rst = Nothing
  43. Set myDB = Nothing
I changed the way your criteria worked in your lines 12 & 13 to make it a little easier if you would need to loop through the copying code more than once (would be needed if if were possible for there to be three or more records that were the same to delete two records after copying them to the "master" record). The main part of my code bookmarks the current record, goes to the duplicate record, gets its values, deletes the record, goes back to the original record and then makes the value changes.
Mar 27 '13 #2
Rabbit
12,516 Expert Mod 8TB
What you should do is load the new data into another table. Run a query to update the duplicates. Then run another query to insert the ones not in the main table.
Mar 27 '13 #3
slenish
283 100+
Hi Seth,

You are amazing! Thank you very much for your reply and help! That worked great!

Now i just need to adjust this to delete the original. I saw where you had the line rstShippers.Delete but i removed it because i was getting an error due to it not being declared. Is there a way to create two bookmarks one for old and new then delete the old or delcare the old as OldValue then remove it?


Thanks again!!
Mar 27 '13 #4
zmbd
5,501 Expert Mod 4TB
slenish/Seth:

WOW talk about a Sledge Hammer to put in carpet tack.

The easier method is as Rabbit pointed out.
Mar 27 '13 #5
Seth Schrock
2,965 Expert 2GB
To fix my code, remove the rstShippers so that just .Delete is left. I will fix this in my previous post for future reference.

However, Rabbits idea would probably be easier.
Mar 27 '13 #6
slenish
283 100+
Hi Seth,

That adjustment did the trick. I have noticed an issue due to the .FindNext. Lets say i have two records if the old date is second and the new date is first in the record order than it works but if its the other way around it does not.I also tried .FindFirst ect and i get the same issue.

I tried Rabbits idea but was not having any luck. If either of you could post an example i would apprecaite it.

thanks again,
Slen
Mar 28 '13 #7
Seth Schrock
2,965 Expert 2GB
To solve the order problem, create a query (either a querydef or in VBA) that sorts the data by LoadDate. That will make sure that all the dates will come in the correct order.

I would help you with Rabbit's idea, but after studying it, the only thing that I can come up with is even more complicated than my solution, so I don't think that I would be of much help there.
Mar 28 '13 #8
zmbd
5,501 Expert Mod 4TB
Rabbit's method (with my tweak):
For duplicates in the final data table, [Tbl_final]:
Removing duplicates (exact duplicated information), frankly, the access wizard does a fairly good job at finding them…. Use it. Once the duplicates with the [Tbl_final] are found you can decide which to delete. Hopefully there is a primary key that will help you to decide which has to go.

To get the new information:
  1. Import your information into a temporary table, [tbl_temp]
    1. The table should have fields with the same data types as the final table. The names do not matter (so long as they are not reserved words); however, I would use something slightly different from the final table.
    2. The temporary table and the final table MUST have a field in common, the Primary Key, in that, the record with a given primary key value in one table will have the same or updated information in the other. (i.e. [tbl_temp]![pk]=1; [tbl_temp]![tmpphone]=”123-555-1212” ::: [tbl_final]![pk]=1; [tbl_final]![home_phone]=”123-555-0000)”
  2. Verify that the information is correct in the temporary table.
  3. Create a query.
    1. Show both the temporary table and the final table
    2. Drag a relationship between the temporary table and the final table – ON THE RELATED primary key: (i.e. [tbl_temp]![pk] ::: [tbl_temp]![pk])”
    3. Edit the relationship so as that all results from the temporary table show and matching from the final table
    4. Drag the fields from the temp/final tables down to the design grid.
  4. Save this as a select query first
  5. Run this query, make sure that the query returns all of the correct information…
    1. If not, determine why
    2. If true, the save a copy of this query as “qry_updatefinaltablename” or whatever you like
  6. Open “qry_updatefinaltablename” in design view.
    1. In the ribbon, select “UpDate”
    2. in the grid, remove all of the [tbl_temp] fields, retain only the [tbl_final] fields
    3. Do NOT include the primary key in the design grid from either table… doing this will allow the update to add new records to [tbl_final]; however, if you have this field from either table, the update query may either choke, or fail to append new records.
    4. in the “update to row” in the design grid, select the fields from the [tbl_temp] that match the fields in [tbl_final] that you need to update the values in.
  7. SAVE
  8. Click that funky HUGE RED exclamation point – you will get a warning about how many records will be updated etc…

Say… Z: what about those records in [tbl_final] that are not in [tbl_temp]?
Once again, the access wizard doesn’t do a half bad job at finding unmatched records between tables… just run it and have it look for records in [tbl_final] without a match in [tbl_temp]. Now you need to decide it those records in [tbl_final] either didn’t have an update to their information or they should be removed from the data set because they are of no value. Only the end user’s business model for how the updated record set is being pushed out will provide that answer.

This should be an insight article with as often as I’ve given this out…
Mar 28 '13 #9
Rabbit
12,516 Expert Mod 8TB
Note that this is only to set it up initially. The steps for future runs is import and run the query.

@Z, if you do an update with an outer join, does it insert records that don't match? If not, they will need to create one more query to insert records that don't exist.
Mar 28 '13 #10
zmbd
5,501 Expert Mod 4TB
It has been my experience that the unmatched will be added.
Apr 1 '13 #11
slenish
283 100+
Hi Seth / zmbd / Rabbit

Thank you for all of your help. I ended up going with both options. I found benefits for using both in different ways. Is there a way i can select both answers to be the best answer?

Thank you again and sorry it took me a little while to respond back.

Slen
Apr 1 '13 #12
Seth Schrock
2,965 Expert 2GB
Unfortunately only one answer may be selected, but just select the one that best matches your original question (even if you happened to change the way you did things) for future viewers to get the most benefit from it. The thanks is all we need :)
Apr 1 '13 #13

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

Similar topics

8
by: Mark | last post by:
When my form goes to a new record, I have a procedure that copies the last record added to the form's underlying table into the form. The intent is that a series of new records may have the same...
2
by: c.kurutz | last post by:
Hello. I have a data entry form with several combo box fields with dlookup code in the after update procedure for the field. I am trying to add a button to copy the record on this form. I am...
4
by: MP | last post by:
I need a code in the Before Update event procedure, which prevents to enter duplicate record in a form. I have the main formX, which takes values from the table named tblX and a subformY, which...
2
by: DaveN | last post by:
Hi all, I'm trying to update a record in a table with data from text boxes on a form. As a background to this, I managed to add a new record to the table in a similar manner with the following:...
1
by: 2D Rick | last post by:
Access2003 in XP If I open a form to a specific record and click on a command button with the following code I get a duplcate record: DoCmd.RunCommand acCmdSelectRecord DoCmd.RunCommand...
6
by: planetthoughtful | last post by:
Hi All, I have a C# ASP.NET page that submits back to itself to insert details from a form into a database table. When / if the user refreshes the page (and gets the standard warning that POST...
0
by: Antonio | last post by:
Hello, everyone. Something strange is happening when I edit a record in the datagrid. I have a cust_id field that, when the grid is in edit mode, users can change. I don't really want to display...
4
by: mcca0081 | last post by:
hi - i'm trying to delete one of the duplicate records based on the most current date. here's the code for my access 2000 db. any help would be appreciated!!! - thank you kindly Sub...
2
by: Ranma13 | last post by:
Hello, I have a duplicate record check written in VB for a check in/check out database. Here's the pseudocode, written for the BeforeUpdate property on the form: If DCount(search for records with...
3
by: JMANTN | last post by:
Hello, I'm hoping someone could help me out with a problem I've ran into while trying to create a training database. I'm just a beginner with Access and VBA so please take that into consideration. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
0
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...
0
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...

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.