Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Opening Access from Excel VBA

Edit: The answer to this question can be found within the comments of the accepted answer.

I am attempting to open an Access database from a button click within my excel file. I currently have this code:

Private Sub bttnToAccess_Click()

Dim db As Access.Application

Set db = New Access.Application
db.Application.Visible = True
db.OpenCurrentDatabase "C:\Users\wcarrico\Desktop\wcarrico-CapstoneFinalSubmission.accdb"

End Sub

This seems to work briefly and then Access shuts down almost immediately. If it matters, the Access file has an AutoExec macro that runs through a few tests itself on open.

like image 506
Bradley Carrico Avatar asked May 01 '13 03:05

Bradley Carrico


4 Answers

Don't try to open the Access application then; just create a connection object using one of the Data Access technologies: - OLE-DB or - ODBC.

Google "ODBC Connection strings" or "OLE-DB Connection Strings" to get details depending on your particular configuration (and Access filetype).

Probably ADODB is the easiest current library to use for your data access.

Update: Try Importing the data from Access then using the Data -> From Access wizard. Yu can always use the Macro recoding facility to automatically generate some VBA code for you, that will create some infrastructure for you; I use this regularly when exploring new portions of the VBA object model.

Update - Final resolution of problem, from comments below
That may be because the variable goes out of scope; move the declaration of db outside the function, to module level

like image 109
Pieter Geerkens Avatar answered Nov 15 '22 20:11

Pieter Geerkens


The code started Access by creating an application instance assigned to an object variable. At the end of the procedure, the variable went out of scope so Access shut down.

You accepted an answer to use a module-level variable for the Access application instance. In that case, Access remains running after the procedure ends. However if the user exits Excel, Access will close down too.

If the goal is to start Access and leave it running until the user decides to close it, just start Access directly without assigning the application instance to an object variable (Set db = New Access.Application). That db variable would be useful if your Excel code needed it for other purposes. However, it's actually only used to open the db file.

You can use the Run method of WScript.Shell to open your db file in an Access session.

Private Sub bttnToAccess_Click()
    Const cstrDbFile As String = "C:\Users\wcarrico\Desktop\wcarrico-CapstoneFinalSubmission.accdb"
    Dim objShell As Object
    Set objShell = CreateObject("WScript.Shell")
    objShell.Run cstrDbFile
    Set objShell = Nothing
End Sub
like image 33
HansUp Avatar answered Nov 15 '22 20:11

HansUp


I know this is an old thread, but you will get this error in Excel VBA if you are trying to open an Access database, but you do not have two specific References clicked. (Tools, References on the VBA Editor screen). You need to click 'Microsoft Access 15.0 Object Library' and 'Microsoft ActiveX Data Objects 6.1 Library'.

like image 22
Derek Sturdy Avatar answered Nov 15 '22 21:11

Derek Sturdy


Remove the New declaration then it works

like image 30
Ant Avatar answered Nov 15 '22 20:11

Ant