Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Macro to Delete Duplicate Records

Status
Not open for further replies.

etseel

Technical User
Jan 22, 2004
34
US
I could use some help with a dcount criteria inside an Access Macro which I'm trying to use for a recordcount. I want a command to run if recordcount is greater than zero.

I have a split database deployed on a network. The Back End contains all of my tables and sits on the server, and there is a Front End available for my users to either user on the server or save locally. All queries/forms are set with admin permission, so whoever uses the database should be able to write to the tables and update the data, so I don't think security is my problem.

Users run a macro from a form that imports a new client portfolio from a CSV file into a "temp" table. The macro will eventually append the temp folder onto the "AllPortfolios" table. These two steps work great.

My problem is the intermediate step (lets call it "DeleteDuplicates"). I have a query that matches the Temp table to the AllPortfolios table on 3 criteria (using type 1 relationships). It then is supposed to delete all records from AllPortfolios that match on all 3 criteria. The intent is to prevent my users from accidentally inputing the same records repeatedly (a major problem in all previous versions of this db).

Several different scenarios with this process have run into snags. It worked great before I split the database. Now however it doesn't work as flawlessly. In the latest iteration it works well as long as there are some duplicates, but when there are no duplicates it fails.

Latest Plan:
-Select query called "FindDuplicates" matching the two tables on 3 fields, joined relationships returns * fields from the AllPortfolios table.
-Macro to run the whole import/scrub/append process
--1) SetWarnings Turns off warnings
--2) OpenQuery Delete query clears out the old TempPort table
--3) TransferText Imports the CSV file using import spec
--4) OpenQuery FindDuplicates table
--5) RunCommand.SelectAll (here I'm hoping for a criteria to only run when dcount >0 or something) It works if I don't use the criteria AND I have some duplicates. Crashes if no duplicates in AllPortfolios
--6) RunCommand.DeleteRecord (here I want to delete the selected records if dcount>0)
--7) Close FindDuplicates query
--8) OpenQuery - append TempPort this works
--9) OpenQuery delete query clears out new TempPort
--10) SetWarnings back on

Thanks in advance if anyone can help me...

Old Iterations:
- With no condition, the macro works great as long as there is some sort of duplicate record.
- First, I tried using a Delete Query built just like my new select query. This worked great until I split the database. It was a simple, fast solution that worked whether or not there were results to the query. Now, however that solution gives me a "query must be updatable" and won't work.
- Then I built a select query that did the same thing, with another query set up to delete the other query - also gave me the same error message about query must be updateable.
 
Hi

A couple of suggestions immediately come to mind.

First, you will be far better served by writing your instructions in a VBA module rather than a macro. That will give you more flexibility and error handling capabilities.

Second, you can prevent the entry of duplicate records by using a multi field primary key in the AllPortfolios table. I believe that when the append query is run against it, only non-duplicate records will be appended.

Cheers,
Bill
 
Bill, Thanks for the ideas. Clearly you're correct saying that the VBA module is more powerful than the macro. However it is also significantly more complex and since I'm not a programmer I find it much more difficult to program in VB in Access than say Excel, where you can at least record a macro and have a foundation of ideas and language to start from...

As for the keys, my problem is the opposite direction - I want the NEW information to replace the old, in addition to preventing duplicates. Some of the other fields may have been updated since the original entry, so I want the temp file version instead of the existing record. I tried to use the "unique records" and later the "unique values" properties, but neither of those worked properly.

Still looking for guidance...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top