Running code from the VBA editor Window

S

Stuart McCall

Wavequation said:
Can I run code directly from the VBA editor window?

Certainly. If you want to execute a procedure that takes no parameters,
click inside the proc, then press F5. Also you can use the Immediate window
(Ctrl-G opens it) to execute arbitrary code snippets.
 
G

Guest

That was easy! In the past, I have tried to run event procedures for form
controls, and a dialog box opened up asking which macro to run or
something... why is a standalone module different?
 
S

Stuart McCall

Wavequation said:
That was easy! In the past, I have tried to run event procedures for form
controls, and a dialog box opened up asking which macro to run or
something... why is a standalone module different?

Because a procedure in a standard module is not dependant on an object (ie a
form) being loaded.
 
A

Albert D. Kallal

Wavequation said:
That was easy! In the past, I have tried to run event procedures for form
controls, and a dialog box opened up asking which macro to run or
something... why is a standalone module different?

Because forms modules (and class modules) require you to create a running
instance of the object BEFORE you attempted to run the code.

There is certainly different behavior when you try to run code in a class
module (answer: because class modules are different then standard code
modules).

So, if you inset a class module, or open up a forms code module (they are
BOTH considered class modules). You can't run the code inside of this module
UNTIL an instance of that form (and it corresponding code module) is
created (in memory).

If you hit f5 while in forms code (or class module code), you get
the macro prompt - there is no way to map the code you looking at to a copy
running in memory.

The reason for this is that class modules (and forms code modules) can have
multiple instances active at the same time (in other words, you can have the
SAME form opened 5 times, or 5 copies of a class module (object) running in
memory). When you hit f5, what instance of the form code are you going to
run? (remember, when you have the same form opened 5 times, you have 5
SEPARATE sets of variables and code..each able to have their own values
independent from each other).

So, a form+forms code module = class object. (and you can have multiple
instances of that form in memory) When you open a form, you are
creating a instance of the forms class object. (and, you *can* have
multiple instance of this form opened)

And,
a class object is also the same thing, and you have to FIRST create a
instance of the object, and then AND ONLY then can you run the code.

So, in this context, hitting f5 in a class module, or forms code module
don't make sense, because there no way to know what instance of that form's
code you want to run (or in fact that a instance is already created in
memory for it to run).

So, the simply answer is that class modules, and form code modules are
different then standard modules, and you have to FIRST create a running
instance of that object BEFORE you attempt to run code in that class module.
Hitting f5 does not create, nor launch that instance of the object you need
before you attempt to run class module code.

I talk about when, and why you would use class modules in ms-access here:

http://www.members.shaw.ca/AlbertKallal/Articles/WhyClass.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top