0
我目前有拉從表A說做B表中不存在的所有記錄的工作表,然後我有一個插入這些值到表B.更新數據庫連接
宏但是我意識到,如果在插入宏之前查詢沒有刷新,它會產生重複項。
使用當前的宏,我如何在插入之前和之後包含刷新?
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
我不知道關於'db2'的語法。但我寧願使用與此類似的解決方案:http://stackoverflow.com/questions/9911659/can-i-use-if-not-exists-to-insert-a-row-if-the-row -s-not-already-in-in-s尤其是如果您允許同一個Excel文件的多個實例,則無法再控制哪些記錄已經插入到數據庫中,哪些記錄仍處於待處理狀態。除此之外,我會實施一個工作表事件來跟蹤哪些已上傳,哪些尚未上傳。但是這個解決方案不會像我提出的第一個那樣簡潔。 – Ralph