VBA Sub or function not defined error

This error can happen when you try to run a sub routine in VBA MSACCESS immediate window.

This can happen if you tried to create a routine in a class module instead of a module.

Notice how the test code works fine in module 1.

Sample vba sub routine compiles and runs in module1.

However when the same sample code is run in class1 the sub routine fails and generates the error: Compile Error: Sub or Function not defined..

Error is generated when attempting to run a routine in class1.

Also you can get the Sub or Function not defined error message when you incorrectly call the sub routine. For example if  you make a spelling error or have a typo in your call in the immediate window, the sub or function not defined error will appear as shown below. The sub routine is named ‘test’ but the calling code in the immediate window calls ‘test2’.

The sub or function not defined error can happen when you have spelling error in your calling code.