MS Access RunCode Macro cannot find my procedure - vba

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:
Use () at the end of the procedure name
DO NOT use the "=" before the procedure name
Still didn't work!

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.

Another solution that worked for me:
The module name can NOT have the same name as the procedure(s) in the module(s).

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

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

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.

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).

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.

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. :)

My VSTO C# calls to app.Run("MyMacro") stopped working suddenly and the error message started printing. ("Access cannot find the procedure MyMacro.")
The problem on my machine was that my code (which connected to a running instance of Access) was connecting to a zombie database instance that did not have that VBA macro in it. Go figure. The zombie instance did not show in the Taskbar, in the Alt-TAB display, or in a list of windows that I printed out using a console program.
The way I discovered it was to have my VSTO code print out the name of the database that it was connecting to. The Access database was one that I had been working with earlier in the day, without errors. I had closed the Access app holding it successfully hours before I tried running MyMacro.
I can't think of anything that I did that was unusual, other than upgrading VStudio to the latest version. Probably a reboot will fix things. What a weird problem.

Related

How to call an afterupdate event from another module/control event (MS ACCESS VBA)

I have been using Microsoft access for a while however i seem to be having some difficulty with a single line of code. i know it is possible to trigger a form controls event from another module or event as i have done before in VBA for excel, however when i try to do this in MS Access it gives me a runtime error
any and all help would be appreciated in this matter as I'm sure there is probably something very basic that I'm missing here.
Please see below code for easy reference.
Forms("Insert_Data_Others").CMBVoy_ID_Oths.AfterUpdate
Call CMBVoy_ID_Oths_AfterUpdate
Make sure procedure is not declared as Private.
Use underscore in place of the last period.
Forms("Insert_Data_Others").CMBVoy_ID_Oths_AfterUpdate
or this version which will show the event in intellisense tips.
Form_Insert_Data_Others.CMBVoy_ID_Oths_AfterUpdate

vba Show references to other procedures

Today i want to ask this question.
say i have a module and in that module i call different procedures (subs or function ) which are located in other modules, how can i list all the procedure names and their module or class in a simple way in case i wanted to copy those procedures to the current module and delete these modules.
thanks
i don't know how to achieve that but here is a dummy code
for each UserDefinedProcedure in UserProcuduresCollection
if UserDefinedProcedure.Name (is Found in this module) then
debug.print UserDefinedProcedure.Name
end if
next
DISCLAIMER: I wrote this part of the Rubberduck add-in myself, and I own and maintain the Rubberduck project.
Download and install Rubberduck. Right-click the procedure name (from its declaration, or any of its usages) and select the "Find all references" command from the code pane context menu:
You can then double-click an item in the list to navigate there.
The latest release dates a little (July 2015), but the project is well alive on GitHub, and version 2.0 should be released in 6-8 weeks. It won't let you iterate procedure/identifier references with VBA code, but a COM API to do exactly that is on the project's roadmap.
Rubberduck 2.0's "find all references" can find references to pretty much anything, from classes:
To properties:
...and library functions:
Doing this with code, even very smart code, using the VBIDE API, isn't going to be reliably possible. The reason for this is that it's perfectly legal to have this code in Module1:
Sub DoSomething()
'do something
End Sub
And then this code in Module2:
Sub DoSomething()
'do something
End Sub
To correctly resolve references to DoSomething inside Module1, you need to check whether the call is qualified (e.g. Module2.DoSomething will call the procedure inside Module2 - an unqualified call will call the procedure inside Module1).
Resolving identifier references is something I've spent pretty much an entire year refining the code for, and it's still not perfect (although it now resolves crazy ambiguous code you wouldn't even think is legal VBA) - doing that in plain VBA is suicidal at best.
Eventually we'll expose a COM API that will allow Rubberduck users to write VBA code something like this completely hypothetical code:
For Each declaration In RubberduckParserState.UserDeclarations
If declaration.DeclarationType = Procedure And declaration.ParentScope = "VBAProject.Module1" Then
For Each reference In declaration.References
Debug.Print declaration.IdentifierName " used in " & reference.ToString
Next
End If
Next
In break mode, press Ctrl+L to view the call stack. This is the best you're gonna get in the VBE.
Note that the call stack cannot be accessed via code and so what appears to be your end-goal cannot be achieved in VBA.

excel vba user type not defined after removing class module

I have an Excel VBA Project which included a custom class module. I changed the code to remove all references to this class, then removed the class module after exporting it. The program then had a compile error
user type not defined
I re-imported the custom class and the program compiled normally. I deleted all code and comments from the custom class module, leaving it as a blank module, and the program still compiled OK. Again, after removing the custom class I get the "user type not defined" error.
No offending code line is highlighted at any of the above steps.
Any ideas?
The problem was experienced using Excel 2016.
The problem was solved by opening the file in Excel 2007, which I fortunately retained before installing Office 365. In Excel 2007 I was able to remove the now empty class module and re-compile and save. On re-opening in Excel 2016, the project again compiled just fine.
I repeated the process on another backup copy with both Excel 2007 and 2016 and the problem consistently occurs with Excel 2016 but not with Excel 2007.
Thanks for other's comments.
I had a similar problem with the same message "user type not defined" running Excel 2007.
The problem arose when I was updating the code in a class module and removed an old private user-defined type and all references to it. This then raised the compiler error "user type not defined".
My temporary solution was (for this description let the type name be "TName"):
Remove "TName" and all references to it from the class module.
Create a new empty class module and name it "TName".
Recompiled and save the project.
Remove the newly created class module "TName".
Recompiled and save the project.
Exit all instances of Excel.
Restart excel with the project.
Done.
Note!
This removes the compiler error "user type not defined" for the moment. However, if any other error appears and it has not even have to be related to the "user type not defined" issue. The error "user type not defined" reappears. So, this is a description to remove the error for the moment. But if further development is done, it is most likely that the error will reappear and if it is not documented thoroughly it will be even harder to fix, since then you probably have forgotten the type name that originally caused the error!
This is a rough description of what I have done. However, I thought I would contribute my work here and let the next person come up with a better solution and description.

Debug function altough macros disabled

I was given a template for report (.dotm) with alot of vba code behind that gets data from a mssql database and writes the data at some bookmarks.
Unfortunately, the code has an error and I have to fix it.
Since I have never done vba before, I'm a bit lost.
There is a function with 2 parameters that does everything.
Can you tell me how I can debug this function?
I have set a breakpoint to the first line but how can I tell word(?) to execute the function?
My first approach was to add a button and set the onclick event to this function. But unfortunately macros are disabled by our policy and this doesn't work :(
Can you please help me out how I can get inside this function?
How can anyone expect the code in the template to work if macros are disabled?
Try ALT+F11 to get into the VBA environment and show the code. Put the cursor in the code you want to debug and press F8 to step into it or F5 to run it to any breakpoint.. If the code you want to debug has parameters, you have to write a dummy Sub to call the function with test-parameters.
Also, see sams comment about the trust center. Maybe you can change the settings yourself to allow macros.

How do I call a macro function in a global template/add-in from another template?

First, I am a VBA newbie. So please pardon my ignorance.
I have a global template that I installed in the Word startup folder.
The global template has some macros that I would like to access. But I can't figure out how to access the macro.
I tried to use Application.Run, but that doesn't work (or at least I'm doing it wrong). I keep getting Runtime Error 424.
I also tried to use something like Call globaltemplatename.modulename.functionname but that doesn't work either.
I also tried to simply call the function but then it says that the function/sub is not defined.
LOL... um... nvm... I found out why...
I didn't create a reference in the template that I was calling from...
Well...in case anybody else runs into this problem, you have to open the template (not the global template) and then go to Tools > References in the VB editor. And then check the project name of the global template.
sTemp = Application.Run ("modulename.functionname")
The entire string is in double quotes.
Word does not want the templates name - leave it out.
Even more Word do not let you use more than ModuleName.ProcName. Otherwise a runtime error will be thrown.