Calling to Procedures

R

RalphInBoise

Hello to all,

I have vba code that works perfectly when I put it in the code handler
for a button. But how do I place that code into a procedure, and then
call that procedure?



For example...



I have a button event handler that I moved the code that worked there to
a class module named, "Class1". Now I want to call that procedure to do
the same thing as its code did when it was in the event handler for the
button. Now I call the method like this, however it does not work. How
do I get the compiler to use the module?





Private Sub Command0_Click()

UpdateThatThing(1, "hotstuff")

End Sub





Then in a module that I named "Class1"...

Public Sub UpdateThatThing(intRecNo As Integer, strStat As String)

'My code goes here, the same code that worked fine

'when used as a button event handler.

End Sub





TIA,

Ralph in Boise
 
G

Gary Miller

Ralph,

You are awful close. If you have a global module ( one that
you get to from the database window module's ) just declare
your sub as Public and then call it by name. What you laid
out looks exactly right. Just be aware that you cannot
reference anything on your form by using the 'Me' keyword
now. If you do need to reference things on the form, pass
the form name to the procedure as well. Ex:

Public Sub UpdateThatThing(frm as Form, intRecNo As Integer,
strStat As String)

Now you would call it...

Call UpdateThatThing(Me, 1, "hotstuff")

If you don't reference form controls, you won't need that.

Gary Miller
Sisters, OR
 
R

RalphInBoise

Gary Miller,

Hello and thanks for your reply. I changed the calling line of code to
start with ... (Call) ...



Call UpdateThatThing(1, "hotstuff")



But I still get a message box saying. "Compile Error: Sub or Function
not defined."



From the Database Window, I can see the object, "Class1", which contains
the Sub...



Public Sub UpdateThatThing(intRecNo As Integer, strStat As String)



But somehow I cannot get the linker to use that module.



I am considering forgetting completely about ACCESS, and going back to
C++ all on its own. At least re-use of code blocks is a simple,
straightforward process there with that language.



TIA,

Ralph in Boise
 
R

RalphInBoise

Gary Miller,

I can send my Access mdb file if that would be helpful. It is a very
small file (196 kb) with only enough substance to test a couple lines of
code. This file includes: one table with three records, one form with
one event handler for that form, and one class module.



TTFN,

Ralph in Boise
 
R

RalphInBoise

Gary Miller,

I can send my Access mdb file if that would be helpful. It is a very
small file (196 kb) with only enough substance to test a couple lines of
code. This file includes: one table with three records, one form with
one event handler for that form, and one class module.



TTFN,

Ralph in Boise
 
M

MacDermott

If you have moved your code to a STANDARD module, then it should be visible
from other modules.

If, however, you have moved your code to a CLASS module (as your naming
suggests), you'll need to instantiate the class and call your sub as a
method of the instance:
Here's one way:
Dim cls as New Class1
cls.UpdateThatThing(1,"hotstuff")

Unless you have some other good reason for using a class module here, I'd
suggest a standard module.

HTH
- Turtle
 
D

Dan Artuso

Well, it's also easy to re-use code in VBA.
It's_just_not_the_same as C++!

When I write C++ code I follow the rules/conventions/syntax of that language,
I don't try to do it the same as I would in VB.

Move your code into a standard module!
 
B

Ben

Ralph -
If you want to call code in a class you need to instantiate an object of
that class and then call the method. For example:
Dim x as class1

set x = new class1
x.UpdateTheThing(arg1,arg2)
set x = nothing

You need to decide if the code belongs in a class module or a standard
module, with a standard module the code you posted would work, though it
is best to disambiguate it with moduleName.procedureName.

HTH -
Ben
 
G

Gary Miller

Ralph,

I would be glad to look at it if you want, but I think that
the suggestions on moving your code to a new 'standard'
global code module should do the trick. Classes are treated
a bit differently in Access and putting your subs and
functions in a normal module is the routine practice.

I have a lot of 'Utility' type modules that I just copy from
db to db.

Gary Miller
 

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