问题
Not sure this is possible, but it seems like it should be doable.... it is 2013 after all! And some forum posts I found suggest it is, but I have not been successful in getting it to work...
I have an Access 2010 db with a macro that downloads files from 3 different web sites and then proceeds to import the data and process it. It runs for 1hr and 17 mins.
I want to schedule this macro to run at 4am so it is all done and dusted by coffee time and work start 8am... So I created a VBScript to run this, which will then be placed into the Task Scheduler for the desired time.
It is a single user DB, my use only on my PC.
I have done some research but I cannot seem to get this working. Here's what I have so far:
Macro inside "Main" module in Access 2010 inside of "Pricing Model.accdb":
Public Sub Download_And_Import()
ProcStep = ""
ExecStep = 1
DoCmd.SetWarnings False
'Empty the Execution Progress table
DoCmd.RunSQL "DELETE * FROM EXECUTION_PROGRESS"
Call Update_EXECUTION_PROGRESS(Format(Now(), "YYYY/MM/DD HH:MM:SS"), ExecStep, "Starting Download_Files Main Procedure...")
Call Download_Files.Download_Files
Call Update_EXECUTION_PROGRESS(Format(Now(), "YYYY/MM/DD HH:MM:SS"), ExecStep, "Finished Download_Files Main Procedure...")
Call Update_EXECUTION_PROGRESS(Format(Now(), "YYYY/MM/DD HH:MM:SS"), ExecStep, "Starting Import_Files Main Procedure...")
Call Import_Files.Import_Files
Call Update_EXECUTION_PROGRESS(Format(Now(), "YYYY/MM/DD HH:MM:SS"), ExecStep, "Finished Import_Files Main Procedure, closing application now...")
DoCmd.SetWarnings True
End Sub
I then created the following VBScript to run Access VBA Macro externally:
dim accessApp
set accessApp = createObject("Access.Application")
accessApp.OpenCurrentDataBase("G:\Pricing DB\Pricing Model.accdb")
accessApp.Run "Download_And_Import"
accessApp.Quit
set accessApp = nothing
I get the message "Illegal function call, Line 4" which is the step:
accessApp.Run "Download_And_Import"
Any ideas / help is highly appreciated! Thanks in advance!
M
回答1:
I don't see anything wrong with the VBScript itself. I copied your code, changed the db file name and procedure name, and it ran without error. So my guess is the VBScript error "bubbles up" from Access when it tries to run the procedure.
Open Pricing Model.accdb
in Access, go to the Immediate window (Ctrl+g), type in the following line and press Enter
Application.Run "Download_And_Import"
If that throws an error you surely have a problem in that procedure. If it doesn't throw an error, you could still have trouble running it from a scheduled task if the procedure requires Windows permissions and you haven't set the task to run under your user account.
If you need to troubleshoot the procedure, first add Option Explicit
to the module's Declarations section. Then run Debug->Compile from the VB Editor's main menu. Fix anything the compiler complains about. Repeat until the code compiles without error.
Disable DoCmd.SetWarnings False
during troubleshooting because it suppresses information. Consider whether substituting the DAO Database.Execute
method for DoCmd.RunSQL
will allow you to avoid turning SetWarnings
off at all.
回答2:
You could try including the ProjectName:
accessApp.Run "[Pricing Model].Download_And_Import"
The project-name defaults to the database name, and square-brackets are needed because of the spaces.
This shouldn't be necessary, because your Sub is Public, but worth a try.
回答3:
I ran into the same (msg) issue the script was OK! Problem was i was testing the script with the database opened as I was in development mode, as soon i closed the Database the script worked well.Close the database before testing the script.
来源:https://stackoverflow.com/questions/17637655/running-vba-macro-within-access-2010-from-external-vbscript