Reputation: 55
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!
Upvotes: 1
Views: 3641
Reputation: 133
Try this:
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.
Upvotes: 0
Reputation: 2638
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.
Upvotes: 1
Reputation: 16015
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
, e.g.:
Function Test()
Call Command42_Click
End Function
And then call this function from the RunCode
action of your macro.
Upvotes: 3