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
Related
I have a linked table in ms-access which is linked to a sql server table, and when I am trying to fetch the data in excel via VBA from ms-access linked table the error message "ODBC connection failed" showing.
Note:- I am manually successfully able to refresh ms-access linked table in ms-access, "peoplemain" is the name of linked table.
Note:- When I tried to fetch data from non linked table, it is running successfully.
Below code is working in Excel-32 bit version, but not working in excel-64 bit.
[code]
Sub FetchData()
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim conn As String
conn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=G:\Workflow Tools (Michael Cantor)\Tool For Fixing Bug From Michael Cantor\PI MDT Reconciliation Workflow Tool\SampleforPractice.accdb;"
Set cn = New ADODB.Connection
Set rs = New ADODB.Recordset
cn.Open conn
rs.Open "Select * from peoplemain", cn 'Error Line
Sheet1.Range("A1").CopyFromRecordset rs
rs.Close
Set rs = Nothing
cn.Close
Set cn = Nothing
End Sub
Thanks
Kashif
I would try out setting a strSQL as a string like this (I had the same issue you did and when I made the change it worked for me):
Sub FetchData()
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim conn As String, strSQL AS String
conn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=G:\Workflow Tools (Michael Cantor)\Tool For Fixing Bug From Michael Cantor\PI MDT Reconciliation Workflow Tool\SampleforPractice.accdb;"
strSQL = "Select * from peoplemain"
Set cn = New ADODB.Connection
Set rs = New ADODB.Recordset
cn.Open conn
rs.Open strSQL , cn 'Error Line
Sheet1.Range("A1").CopyFromRecordset rs
rs.Close
Set rs = Nothing
cn.Close
Set cn = Nothing
End Sub
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.
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
I have setup some queries in MS access that fetch data from a MS SQL server on the network. The queries work fine when I run them from MS access. However, when trigger the same query through VBA in Excel (need of my dashboard), then it says ODBC connection failed. I am not able to figure out what is broken in the process. All details below:
ODBC connection string in MS Access - set in the properties of the query
ODBC;DRIVER=SQL Server;SERVER=xyz;UID=xyz_reader;PWD=xyzpassword;Network=DBMSSOCN;Address=xyz,1433
Connection string used in Excel VBA - this is used to trigger the odbc query in ms access
strConnection = "Provider=Microsoft.ACE.OLEDB.12.0;" & "Data Source=" & mydb.accdb
Error that I get
Run-time error '-2147467259 (80004005)':
ODBC--connection to 'SQL Serverxyz' failed.
Excel VBA Connection is an ADODB connection so you need to add a references Tool>>References then add “Microsoft ActiveX Data Object Library”
Then use code like
Dim strConn As ADODB.Connection
Dim rs As ADODB.Recordset
Set strConn = New ADODB.Connection
Set rs = New ADODB.Recordset
strConn = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=True;Initial Catalog=YourDATABASE;Data Source=YourServer"
strSQL = "Your Select statement”
strConn.Open
rs.Open strSQL, strConn
Now you have your values in record set. I hope this will solve your issue.
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