provider not found for oracle OraOLEDB - excel

I am trying to run a vba script in excel using Oracle db. When i try to establish a conn, it is showing some error like:
Connection Error
Import failed: 3706 - Provider cannot be found. it may not be installed properly.
I know there are many Q&A related to this, but i couldn't find any useful answers. please help me.

please check with this code
Sub Oracle_Connection_VBA()
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 with the corresponding values
strcon = "Provider=MSDAORA;Data Source=[Your Database];Persist Security Info=True;User ID=[DB username];Password=[DbPassword]"
con.Open (strcon)
'do
con.Close
End Sub

Related

Not able to connect to Oracle DB using excel VBA

I am trying to connect to Oracle Database, using Excel VBA, but not able to connect. Any guidance is welcome.
Sub connection()
Dim cn As ADODB.connection
Dim rs As ADODB.Recordset
Set cn = New ADODB.connection
Set rs = New ADODB.Recordset
constr = "Driver={Microsoft ODBC for Oracle}; CONNECTSTRING=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=<HostName>)(PORT=<port>))(CONNECT_DATA=(SID=<sid>))); uid=<UserName>; pwd=<Password>;"
cn.Open (constr)
End Sub
When trying to debug, it's giving the below error at Open Connection.
Automation Error
Unspecified Error
Error While Creating the Database connection

VBA - ADODB connect to remote Oracle db

I am developing an Excel VBA program that I want to connect to a remote Oracle 11g database, run a query, and return the data to Excel.
Connection strings and drivers are all OK as far as I know. (see below)
It was working perfectly for a while yesterday, connecting to db and returning with correct data. Then I saved and closed the workbook, opened it 20 minutes later (no changes made!) and when I ran the macro I got the following error:
Runtime error '-2147418113 (8000ffff)': Catastrophic failure
any ideas what could be causing the error?
Could it be something on the DB's side?
Thanks
Sub ExtractFromOracle(environment As String)
Dim cn As ADODB.connection
Dim recordSet As ADODB.recordSet
Set cn = CreateObject("ADODB.Connection")
Set recordSet = CreateObject("ADODB.recordset")
Dim SQLQuery As String
SQLQuery = "SELECT User_Id, Prof_Id FROM user_profile ORDER BY User_Id ASC"
Dim returnData As Variant
Dim returnedRowsCount As Integer
Dim connectionString As String
connectionString = ReturnConnectionString(environment)
cn.Open (connectionString) ------------> ERROR OCCURS HERE
Set recordSet = cn.Execute(SQLQuery)
returnData = Application.Transpose(recordSet.GetRows)
returnedRowsCount = UBound(returnData)
If Not SheetExist(environment) Then
CreateTab (environment)
End If
Worksheets(environment).Activate
ActiveSheet.Range("A1:B" & returnedRowsCount) = returnData
Set rs = Nothing
Set cn = Nothing
ConsolidateUsers (environment)
End Sub
CONNECTION STRING :
"Provider=OraOLEDB.Oracle;Data Source=(DESCRIPTION=(CID=GTU_APP)(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=xxxxxxx)(PORT=xxxx)))(CONNECT_DATA=(SID=xxxxx)(SERVER=DEDICATED)));User Id=xxxxxx;Password=xxxxxxx;"
Can you try to access the ADODB as below
Dim strSQL As String, conStr as String
Dim cnn As New ADODB.Connection
Dim rs As New ADODB.Recordset
conStr = "Provider=OraOLEDB.Oracle;Data Source=(DESCRIPTION=(CID=GTU_APP)(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=xxxxxxx)(PORT=xxxx)))(CONNECT_DATA=(SID=xxxxx)(SERVER=DEDICATED)));User Id=xxxxxx;Password=xxxxxxx;"
Dim SQLQuery As String
SQLQuery = "SELECT User_Id, Prof_Id FROM user_profile ORDER BY User_Id ASC"
cnn.open conStr
rs.Open SQLQuery, cnn, adOpenStatic, adLockOptimistic, adCmdText
I fixed the error so I though I would update for anyone with the same issue who finds this thread.
It turns out that the 'Catastrophic Failure' error was due to my Oracle ODBC driver (oraOLEDB.oracle) becoming somehow corrupted, I couldn't even reinstall it properly, the error only occurred on my machine.
In the end I did a system restore to a point from before the issue and all was fixed. Not the most elegant solution but it worked.

