Excel VBA ERROR ODBC connection failed

kashif.special2005

Active Member
Joined
Oct 26, 2009
Messages
443
Hi,

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.

Rich (BB 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

Please help me to resolve this problem.

Thanks
Kashif
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
probably considered, some tables are case sensitive ?
 
Upvote 0
Hi Kashif, i think after rs.open you have to give source name..

--

You can try this may b worked for you..

Code:
Dim Connection As ADODB.Connection
Dim Recordset As ADODB.Recordset


Dim Connect As String, Source As String
Dim DBFullName As String


'Your path will be different
DBFullName = "C:\Users\643550\Documents\Database2.accdb"


'Open the connection
Set Connection = New ADODB.Connection
Connect = "Provider=Microsoft.ACE.OLEDB.16.0;"
Connect = Connect & "Data Source=" & DBFullName & ";"
Connection.Open ConnectionString:=Connect


Set Recordset = New ADODB.Recordset
With Recordset
Source = "SELECT * from..

.Open Source:=Source, ActiveConnection:=Connection

'Write recordset
 Sheet1.Range("A1").CopyFromRecordset rs
 
Last edited:
Upvote 0
Hi,

I somehow manage to retrieve the query's result in recordset, however setting the accdb=nothing, it show the error message like below.

Error Message:

Microsoft Access has stopped working


Windows can try to recover your information

Code:
Sub FetchData()


    Dim rs As ADODB.Recordset
    Dim ss As String
    Dim conn As String
    Dim accdb As Access.Application
    
    Set accdb = New Access.Application


    
   Call accdb.OpenCurrentDatabase(Filepath:="G:\Workflow Tools (Michael Cantor)\Tool For Fixing Bug From Michael Cantor\PI MDT Reconciliation Workflow Tool\PI Database.accdb", exclusive:=False)


    ss = "[B]Select * from peoplemain[/B]"


    Set rs = New ADODB.Recordset
    Set rs = accdb.CurrentProject.Connection.Execute(ss)
    
    Sheet1.Range("A1").CopyFromRecordset rs




[B]    Set accdb = Nothing  'Here it is giving me that error message[/B]
    rs.Close
    Set rs = Nothing
    'cn.Close
    
    MsgBox "done"


End Sub

Thanks
Kashif
 
Upvote 0
I don't understand why you are querying Access for a SQL Server table? Why deliberately add in an extra level of complexity when you can query SQL Server directly?
 
Upvote 0
Hi,

Thanks for reply, actually my main database is MS-Access database, and all the required tables are in MS-Access database except 4 tables, that's why I created linked table's for these 4 in MS-Access so that I can create a join query from one platform, in my post for example purpose I gave linked table name 'peoplemain', actually those queries are not working that is taking reference of "Linked" table.

I hope I clarify your thought.

Thanks for your reply.

Thanks
Kashif
 
Upvote 0
actually those queries are not working that is taking reference of "Linked" table

Well... exactly. Those connections are failing because you're routing the query through a different "engine" to a different location with a different native syntax and (most importantly) a completely different security environment. Access is designed to handle that internally but for pass through queries? I seriously doubt it.

Query separately then join the results. Failing that create a query in Access that exposes the actual data in the underlying SQL Server tables you need and query that.
 
Upvote 0
Hi ScottR,

Thanks for reply, actually it is working in excel 32-bit version, 32-bit users are successfully getting the results from linked tables from join query, but some users moved in excel 64-bit version, and 64-bit users are facing the query problem from linked table error "ODBC connection failed".

Thanks
Kashif
 
Upvote 0

Forum statistics

Threads
1,215,214
Messages
6,123,664
Members
449,114
Latest member
aides

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