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
Related
I am trying to paste in the results of an access query to a sheet in Excel.
When I run the following code, nothing happens. No errors or warnings, just nothing ever gets pasted in. However, when I run the same code, but use a table instead of a query name, it successfully pulls in the table. But when I replace it with a query name, it returns nothing.
In the past I have done the same thing but instead of calling the query name, I have stored the SQL query as a string within the macro and called the query by referencing the string. I tried that and that did not work in this case.
The query I am trying to pull references other queries as tables, and I think this is why I am having issues. Like I said, I have pulled data from Access with this code many times before but have only been able to do so when I am referencing tables only, and not queries.
Dim Con As ADODB.Connection
Set Con = New ADODB.Connection
Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset
Dim iCols As Integer
Set Con = New ADODB.Connection
With Con
.Provider = "Microsoft.ACE.OLEDB.12.0"
.Open AccessDBPath
End With
Set rs = New ADODB.Recordset
rs.Open "query1", Con
Sheets("NAHV").Range("E2").CopyFromRecordset rs
I have tried replacing the rs.Open line with the Execute command:
Set rs = Con.Execute("query1")
and this also did not work.
I have also tried replacing the full block with
Dim cn As Object, rs As Object
Dim myFile As String: myFile = AccessDBPath
Set cn = CreateObject("ADODB.Connection")
With cn
.Provider = "Microsoft.ACE.OLEDB.12.0"
.ConnectionString = "Data Source=" & myFile & ";"
.Open
End With
Set rs = cn.Execute("query1")
Sheets("NAHV").Range("E2").CopyFromRecordset rs
rs.Close
cn.Close
Set rs = Nothing
Set cn = Nothing
and this did not work either.
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.
Hi I need to connect my Excel file to run a macro that executes select query.
My sample code is
Sub Button1_Click()
Range("A1").Value = "Hiii!"
Dim cn As Object
Dim rs As Object
Dim strCon As String
Dim strSQL, strInput As String
strCon = "Provider=MSDAORA.1;User ID=bis5151XXXXXa1;Data Source=canafpi5467qanfo.iaksedafdqwte.com;Password=iXoXXX8886s55it"
Set cn = CreateObject("ADODB.Connection")
cn.Open strCon
strInput = InputBox("Input Desired Name")
Sheet1.Range("B1").Value = strInput
strSQL = "select * from " & strInput & " where rownum<200"
Sheet2.Range("D1").Value = strSQL
'Added the following four lines
Set rs = CreateObject("ADODB.RECORDSET")
rs.activeconnection = cn
rs.Open strSQL
Sheet2.Range("A10").CopyFromRecordset rs
rs.Close
cn.Close
Set cn = Nothing
End Sub
=============================================
But my Database doesnt have an entry in the tnsnames.ora file. I also tried giving tns details as data source but am getting TNS failure error.
Could some one please help me to connect my excel to oracle just to run a select statement so that the value is returned to a particular cell in excel.
FYI I already have my ODBC configured.
I am using VBA in Excel 2010 on Windows 7 (ADO 6.1). I'm trying to execute the code below to grab a disconnected recordset from a SQL stored procedure. There are no parameters, and if I try to execute this as a connected recordset (server side cursor), the code executes just fine.
The problem occurs on the very last line of the procedure where I try to set the activeconnection property to nothing. Excel pops up a blank error message box with no error message or error number, just the vbCritical icon.
Does anyone know why an error would occur trying to clear the active connection?
Dim SQLConnectionString as String
SQLConnectionString = "Driver={SQL Server};Server=SERVERNAME\INSTANCE;Database=Database;UID=user;PWD=password;"
Dim cn As ADODB.Connection
Dim cmd As ADODB.Command
Dim rs As ADODB.recordset
Set cn = New ADODB.Connection
cn.Open SQLConnectionString
Set cmd = New ADODB.Command
Set rs = New ADODB.recordset
With cmd
.ActiveConnection = cn
.CommandText = sp
.CommandType = adCmdStoredProc
End With
Set rs.ActiveConnection = cn
rs.CursorLocation = adUseClient
rs.LockType = adLockBatchOptimistic
rs.CursorType = adOpenStatic
rs.Open cmd.Execute
Set rs.ActiveConnection = Nothing
I'm trying to connect excel on a database which has the following query.
SELECT * FROM Products WHERE Order = [Enter Order]
but excel can't seem to find this query. It only shows the actual table and other queries which does not use parameters.
Is there a way to connect excel on a query which uses parameters? I'm using MS-Excel 2007.
Some notes.
"Parameter queries must be created in Microsoft Query."
Customize a parameter query
Use Microsoft Query to retrieve external data
ADODB & VBA
''Ref: Microsoft ActiveX Data Objects x.x Library
Dim cmd As New ADODB.Command
Dim conn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim prm As ADODB.Parameter
Dim strConn As String
strConn = "Provider = Microsoft.ACE.OLEDB.12.0;" _
& "Data Source=z:\docs\test.accdb"
conn.Open strConn
cmd.ActiveConnection = conn
cmd.CommandText = "Query4"
cmd.CommandType = adCmdStoredProc
Set prm = cmd.CreateParameter("EnterText", adVarWChar, adParamInput, 50)
cmd.Parameters.Append prm
cmd.Parameters("EnterText").Value = ActiveWorkbook.Sheets("Sheet5").[A2]
'Execute the Stored Procedure
Set rs = cmd.Execute
ActiveWorkbook.Sheets("Sheet8").Cells(2, 1).CopyFromRecordset rs
'Close the connection
conn.Close