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
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