VBA - Insert Into Oracle Table

Noz2k

Well-known Member
Joined
Mar 15, 2011
Messages
693
Hey, I'm trying to insert data into a table in an Oracle database but not having much luck with what I can find online. Below is the code which I currently have, any help would be really appreciated.

Code:
Sub Insert()


Set ws1 = Sheets("Sheet1")

With ws1
   inputData = ws1.Range("A1").Value
End With

Dim cnn As ADODB.Connection




   Dim SqlString As String
   Set cnn = New ADODB.Connection
   ' Set properties of the Connection.
   cnn.ConnectionString = "Provider=MSDAORA;Data Source=xxxxxx;user ID=xxxxxxx;password=xxxxxxx;"
   cnn.ConnectionTimeout = 90
   ' Open the connection.
   cnn.Open
   ' Find out if the attempt to connect worked.
   If cnn.State = adStateOpen Then
   
   Else
      MsgBox "Sorry. Connection Failed"
      Exit Sub
   End If
 
        
        'Define Query
        SqlString = "Insert into TABLE_NAME Values('" & inputData & "'); " & vbNewLine & _
        "Commit;"
                  
        ws1.Range("A10") = SqlString
        cnn.Execute SqlString
        
   'Next x
   ' Close the connection.
   cnn.Close
   Set cnn = Nothing
End Sub
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
I get an "invalid character" error, but the SQL works if I paste what it returns in Cell A10
 
Upvote 0
Just for anyone who may come across a similar problem, I was missing the "adCmdText"

So the code now looks like

Code:
Sub Insert()


Set ws1 = Sheets("Sheet1")
'Define last row
With ws1
   inputData = ws1.Range("A1").Value
End With
 Dim cnn As ADODB.Connection




   Dim SqlString As String
   Set cnn = New ADODB.Connection
   ' Set properties of the Connection.
   cnn.ConnectionString = "Provider=xxxxxxx;Data Source=xxxxxxx;user ID=xxxxxxx;password=xxxxxxx;"
   cnn.ConnectionTimeout = 90
   ' Open the connection.
   cnn.Open
   ' Find out if the attempt to connect worked.
   If cnn.State = adStateOpen Then
   
   Else
      MsgBox "Sorry. Connection Failed"
      Exit Sub
   End If


        'Define Query
        SqlString = "Insert into TABLE_NAME Values('" & inputData & "')"
                  
        ws1.Range("A10") = SqlString
        cnn.Execute SqlString, , adCmdText
        
   'Next x
   ' Close the connection.
   cnn.Close
   Set cnn = Nothing
End Sub
 
Upvote 0

Forum statistics

Threads
1,222,622
Messages
6,167,127
Members
452,098
Latest member
xel003

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
Back
Top