0

I have an autoexec macro in a microsoft access database that I would like to execute everytime I open an excel file. Essentially, I need to open the access database to trigger the autoexec and then close again.

I have some VBA code in my excel file already to date/timestamp individual records (worksheet code) but I was hoping there is some functionality in excel that I can utilize when the file is opened to trigger this access process.

Can anyone share some code they have used for this type of functionality or share any excel functionality that I'm not aware of to accomplish this same action?

Thanks in advance!

1
  • Wouldn't it be simpler to just do whatever the autoexec is doing in Excel instead? Opening up Access seems like overkill Commented Mar 26, 2018 at 16:23

2 Answers 2

1

Executing a macro from Excel using VBA is pretty simple. You can use the following VBA to open an Access database, and then close it again.

With CreateObject("Access.Application")
    .OpenCurrentDatabase "path\to\my\file.accdb"
    'AutoExec macro should fire automatically
    .Quit
End With
7
  • Thanks so much! Should I be able to copy directly into my excel VBA sheet and execute or is there additional code surrounding this that is needed? Commented Mar 26, 2018 at 16:17
  • This should be enough. Just fill in the file location. Of course, the code needs to be added to a sub, and needs to be triggered. You could add it in the ThisWorkbook module, to the Workbook_Open sub to execute it automatically when the workbook opens.
    – Erik A
    Commented Mar 26, 2018 at 16:21
  • so I created the following code block: Private Sub Workbook_Open() With CreateObject("Access.Application") .OpenCurrentDatabase "path\to\my\file.accdb" 'AutoExec macro should fire automatically .Quit End With End Sub Commented Mar 26, 2018 at 16:51
  • I'm getting the following error though: "Compile error: Invalid outside procedure" Commented Mar 26, 2018 at 16:53
  • Can you please provide the code of the entire module in your question (so you can format it)? This seems to refer to code outside of a sub or function. The code you're providing doesn't have any, so I can't explain that error.
    – Erik A
    Commented Mar 26, 2018 at 16:57
0

To run an Access script when Access opens, make sure you have a FUNCTION (not a sub) in a standard Module.

Option Compare Database

'------------------------------------------------------------
' AutoExec
'
'------------------------------------------------------------
Function AutoExec()
On Error GoTo AutoExec_Err

    DoCmd.RunCommand acCmdWindowHide
    MsgBox "Welcome to the client billing application!", vbOKOnly, "Welcome"
    DoCmd.OpenTable "Orders", acViewNormal, acEdit


AutoExec_Exit:
    Exit Function

AutoExec_Err:
    MsgBox Error$
    Resume AutoExec_Exit

End Function

Keep in mind, there are several ways to control Access from Excel. Here is one option.

Global oApp As Object

Sub OpenAccess()

   Dim LPath As String
   Dim LCategoryID As Long

   'Path to Access database
   LPath = "C:\Users\Excel\Desktop\Coding\Microsoft Access\Northwind.mdb"

   'Open Access and make visible
   Set oApp = CreateObject("Access.Application")
   oApp.Visible = True

   'Open Access database as defined by LPath variable
   oApp.OpenCurrentDatabase LPath

   'Open form of interest
   oApp.DoCmd.OpenForm "Form1"

End Sub

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.