VBScript to run Access Databse Automatically

Status
Not open for further replies.
I'm attempting to automate a process at my job where we import data into an Access Database, run a series of queries to modify the data, then export the modified data. What I'm attempting to do is just make a VB script in the folder that you click on and then it does everything automatically. I know I can open the database using the following code:

Code:
Set ProcessDB = CreateObject("ADODB.Connection")
ProcessDB.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"User ID=;Data Source=" & ProcessDatabase

Then put what I want to execute between

Code:
ProcessDB.Open
ProcessDB.Close

But I'm unsure how to get it to run a module or a query. All I've ever done with this before is execute SQL code. Is there some way to run a module using this?
 

Fristie Blade

Ars Tribunus Militum
1,722
Subscriptor++
If you are using the "ADODB.Connection" COM interface, then you will not be able to run functions/subs that are defined in modules (if I am not mistaken, it's been a while since I used this).
With that interface you can
- query data
- insert / update / delete data
- make data changes
- call stored procedures (Those pre-saved update-queries/insert-queries or delete-queries in Access)

If you really need to call functions from modules, you will need to use the "Access.Application" COM interface. (Access will need to be installed on the machine where the VBS is running)
Something like this might do it:
Code:
Set appAccess = CreateObject("Access.Application")
appAccess.OpenCurrentDatabase "C:\test.accdb"
appAccess.Run "testproject.testprocedure"
Set appAccess = Nothing
But it would probably not run due to security restrictions. And you usually don't want to ease up up those restrictions

If your goal is only to import data into Access, modify it with predefined stored procedures and export again, you'd better stick to the "ADODB.Connection" as you started off with.
This will not require Access to be installed on the machine where the vbs is running. All you need is the ADODB to be installed (MDAC 2.8 or so)

As for ADODB programming, lots of information on the internets available ...

For inserting I would open a recordset on the access table with a client cursor and update all inserts in batch.
Then call your presaved-update-queries to transform the data
Then open a recordset with a firehose cursor to pump out the data again.

Good luck
 
Ravensroke":1e6yp9g0 said:
If possible, upgrade the database to SQL CE or SQL Express and use the .Net API's, figuring out how to do it properly now will save you tons of headache later.

IT won't support that. The software I have installed now is what I've got to work with.

Thanks for all the info Firsty Blade, I'll try that and see if I can get it to work.
 

Fristie Blade

Ars Tribunus Militum
1,722
Subscriptor++
arthur barnhouse":3j6kgysb said:
Ravensroke":3j6kgysb said:
If possible, upgrade the database to SQL CE or SQL Express and use the .Net API's, figuring out how to do it properly now will save you tons of headache later.

IT won't support that. The software I have installed now is what I've got to work with.

Thanks for all the info Firsty Blade, I'll try that and see if I can get it to work.

You're welcome. If you got any more questions/issues while doing data import/export over adodb recordsets, let me know. Did those things quite a lot in the past.
 
The access.application COM interface actually worked. I can use it to run a procedure, which I wasn't able to do with the ADODB.Connection. But I've run into one other snag. Is there a way to pass a declared variable that has a path stored in it from the VB script to the VB procedure in the access database? If not I think I can work around it, but if there's a way, it would help a lot.
 

Fristie Blade

Ars Tribunus Militum
1,722
Subscriptor++
arthur barnhouse":nkyomm0m said:
The access.application COM interface actually worked. I can use it to run a procedure, which I wasn't able to do with the ADODB.Connection. But I've run into one other snag. Is there a way to pass a declared variable that has a path stored in it from the VB script to the VB procedure in the access database? If not I think I can work around it, but if there's a way, it would help a lot.

Sure you can, define your procedure to have parameters.

Suppose you following VBA procedure in Access, defined in a code module named "testmodule"
Code:
Public Sub testprocedure(aMessage As String)
    MsgBox aMessage
End Sub

Within other VBA code of the same access project you might call it like this
Code:
Dim p1 as String
p1 = "Ars Technica"   ' First parameter passed on to the procedure  
testmodule.testProcedure p1

The similar thing through the com interface for your vbscript would be
Code:
p1 = "testmodule.testprocedure"  ' This one is the procedure to call (mandatory)
p2 = "Ars Technica"        ' First parameter passed on to the procedure  (optional)
appAccess.Run p1, p2

The run command accepts a variable nr of parameters.
The first parameter should always be the name of the procedure to call and all the optional parameters are passed on towards the procedure you will call.

There is great documentation about the com interface within the VBA editor of access, if you d ever need it.
 
I'm running into a problem with the code you suggested:

Code:
p1 = "testmodule.testprocedure"  ' This one is the procedure to call (mandatory)
p2 = "Ars Technica"        ' First parameter passed on to the procedure  (optional)
appAccess.Run p1, p2

The variable I need to pass isn't stored in a module or a procedure, it's stored in the VBScript that i run initially to start up the database. So I need a way to pass that variable. This would only seem to work if I wanted the module to pull information from another module.

Edit: nevermind, I just misunderstood part of the syntax of what you were showing me. This worked perfectly. Thanks again!
 
Status
Not open for further replies.