I currently have a worksheet that pulls all records from Table A that do not exists within Table B, I then have a Macro that inserts those values into Table B.
But I realized that it will produce duplicates if the query is not refreshed before the macro is inserted.
With the current Macro I have, how can I include a refresh before and after the insert?
Sub Insert_New_Bills()
Dim conn As New ADODB.Connection
Dim iRowNo As Integer
Dim sBILL_NUM, sROCKTENN_DOC, sACTION, sNOTE1, sNOTE2 As String
With Sheets("NEW BILLS")
'Open a connection to SQL Server
conn.Open "Driver={IBM DB2 ODBC DRIVER};Database=BROWN;Hostname=192.168.100.44;Port=50000;Protocol=TCPIP;Uid=" & "User" & ";Pwd=" & "Maddox2009" & ";CurrentSchema=LYNX;"
'Skip the header row
iRowNo = 2
'Loop until empty cell in COLUMN1
Do Until .Cells(iRowNo, 2) = ""
sBILL_NUM = .Cells(iRowNo, 2)
sROCKTENN_DOC = .Cells(iRowNo, 3)
sACTION = .Cells(iRowNo, 4)
sNOTE1 = .Cells(iRowNo, 5)
sNOTE2 = .Cells(iRowNo, 6)
'Generate and execute sql statement to import the excel rows to SQL Server table
conn.Execute "INSERT INTO OH_CU_WR_TEMPLATE (BILL_NUMBER, ROCKTENN_DOC, ACTION, NOTE1, NOTE2) values ('" & sBILL_NUM & "','" & sROCKTENN_DOC & "', '" & sACTION & "', '" & sNOTE1 & "', '" & sNOTE2 & "')"
iRowNo = iRowNo + 1
Loop
MsgBox "RECORD UPDATED"
conn.Close
Set conn = Nothing
Selection.ListObject.QueryTable.Refresh BackgroundQuery:=False
End With
End Sub