Excel VBA Executing Access Query that Links to Oracle

Let me start of by saying I am new to both this site as well as VBA so please bear with me. Thank you in advance for your help.
I have a VBA function that runs an existing query from Access. Some of the tables being queried are stored in an Oracle database that require a user specific password access. Right now, a sub that I wrote to automate a report calls this function 7 times and requires the user to input their Oracle password each time the function is called (it also stops the sub and gives an error message if they type in the password incorrectly which I see as a likely event if they need to do it 7 times). The code works but I would like to find a way to have the code ask for the password once and be done with it. All of the solutions I have found involve connecting to and querying Oracle directly which requires very complicated SQL coding that I am by no means capable of writing.
I am also having an issue where the columns show up in the excel sheet in a different order than they do in Access for some of the queries. This seems to be consistent so it isn't to big of a problem but I would like to know how to prevent this to prevent any future issues.
Here is the code for the function I am currently using. Any insight would be greatly appreciated!
Option Explicit
'Single Argument "qryName" as string. Runs access qry and copys recordset to active sheet.
Function AccessQueryPull(qryName As String)
'Declare variables
Dim rs As ADODB.Recordset
Dim cn As ADODB.Connection
'open the connection to the access database
Set cn = New ADODB.Connection
cn.Open "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=filePath.accdb;"
'format the command to run the query
Dim cmd As New ADODB.Command
cmd.CommandType = adCmdStoredProc
cmd.CommandText = qryName
cmd.ActiveConnection = cn
'execute the query
Set rs = New ADODB.Recordset
Set rs = cmd.Execute()
'copy data to excel sheet
ActiveSheet.Cells(2, 1).CopyFromRecordset rs
'Cleanup
rs.Close
Set rs = Nothing
The reason you are prompted each time for Oracle credentials is that you create a fresh, new connection to MS Access each time the function is called.
Simply persist the MS Access connection by connecting once in the Sub that calls function and pass connection object as parameter. In this way, any connection error is caught in the parent routine. As for column order simply declare the columns in the needed order in SQL statement.
Sub RunQueries()
Dim rs As ADODB.Recordset
Dim cn As ADODB.Connection
'open the connection to the Access database
Set cn = New ADODB.Connection
cn.Open "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=filePath.accdb;"
Call AccessQueryPull(cn, "SELECT Col1, Col2, Col3 FROM Qry1") ' AND OTHER 6 CALLS
cn.Close
Set cn = Nothing
End Sub
Function AccessQueryPull(accConn As ADODB.Connection, qryName As String)
'format the command to run the query
Dim cmd As New ADODB.Command
cmd.CommandType = adCmdStoredProc
cmd.CommandText = qryName
cmd.ActiveConnection = accConn
'execute the query
Set rs = New ADODB.Recordset
Set rs = cmd.Execute()
'copy data to excel sheet
ActiveSheet.Cells(2, 1).CopyFromRecordset rs
'Cleanup
rs.Close
Set rs = Nothing: Set cmd = Nothing
End Function

Get specific data(query) from Oracle into excel

I want to load specific data(query) from Oracle database into excel. I am able to achieve it through external connections, (Mircosoft Data Access - OLE DB Provider for Oracle), but all of the table is loaded. This was by hit and try. I am not aware what OLE DB is.
Is it possible to load specific data using that method.
How can I load the same from VBA, I have read many sources, blogs but none are lucid and comprehensive. Can somebody please explain for a newbie. Or refer to me some book/source.
This function will connect to an Oracle Database using ADODB. Make sure to include Microsoft ActiveX Data Objects 2.8 as a reference. You can configure the connectingstring to suit your needs if there are admin privileges.
It will store your database into a variant.
Function ConToDataBase(DBPath As String) As Variant
Dim Con As ADODB.Connection
Dim Rs As ADODB.Recordset
Dim SQL As String
SQL = "SELECT * FROM TableName"
Set Con = New ADODB.Connection
With Con
.ConnectionString = "Provider=OraOLEDB.Oracle;Data source=" & DBPath & ";UserID=;Password:=;"
.Open
End With
Set Rs = New ADODB.Recordset
Rs.Open SQL, Con
Dim Var As Variant
Var = Rs.GetRows
ConToDataBase = Var
Set Rs = Nothing
Con.Close
End Function

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