简体   繁体   中英

Running a MS Access 2016 VBA function through a Macro, then Running that Macro through Excel

I am editing a project and trying to take a VBA function that is currently run by a button click on a form, and run it through a macro so I can then call it to run in Excel.

The current sub reads something like the following.

Public Sub Command42_Click()

'Clear All Tables
DoCmd.RunSQL "DELETE * FROM dbo_Tbl1"
DoCmd.RunSQL "DELETE * FROM dbo_Tbl2"
DoCmd.RunSQL "DELETE * FROM dbo_Tbl3"
DoCmd.RunSQL "DELETE * FROM dbo_Tbl4"
DoCmd.RunSQL "DELETE * FROM dbo_Tbl5"
DoCmd.RunSQL "DELETE * FROM dbo_Tbl6"
DoCmd.RunSQL "DELETE * FROM dbo_Tbl7"
DoCmd.RunSQL "DELETE * FROM dbo_Tbl8"

  Dim strSQL     As String

  Dim strLocalTable  As String

'Data Download

  With CurrentDb.QueryDefs("setup_PTQ")
     .SQL = "select * from SQL_Server_tbl"
  End With

  strLocalTable = "dbo_tbl1"

  CurrentDb.Execute "INSERT INTO " & strLocalTable & " SELECT * FROM setup_PTQ"

DoCmd.OpenQuery "qry_1"
DoCmd.Close acQuery, "qry_2"

End Sub

I have shortened the code for the sake of brevity, but the 'Data Download step is repeated for tables 2 - 8 as well.

This code works great in the Access form button and needs no edits.

When I copy this code into a standalone module and run it, it also works.

My problems begin when I try and call the newly developed Module in a macro.

I set up a run code step, enter the new function name (for example I change Command42_Click() to Test() and that is what I call) and then it returns an error.

A message box appears with text stating:

"The Expression you entered has a function name that Microsoft Access can't find"

and then tells me the error number is 2425 and that the argument "Test()" is what cannot be found.

Ultimately, I want to call this macro with a .RunMacro VBA expression in Excel so the end user never has to touch the MS Access DB.

At the end of the day, I am just looking for a way to run the code above from Excel so if someone can offer a better solution than a fix to this error, I am open to that as.

Thank you in advance!

You'll need to change your Public Sub to a Public Function (or just Function , since Public is the default scope) residing within a module for it to be able to be evaluated from within the RunCode action of an MS Access Macro.

From the MS Access documentation :

You can use the RunCode macro action in Access desktop databases to call a Visual Basic for Applications (VBA) Function procedure.

Alternatively, you can create a function which simply calls your Sub , eg:

Function Test()
    Call Command42_Click
End Function

And then call this function from the RunCode action of your macro.

Don't use DoCmd.RunMacro

Don't use a macro

Use appAccess.Run "MyNewModule.Command42_Click"

An additional advantage of .Run is that it runs subroutines as well as functions.

Try this:

  1. Open your RunCode macro in design mode.
  2. Click to edit the function name
  3. Press Ctrl+F2 to open the Expression Generator dialog
  4. Within the Expression Generator, navigate to your function clicking on the name of your database file, the name of the module and then the name of the function.

At first I got the same error 2425 you describe. After going through the Expression Generator to select the function, it worked smoothly. My Access database file has a .mdb extension.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM