unable to call a sub in a module

smiler44

Registered User.
Local time
Today, 01:35
Joined
Jul 15, 2008
Messages
671
unable to call a sub in a module [solved]

I have a userform with a command button on it. The code associated with the commandbutton is call nexttwenty. The sub nexttwenty is in a module. I have tried sub nexttwenty () and public sub nexttwenty() but on the line call nexttwenty I'm getting a user defined error massage. It is as if the sub nexttwenty does not exist.

please can you advise?

thanks

smiler44
 
Last edited:
Have you declared your sub as Public in the module
 
Can you show us the very first line of your sub
 
A search via Google today gave me the answer.
Instead of call nexttwenty it needs to be call userform1.nexttwenty.

I'm surprised I have never had this problem before

smiler44
 
A search via Google today gave me the answer.
Instead of call nexttwenty it needs to be call userform1.nexttwenty.

I'm surprised I have never had this problem before

smiler44
So, for the sake of clarity, would be right in assuming that you sub is a forms module rather than a general module.
 
  • Like
Reactions: Rx_
Bob, I think so. I'm calling a sub routine that is part of a userform from a sub routine that is in an ordinary module. I could possibly move the routine from the userform to an ordinary module but as I have solved the problem, no need.

smiler44
 
I thought i'd posted that I'd solved this.
I needed to add userfrom1. to the front of the routine being called
so call userform1.start for example instead of call start

smiler44
 
you should move your public sub to a Module, not in the userform. if there is no module just add from menu and cut and paste your code there.
 
you should move your public sub to a Module, not in the userform. if there is no module just add from menu and cut and paste your code there.

any reason for doing this apart from having all code in a module?
smiler44
 
You need to understand the concept of "scope". It is all about where the function (or variable or sub) is meant to be available within the Project.

A Private function (or Sub) within an Access Object Module is only available from within the module where it is located. It can be called using its name alone (plus any parameters). These functions are used to do jobs that don't need to be initiated from outside the object. The Event Procedures called by objects within the module are typical examples.

A Public function in an Access Object Module becomes a Method of the object and can be called using the object name and function as you have found. These are used when the Method needs to be called from outside the object.

A Public function in a Standard Module has a scope of the entire Project and can even be called from queries by simply using its name (and parameters if required). By default a function in a Standard module is Public if its scope not explicitly declared.

A Private function inside a Standard Module only has scope within the module. These are used when the function is only intended to support the public functions of that module.

For best practice, the scope should always be limited to the minimum requirement because it is far less likely to become confused with other functions which have been given the same name within other scopes. When a function is called, Access looks first at the scope of the call. If it finds a match then that version is used. If not then it will look to a Public function in a Standard module. This allows different objects to have the same function names and even for the the same function name to be used at the Project wide scope.

If the function is only required for use within particular object module then that is where it should be placed. It is completely inappropriate to place a general purpose Public function inside an object module and call it from outside. Only Methods of the object (ie things that apply only to that object) should be used in this way.
 
Last edited:

Users who are viewing this thread

Back
Top Bottom