title: How to insert records from Excel using VBA to DB2 Database
body: <p>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.</p>
  
  <p>How can I have the records from Excel be inserted only if the record does not exist within the database? </p>
  
  <p>This is what I currently have. </p>
  
  <p>Additionally can I have records that do exist be updated or would that require another Module?</p>
  
  <pre><code>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=" &amp; "XXXX" &amp; ";Pwd=" &amp; "XXXXXX" &amp; ";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 ('" &amp; sequipid &amp; "','" &amp; stype &amp; "','" &amp; sname &amp; "')"
  
              iRowNo = iRowNo + 1
          Loop
  
          MsgBox "RECORD UPDATED"
  
          conn.Close
          Set conn = Nothing
  
      End With
  
  End Sub
  </code></pre>