One line subroutine does not run...

johnv

New member
Local time
Yesterday, 23:03
Joined
May 17, 2009
Messages
3
Hi all

This must be really simple.

I have Access 2003. I created a new database, with one form, with one text field.

In the VBA editor for the form I have created a function in the form's module, which is

public sub test1()
msgbox "Hi"
end sub
I have compiled it. But if a select Run | Run Sub on the editor menu to run it, Access displays its dialog box for creating a macro, with just the cancel button highlighted. I don't get the message box.

I thought that I could run any procedure by just putting the cursor in the code and then selecting Run | Run Sub.
Why does it want a macro name? can I only run subroutines from a general module?

regards
John
 
Put the cursor in first line of your sub.
 
Thanks Mstef
However, it does not matter which line I have the cursor in - I just get the macro edit box popping up when I try to Run.
John
 
Yes - you can only run code like this from a standard module... the form is effectivly a class module - which requires instantiation before its code becomes active
 
Thanks !Blue

I guess that Access is trying to find a macro because its not allowing the subroutine of the same name to run.

Re instantiation, I have the form open when I try this, so is it not instantiated then?

John
 
you can run code in a form, from a button on the form

but you can only DIRECTLY run code that is in a code module.

the point is that the code in the FORMs code module only exists, as it were, when the form is open, so you can never run it directly (I think)
 
I'm regretting putting that word in...

Yes the form is instantiated when it is open - but the code inside doesn't know that... you could have instantiated many instances of the form - the code inside wouldn't know which one you were referring to...

Hopefully that goes some way to explaining it - I could go on but I'm not into lengthy explainations - if you're keen to understand better - I'd reccomend getting a book on OOP
 

Users who are viewing this thread

Back
Top Bottom