How to connect to Oracle 12c DB with VBA - excel

I have added a reference to Microsoft ActiveX Data Object 2.0 Library, and tried to execute the following code:
Sub Ora_Connection()
Dim con As ADODB.Connection
Dim rs As ADODB.Recordset
Dim query As String
Set con = New ADODB.Connection
Set rs = New ADODB.Recordset
strCon = "Driver={Microsoft ODBC for Oracle}; " & _
"CONNECTSTRING=(DESCRIPTION=" & _
"(ADDRESS=(PROTOCOL=TCP)" & _
"(HOST=****)(PORT=****))" & _
"(CONNECT_DATA=(SERVICE_NAME=****))); uid=****; pwd=****;"
con.Open (strCon)
End Sub
I am getting these errors:
I have tried installing Oracle instant client ODBC drivers (I've tried 32bit and 64 bit versions) and I am having no luck.
I am not sure what the problem is. I can connect to the database in Visual Studio 2012 with VB.Net, but I am unable to connect in excel 2010 using VBA.
Any insight would be greatly appreciated!

Related

I get a runtime error when attempting to connect to Snowflake using VBA ADODB

I am trying to connect to Snowflake within VBA and get a runtime error: Automation error, unspecified error. Any idea what could be causing it? I do have the ActiveX package installed. In the debugging I do see the problem is when the connection string attempts to open.
Sub selectSnow()
Dim cnn As New ADODB.Connection
Dim rst As New ADODB.Recordset
Dim ConnectionString As String
Dim StrQuery As String
ConnectionString = "DRIVER={SnowflakeDSIIDriver};SERVER=XXXXXX.snowflakecomputing.com;" & _
"DATABASE=XXXXX;SCHEMA=XXXXXX;" & _
"ROLE=XXXXX;UID=XXXXXX;" & _
"PROXY = http://proxy:port;Authenticator = externalbrowser"
cnn.Open ConnectionString
End Sub

Error opening recordset in VBA

I'm developing an Excel VBA application using ADO. I'm trying to open a recordset, but the open method fails if my table has more than 65536 lines. I know this number is the old Excel line limit, but I'm using the 2016 version and the correct connection strings. Perhaps it's some library referenced in my project, but I can't find out which one.
I would appreciate very much if I could get some help with this.
The error:
Runtime error '-2147217865 (80040e37)': The Microsoft Jet database engine could not find the object 'My sheet$A8:AD70000'. Make sure the object exists and that you spell its name and the path name correctly.
My code (I had to switch the worksheet name so I don't reveal any sensitive data):
Sub MySub()
Dim conn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim str As String
Set rs = New ADODB.Recordset
Set conn = New ADODB.Connection
str = "SELECT * FROM [My sheet$A8:AD70000];"
'Opening connection with the workbook
conn.ConnectionTimeout = 90
conn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=" & Application.ThisWorkbook.FullName & ";" & _
"Extended Properties=""Excel 12.0 Macro;HDR=YES;IMEX=1"";"
conn.Open
rs.Open str, conn, adOpenForwardOnly, adLockReadOnly, adCmdText
rs.Close
conn.Close
End Sub
If I change "My sheet$A8:AD70000" to "My sheet$A8:AD60000", this code Works.
The libraries I'm using:
Visual Basic For Applications
Microsoft Excel 16.0 Object Library
Microsoft Forms 2.0 Object Library
Microsoft Office 16.0 Object Library
Microsoft ActiveX Data Objects 6.1 Library
OLE Automation
Thank you very much for your attention.

VS2010, Windows Form Application

As part of the application, I am trying to update an excel sheet using ado. But I get an error stating "ODBC driver does not support the requested properties".
Code is as below. Can anyone please suggest . Thanks.
Dim cn As New ADODB.Connection
Dim rc As New ADODB.Recordset NewNmae = "INSERT INTO [Names$] VALUES('ASDASD','ASDASDASD','ASDASDASD');"
cn.ConnectionString = "Driver={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};" & _
"DBQ=" & ListNames
cn.Open(cn.ConnectionString)
rc.Open(NewNmae, cn, ADODB.CursorTypeEnum.adOpenKeyset, ADODB.LockTypeEnum.adLockPessimistic)
cn.Close()
rc.Close()`
You should set the provider to Jet OLEDB:
cn.Provider="Microsoft.Jet.OLEDB.4.0";

[Microsoft][ODBC Driver Manager]Data source name not found and no default driver specified

I write some VBA code in my spreadsheet in excel 2013, the code will connect Oracle 11g to write some data into it. I use following code to connect remote Oracle server:
Private Function open_DB(tnsInfo As String, userName As String, pwd As String) As ADODB.Connection
Dim conn As ADODB.Connection
Dim connStr As String
Dim tnsInfoArr As Variant
tnsInfoArr = getTnsProperty(tnsInfo, ";")
On Error GoTo Error_Handling
Set conn = New ADODB.Connection
'connStr =
connStr = "Driver={Microsoft ODBC for Oracle}; " & _
"CONNECTSTRING=(DESCRIPTION=" & _
"(ADDRESS=(PROTOCOL=TCP)" & _
"(HOST=" & tnsInfoArr(1) & ")(PORT=" & tnsInfoArr(2) & "))" & _
"(CONNECT_DATA=(SERVICE_NAME=" & tnsInfoArr(0) & "))); uid=" & userName & " ;pwd=" & pwd & ";"
conn.ConnectionString = connStr
conn.CursorLocation = adUseClient
conn.Open
conn.CommandTimeout = 120
If conn.State = adStateOpen Then
'MsgBox "connect to Oracle successfully"
'conn.Close
End If
Set open_DB = conn
End Function
this code works fine in my computer but it doesn't work in my colleague's PC, following error messages displayed in my colleague's machine:
[Microsoft][ODBC Driver Manager]Data source name not found and no default driver specified
ADO error #:-2147467259
...
do my colleague need some configuration on his PC? but I remember that I have not configure something in my own computer.
can anybody tell me how to solve this problem?
My OS version is 64-bit windows 7 enterprise edition SP1
and I use MS Office 2013 64-bit edition.
I think you need to set up the user DSN via ODBC Data Source Admin (32 bit/64 bit). Also be sure that your colleague's PC has all drivers you need to run the code.
Cheers.

Unspecified run time error while executing vba script

I am new to vba and I am using vba script to connect to database from excel and get the records. I have written the following script for that.I am getting a run time error '-2147467259(80004005)':Unspecified error.
How to resolve this error. See the error screen shot.
Sub Ora_Connection()
Dim con As ADODB.Connection
Dim rs As ADODB.Recordset
Dim query As String
Set con = New ADODB.Connection
Set rs = New ADODB.Recordset
'--- Replace below highlighted names with the corresponding values
strCon = "Driver={Microsoft ODBC for Oracle}; " & _
"CONNECTSTRING=(DESCRIPTION=" & _
"(ADDRESS=(PROTOCOL=TCP)" & _
"(HOST=host_name)(PORT=1521))" & _
"(CONNECT_DATA=(SERVICE_NAME=service_name))); uid=id; pwd=pw;"
'--- Open the above connection string.
con.Open (strCon)
'--- Now connection is open and you can use queries to execute them.
'--- It will be open till you close the connection
query = "select * from security.forms"
Set rs = con.Execute(query)
For i = 0 To rs.Fields.Count - 1
Sheet1.Cells(1, i + 1).Value = rs.Fields(i).Value
Next
con.Close
End Sub
Error screen shot:
Had you tried to use the open method of your RecordSet instance? Maybe it will give you another error that will be more helpful.
Dim connection As New ADODB.connection
Dim rst As New ADODB.Recordset
Dim query As String
connection.ConnectionString = CONNECTION_STRING
connection.Open
rst.Open query, connection, adOpenKeyset, adLockOptimistic
do while not rst.EOF
rst.MoveNext
loop
connection.Close
I had the exact same problem and it was tough to find an answer since most posts on this issue are unanswered.
I solved it by using another Oracle driver. Instead of Microsoft ODBC for Oracle try using the Oracle in OraClient11g_home1 driver. Hope this helps
Please add the reference 'Microsoft ActiveX Data Objects 2.8 Library'

Resources