SQL Insert or Update depending on if record is found ?

JumboCactuar

Well-known Member
Joined
Nov 16, 2016
Messages
727
Office Version
  1. 365
Platform
  1. Windows
Hi,

I have the following code which inserts data into a table with ADODB connection or updates table depending on the if statement.

VBA Code:
Sub Button1Click()

If Sheet3.Range("N3") <> True Then
    insertDATA
Else
    updateDATA
End If

End Sub

VBA Code:
Sub insertDATA()

   Set cnn = CreateObject("ADODB.Connection")
    cnn.Open "Driver={MySQL ODBC 5.1 Driver};SERVER=xxx;DATABASE=xxx;UID=xxx;PWD=xxx;PORT=3306;"

    i = 2
    
    Data2 = Sheet3.Cells(i, 2).Value
    Data3 = Sheet3.Cells(i, 3).Value
    Data4 = Sheet3.Cells(i, 4).Value
    Data5 = Sheet3.Cells(i, 5).Value
    Data6 = Sheet3.Cells(i, 6).Value
    Data7 = Sheet3.Cells(i, 7).Value
    
    SQLSTRING = "INSERT INTO TABLE1 (RECID,RECDATE,RECTIME,RECLOC,RECDATA,RECMEMO) VALUES ('" & Data2 & "','" & Data3 & "','" & Data4 & "','" & Data5 & "','" & Data6 & "','" & Data7 & "');"
    cnn.Execute SQLSTRING
    Next i
    
    cnn.Close
    Set cnn = Nothing
    
Sheet3.Range("N3") = True

End Sub

VBA Code:
Sub updateDATA()

   Set cnn = CreateObject("ADODB.Connection")
    cnn.Open "Driver={MySQL ODBC 5.1 Driver};SERVER=xxx;DATABASE=xxx;UID=xxx;PWD=xxx;PORT=3306;"

    i = 2
    
    Data2 = Sheet3.Cells(i, 2).Value
    Data3 = Sheet3.Cells(i, 3).Value
    Data4 = Sheet3.Cells(i, 4).Value
    Data5 = Sheet3.Cells(i, 5).Value
    Data6 = Sheet3.Cells(i, 6).Value
    Data7 = Sheet3.Cells(i, 7).Value
       
    SQLSTRING = "UPDATE TABLE1 " & _
    "SET RECID = '" & Data2 & "' " & _
    ",RECDATE = '" & Data3 & "' " & _
    ",RECTIME = '" & Data4 & "' " & _
    ",RECLOC = '" & Data5 & "' " & _
    ",RECDATA = '" & Data6 & "' " & _
    ",RECMEMO = '" & Data7 & "' " & _
    "WHERE RECID='" & Data2 & "';"
            
    cnn.Execute SQLSTRING
    
    Next i
    
    cnn.Close
    Set cnn = Nothing

End Sub

30SqVQn.png



I think i can somehow do this all in one module using recordset somehow?

IF RECID is not found in table then INSERT else UPDATE

Does anyone know the best way to do this? Without locking the database longer than needed

any help appreciated
 

Some videos you may like

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple

JumboCactuar

Well-known Member
Joined
Nov 16, 2016
Messages
727
Office Version
  1. 365
Platform
  1. Windows
edit:
something like this but unsure how i can use with the above

VBA Code:
If rs.EOF Then
    'Record not found so insert
     SQLSTRING = "INSERT INTO...."
Else
     'Record is found so update
     SQLSTRING = "UPDATE TABLE1...."
End If
 

Watch MrExcel Video

Forum statistics

Threads
1,128,113
Messages
5,628,774
Members
416,337
Latest member
tl3phd

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top