Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Access VBA prompt me for a Macro when I press F5 or Run Sub?

Tags:

vba

ms-access

I found this question Why does Excel VBA prompt me for a Macro name when I press Run Sub but the answers don't help me because my sub doesn't have arguments and is not triggered by an event.

I have two subs. One in a Module and one in a Form.

Sub ModuleSub()
    MsgBox ("Hello World")    
End Sub


Sub FormSub()
    MsgBox ("Hello World")
End Sub

The Module Sub DOES RUN when I press F5, but the Form Sub doesn't. It brings up the Macro dialog. What is the problem here?

like image 976
PBeezy Avatar asked Aug 13 '15 14:08

PBeezy


1 Answers

Macros stored in modules are independent and can be ran just like that.

However, all the functions/subs stored in Form modules or in Class modules are in fact the methods of this form/class and they cannot be ran without instance of this form/class.


If you open Immediate window (CTRL + G) you can run this code:

Call ModuleSub

without any issues.


This code:

Call FormSub

will return Compile error: Sub or function not defined.

However, if you add the name of form before the name of the function (like below):

call Form_FormName.FormSub 'replace FormName with the name of your form.

this sub will be also invoked without any issues, since you dynamically create an instance of form and VBA compiler can use its methods.

like image 139
mielk Avatar answered Oct 23 '22 05:10

mielk