One line subroutine does not run... (1 Viewer)

johnv

New member
Local time
Today, 06:16
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
 

MStef

Registered User.
Local time
Today, 14:16
Joined
Oct 28, 2004
Messages
2,251
Put the cursor in first line of your sub.
 

johnv

New member
Local time
Today, 06:16
Joined
May 17, 2009
Messages
3
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
 

MStef

Registered User.
Local time
Today, 14:16
Joined
Oct 28, 2004
Messages
2,251
I don't know, when I try it works.
 

!Blue

Registered User.
Local time
Today, 14:16
Joined
Jul 4, 2008
Messages
28
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
 

johnv

New member
Local time
Today, 06:16
Joined
May 17, 2009
Messages
3
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
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 14:16
Joined
Sep 12, 2006
Messages
15,660
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)
 

!Blue

Registered User.
Local time
Today, 14:16
Joined
Jul 4, 2008
Messages
28
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

Top Bottom