I want to insert records into a table from data in my workbook, using VBA I do not have a problem with the inserts, but the problem is the duplicates.
How can I have the records from Excel be inserted only if the record does not exist within the database?
This is what I currently have.
Additionally can I have records that do exist be updated or would that require another Module?
Sub Button1_Click()
Dim conn As New ADODB.Connection
Dim iRowNo As Integer
Dim sequipid, stype, sname As String
With Sheets("Sheet1")
'Open a connection to SQL Server
conn.Open "Driver={IBM DB2 ODBC DRIVER};Database=XXXX;Hostname=192.168.XXX.XX;Port=50000;Protocol=TCPIP;Uid=" & "XXXX" & ";Pwd=" & "XXXXXX" & ";CurrentSchema=LYNX;"
'Skip the header row
iRowNo = 2
'Loop until empty cell in CustomerId
Do Until .Cells(iRowNo, 1) = ""
sequipid = .Cells(iRowNo, 1)
stype = .Cells(iRowNo, 2)
sname = .Cells(iRowNo, 3)
'Generate and execute sql statement to import the excel rows to SQL Server table
conn.Execute "INSERT INTO OH_TEST_TABLE (EQUIPID, TYPE, NAME) VALUES ('" & sequipid & "','" & stype & "','" & sname & "')"
iRowNo = iRowNo + 1
Loop
MsgBox "RECORD UPDATED"
conn.Close
Set conn = Nothing
End With
End Sub