user441521
user441521

Reputation: 6998

Run macro and sub in access from vbscript

I have a VBScript and need to run a macro and insert data into a table. To run the macro I create an Access.Application object and use the Run function:

Dim oAccess
Set oAccess = CreateObject("Access.Application")
oAccess.OpenCurrentDatabase(accessFile)

oAccess.Run macroName

Now I need to insert some records and to do that I need to use an ADODB.Connection and Recordset. However normally I would call Connection.Open but the database is already open from the Access.Application. Is there a way to not have to open the Access file twice and instead get the ADODB.Connection from the Access.Application object?

Upvotes: 1

Views: 1118

Answers (1)

HansUp
HansUp

Reputation: 97101

oAccess.CurrentProject.Connection is an ADO Connection for the database which is currently open in that oAccess session.

Use that Connection directly, or Set an object variable to it:

Dim cn
Set cn = oAccess.CurrentProject.Connection

Upvotes: 1

Related Questions