Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6699339
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:41:03+00:00 2026-05-26T06:41:03+00:00

I am trying to write some vba code in Excel to query a table

  • 0

I am trying to write some vba code in Excel to query a table in Access. I have tried multiple code samples for this such as the added links and they all seem to fail at the “Open connection” part. I have tried using different references but I’m not sure which I should be using, what the differences are between some of the different versions (ie. Microsoft ActiveX Data Objects 2.0,2.1,…,6.0) or what the provider information should be. For the provider information I’ve usually been seeing something along the lines of

“Provider=Microsoft.Jet.OLEDB.4.0;Data Source=”

But I’m not sure if that is what I need to use or why/what conditions anything in the provider string shown above should change. Can someone please educate me on how to properly do this sort of thing?

Note: If at all possible I would like a solution that would work without having to download any other application and would work for both 2007 and 2010 versions of Access and Excel since this will need to run on different computers with possibly different versions of office.

Links to similar questions:
Excel VBA query to access is failing
http://www.mrexcel.com/forum/showthread.php?t=527490

Code:

Sub asdf()

strFile = "C:\Users\bwall\Desktop\Excel Query Access Testing"

Dim cn As Object
Dim rs As Object
Dim strSql As String
Dim strConnection As String
Dim AppPath As String
Set cn = CreateObject("ADODB.Connection")
AppPath = Application.ActiveWorkbook.Path


strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    "Data Source=" & AppPath & "\Masterlist_Current_copy.accdb;"
Debug.Print strConnection
strSql = "SELECT [Neptune Number],[Description],[Manufacturer],[Manufacturer P/N] FROM [All Components];"
cn.Open strConnection
Set rs = cn.Execute(strSql)
MsgBox rs.Fields(0) & " rows in MyTable"
rs.Close
Set rs = Nothing
cn.Close
Set cn = Nothing

End Sub  

strConnection value =

Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=C:\Users\bwall\Desktop\Excel Query Access
Testing\Masterlist_Current_copy.accdb;

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T06:41:03+00:00Added an answer on May 26, 2026 at 6:41 am

    The Provider piece must be Provider=Microsoft.ACE.OLEDB.12.0 if your target database is ACCDB format. Provider=Microsoft.Jet.OLEDB.4.0 only works for the older MDB format.

    You shouldn’t even need Access installed if you’re running 32 bit Windows. Jet 4 is included as part of the operating system. If you’re using 64 bit Windows, Jet 4 is not included, but you still wouldn’t need Access itself installed. You can install the Microsoft Access Database Engine 2010 Redistributable. Make sure to download the matching version (AccessDatabaseEngine.exe for 32 bit Windows, or AccessDatabaseEngine_x64.exe for 64 bit).

    You can avoid the issue about which ADO version reference by using late binding, which doesn’t require any reference.

    Dim conn As Object
    Set conn = CreateObject("ADODB.Connection")
    

    Then assign your ConnectionString property to the conn object. Here is a quick example which runs from a code module in Excel 2003 and displays a message box with the row count for MyTable. It uses late binding for the ADO connection and recordset objects, so doesn’t require setting a reference.

    Public Sub foo()
        Dim cn As Object
        Dim rs As Object
        Dim strSql As String
        Dim strConnection As String
        Set cn = CreateObject("ADODB.Connection")
        strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
            "Data Source=C:\Access\webforums\whiteboard2003.mdb"
        strSql = "SELECT Count(*) FROM MyTable;"
        cn.Open strConnection
        Set rs = cn.Execute(strSql)
        MsgBox rs.fields(0) & " rows in MyTable"
        rs.Close
        Set rs = Nothing
        cn.Close
        Set cn = Nothing
    End Sub
    

    If this answer doesn’t resolve the problem, edit your question to show us the full connection string you’re trying to use and the exact error message you get in response for that connection string.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write some C# code to get to a specific folder in
I'm trying to write some LINQ To SQL code that would generate SQL like
I'm trying to write some C# code that calls a method from an unmanaged
I'm trying to write some code to find a specific XmlNode object based on
I'm trying to write some code to work with an htdigest password file. The
I was trying to write some code that would check if an item has
I'm trying to write some code using pure SQL using ASP.NET MVC. I assume
I'm trying to write some code to Hide columns if the first 3 characters
I am trying to write a VBA script which imports all of the Excel
I'm trying to write some abstract code for searching through a list of similar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.