Very Slow ADO

c68

Board Regular
Joined
Nov 17, 2004
Messages
54
I have a process of updating a table from an Excel file. Before I upload anything to the table, I want to make sure that I do not have the same data already in the database based on values that appear in column A, B and I of the spreadsheet. If the data points do exist, then they are deleted. The routine below examines each row vs. the table one by one. As a result, the process takes a long time. Someone has suggested that I place those values from column A, B, and I in arrays and examine the database that way. I don’t have much experience with arrays to go about this process. Can someone please help me? Your help is much appreciated. Thank you.


Sub DeleteIfReturnsAlreadyExist()
Dim rst As Object
Dim cn As Object
Dim i As Integer
Dim lstCell As Long

lstCell = [a65536].End(xlUp).Row

Application.ScreenUpdating = False

Set rst = CreateObject("ADODB.Recordset")
Set cn = CreateObject("ADODB.Connection")

With cn
.Provider = "Microsoft.Jet.OLEDB.4.0"
.ConnectionString = tmPath
.Open
End With

For i = 2 To lstCell
With rst
.Open "Select * From Returns Where Port_ID='" & Cells(i, 1) & "' And As_Of_Date=#" & _
Cells(i, 2) & "# And Tier=" & Cells(i, 9), cn, 1, 2
Do While Not .EOF
.Delete
.MoveNext
Loop
.Close
End With
Next

cn.Close
Set rst = Nothing
Set cn = Nothing

Application.ScreenUpdating = True
End Sub
 
Possibly setting a unique (composite) index in the access table would work -- if I understand the constraint correctly. then you just run the insert and let Access reject the duplicates.
 
Upvote 0

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Thanks Xenou. Unfortnately I can't do that since the values for the three criterias maybe subject to changes.
 
Upvote 0
Can you provide a little bit of sample data to show how this works? Is the database a permanent data store (you keep adding to it) or a temporary data store (you use it with the data in the workbook for a while then start again with a new dataset)?
 
Upvote 0
Your delete statement should basically look something like this (like your SELECT statement). It only affects the data when all criteria are met.

"DELETE From Returns Where Port_ID='" & Cells(i, 1) & "' And As_Of_Date=#" & _
Cells(i, 2) & "# And Tier=" & Cells(i, 9)
 
Upvote 0
Thank you everyone. I made a slight adjustment to Richards SQL and everything worked out.

strSQL = "DELETE * FROM Returns WHERE (Port_ID & As_Of_Date & Tier) In(" & vArr & ")"
 
Upvote 0

Forum statistics

Threads
1,225,156
Messages
6,183,240
Members
453,152
Latest member
ChrisMd

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top