• Skip to main content
  • Skip to primary sidebar

Ryan McCormick

How to Connect to Access Database with VBScript

June 5, 2014 by Ryan 7 Comments

I am posting this one because it took me a little while to find a solid solution. Everything I found had an answer, just not an answer that works. With this solution, you will be able to connect to and pull data from an MS Access database with VBScript.

So without further ado, here is the example:

Access Source Data

For this example, I am using a database stored in a folder called “example” on my desktop. The table I will be targeting is called “people” and the data contained in my table is:

ID Fname
1 Jim
2 Bob
3 Carl
4 Sandra
5 Jim

Connect to Access Database With VB Script | Access 2007,2010,2013

This example uses the “Provider=Microsoft.ACE.OLEDB.12.0;” to access *.accdb files

Dim connStr, objConn, getNames
'''''''''''''''''''''''''''''''''''''
'Define the driver and data source
'Access 2007, 2010, 2013 ACCDB:
'Provider=Microsoft.ACE.OLEDB.12.0
'Access 2000, 2002-2003 MDB:
'Provider=Microsoft.Jet.OLEDB.4.0
''''''''''''''''''''''''''''''''''''''
connStr = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=C:\Users\username\Desktop\example\example.accdb"

'Define object type
Set objConn = CreateObject("ADODB.Connection")

'Open Connection
objConn.open connStr

'Define recordset and SQL query
Set rs = objConn.execute("SELECT Fname FROM people")

'While loop, loops through all available results
DO WHILE NOT rs.EOF
'add names seperated by comma to getNames
getNames = getNames + rs.Fields(0) & ","
'move to next result before looping again
'this is important
rs.MoveNext
'continue loop
Loop

'Close connection and release objects
objConn.Close
Set rs = Nothing
Set objConn = Nothing

'Return Results via MsgBox
MsgBox getNames
	

Connect to Access Database With VB Script | Access 2000, 2002-2003

This example uses the “Provider=Microsoft.Jet.OLEDB.4.0” to access *.accdb files

Dim connStr, objConn, getNames
'''''''''''''''''''''''''''''''''''''
'Define the driver and data source
'Access 2007, 2010, 2013 ACCDB:
'Provider=Microsoft.ACE.OLEDB.12.0
'Access 2000, 2002-2003 MDB:
'Provider=Microsoft.Jet.OLEDB.4.0
''''''''''''''''''''''''''''''''''''''
connStr = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\Users\username\Desktop\example\example.mdb"

'Define object type
Set objConn = CreateObject("ADODB.Connection")

'Open Connection
objConn.open connStr

'Define recordset and SQL query
Set rs = objConn.execute("SELECT Fname FROM people")

'While loop, loops through all available results
DO WHILE NOT rs.EOF
'add names seperated by comma to getNames
getNames = getNames + rs.Fields(0) & ","
'move to next result before looping again
'this is important
rs.MoveNext
'continue loop
Loop

'Close connection and release objects
objConn.Close
Set rs = Nothing
Set objConn = Nothing

'Return Results via MsgBox
MsgBox getNames
	

Save and Run VBS File

NOTE: If you skip this step you will encounter an error: Provider cannot be found. It may not be properly installed

Because Windows 7 doesn’t have a driver that will run VBS files in 64 bit, you need to make it run your file in 32 bit. This can be accomplished by either opening your command prompt or by creating a shortcut to your file(without quotes):

“%windir%\SysWoW64\wscript.exe C:\Users\username\Desktop\example\example.vbs”

Output from the above samples

access-vbs-output

Conclusion

There are many creative reasons for connecting to an Access Database with VBS. Please comment with questions, suggestions or improvements.

Filed Under: Microsoft Access, VBScript Tagged With: Access Database, Data Connection, VBScript

Reader Interactions

Comments

  1. Brian says

    September 21, 2014 at 5:13 pm

    Was not able to connect to my Access database with VBscript in Windows 8. Thanks to this post I got it working by running cscript in c:windowssyswow64 directory.

    Thanks Ryan

    Reply
  2. Mlondie says

    July 11, 2015 at 4:35 am

    I’ve tried everything you say about forcing 32bit but the error persists. I have verified that there’s nothing wrong with my script because it runs perfectly on another computer, but on my laptop it just won’t! Please help. No matter which database format I use, which Provider connection string I use, all the providers “cannot” be found. What’s wrong with my laptop?

    Reply
    • Ryan McCormick says

      July 11, 2015 at 12:26 pm

      maybe post your code, lets see what you are trying to do.

      Reply
  3. Mike hancoski says

    August 4, 2015 at 11:23 am

    I am getting a no read permission error, do you happen to have an example of how to access a secure mdb file

    Code

    connStr = “Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:SMCORE32.mdb”

    ‘connStr = “Provider=Microsoft.ACE.OLEDB.12.0; Data Source=C:SMCORE32.mdb”‘

    Set objConn = CreateObject(“ADODB.Connection”)

    objConn.open connStr

    ‘Define recordset and SQL query
    Set rs = objConn.execute(“SELECT * FROM vendors”)

    ‘While loop, loops through all available results
    DO WHILE NOT rs.EOF
    ‘add names seperated by comma to getNames
    getNames = getNames + rs.Fields(0) & “,”
    ‘move to next result before looping again
    ‘this is important
    rs.MoveNext
    ‘continue loop
    Loop

    WScript.Echo “Hello World!”

    Reply
    • Ryan McCormick says

      August 5, 2015 at 11:33 am

      It looks like you may need to pass a password in the connection string. I am writing this comment from my iPhone so I don’t have the ability to post a good example but I found this https://www.connectionstrings.com/access/ the site looks like it has some good examples of this.

      Reply
  4. zunaid says

    September 18, 2015 at 2:11 pm

    Yes It worked for me. I had to create a shortcut and edit property – target – with- %windir%SysWoW64wscript.exe C:UsersusernameDesktopexampleexample.vbs

    Reply

Trackbacks

  1. SOLVED: Provider cannot be found. It may not be properly installed says:
    June 5, 2014 at 3:10 pm

    […] I am writing this one as part two of my post: How to Connect to Access Database with VBScript […]

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

Recent Posts

  • Force Quit Kill all Chrome Windows MacOS
  • SOLVED: Angular 6 CLI Karma Stuck in Single Run | Karma Stops Running
  • How to Manually Install Java 8 on Ubuntu 18.04 LTS
  • Remove VirtualBox from Ubuntu 16.04 Xenial
  • Clear all Node Modules Folders Recursively Mac/Linux

Recent Comments

  1. KKV on Webstorm adding spaces between imports and braces | JavaScript and TypeScript
  2. jusopi on Clear all Node Modules Folders Recursively Mac/Linux
  3. Qaisar Irfan on Clear all Node Modules Folders Recursively Mac/Linux
  4. mustafa on Remove VirtualBox from Ubuntu 16.04 Xenial
  5. Pourya on How to Manually Install Java 8 on Ubuntu 18.04 LTS

Copyright © 2025 · Magazine Pro on Genesis Framework · WordPress · Log in