MS Access RunCode Macro cannot find my procedure

心不动则不痛 提交于 2019-12-28 16:36:27

问题


I'm only posting this since I wasn't able to find a solution anywhere. I finally figured it out. Kind of silly really.

When using the RunCode property within an Access Macro, I was trying to run a Sub from my global module. I was getting the error "The expression you entered has a function name that database can't find." I couldn't figure out what the issue was. I followed the advice of everyone that posted on this issue, which was mostly the following:

  1. Use () at the end of the procedure name
  2. DO NOT use the "=" before the procedure name

Still didn't work!


回答1:


THEN I read the error message carefully. It mentions that it could not find the FUNCTION name. Apparently, the RunCode property specifically requires a "Function" not a Sub. So, I simply changed my Sub to Function and it worked fine!

Hope this helps.




回答2:


Another solution that worked for me:

The module name can NOT have the same name as the procedure(s) in the module(s).




回答3:


I had a similar issue with the error message. My VBA code had the following declaration:

private function MyFunction()

....

end function

I removed private declaration to get the Macro Runcode to execute the MyFunction()

For example:

Function MyFunction()

End Function



回答4:


Access 2013: A function called with MyFunction() from RunCode where MyFunction does not exist gives me error 2425. None of the above work for me, however, and I was still getting Error Number 2001 when the function exists and is public. Database is in a Trusted Location. No compile errors, but something in MyFunction did not work, namely

DoCmd.ShowAllRecords 

after GoToControl worked to select my subform. Real problem was my code to remove a filter with VBA. Manual for ShowAllRecords seems to indicate that this should work, but replacing DoCmd.ShowAllRecords with

DoCmd.RunCommand acCmdRemoveFilterSort 

fixed my problem.




回答5:


The database seems to need to have objects in it other than the VBA function being called as well. Without other objects (specifically a table in my case), the function is unable to be found from within the calling environment (eg Excel VBA).




回答6:


My mistake was putting the function in a Class Module instead of a regular module.




回答7:


Access Office 365: My subroutine works when called from within VBA and used to work in Macros. Moved function text to a separate module, saved it by itself. Gave the module (not class) a unique name.




回答8:


I was borrowing a "template" vbasic text from online as a shell. And while I renamed the function in the code section, MSAccess/Vbasic did not show the name change in the function heading of the Module box so when I ran the macro which called this function it said it couldn't find it. Tried repeatedly to fix. Then I noticed the name was different in the code section of vbasic versus the heading of the function dialog box. So I changed the name manually in the heading box and it prompted me to save which I did and now it works. Maybe this is why snake software is so popular. :)



来源:https://stackoverflow.com/questions/17244980/ms-access-runcode-macro-cannot-find-my-procedure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!