Public Sub in Module

  • Thread starter יריב החביב
  • Start date
×

יריב החביב

Hello,

I wrote Public Sub (x) in Module (y).

It did not work when i tried to run it from a form (docmd.openmodule, "x",
"y")

can you help me, way it didnt work ?

when i tried to use it trough macro it didnt recognize the Public Sub .

just when i replace (in the module) the sub with function it (the macro)
recognize it.

way ? (it is not function it sub)

thank you
 
S

Stefan Hoffmann

hi,

יריב החביב said:
I wrote Public Sub (x) in Module (y).

It did not work when i tried to run it from a form (docmd.openmodule, "x",
"y")
can you help me, way it didnt work ?
Yiu simple call this Sub like this:

x

or

y.x

or

Call x()


mfG
--> stefan <--
 
R

Rob Parker

Using the code you wrote will simply open the subroutine code for editing in
the VBA editor; it will not run the code.

To run a Public subroutine from a form, all you need is the name of the
subroutine in a single line of code; using your simplified example, the line
would simply be:
y

Or, to make it clearer what is happening, you can use:
Call y

I can't comment on how to do this from a macro, as I never use them - only
VBA code.


HTH,

Rob
 
S

Stefan Hoffmann

hi Ken,
To execute a procedure or function in code you simply enter the name of the
function:

x

The name of the module is irrelevant.
This is only correct, if there is only one sub procedure with this name.

mfG
--> stefan <--
 
T

techrat

Stefan:

That is of course true, but it would be unwise to have two procedures or
functions declared Public and with the same name.  
I'm obviously thinking only of standard modules here.  Class modules are a
different kettle of fish.

Ken Sheridan

+1 on not having 2 procedures with the same name declared public in
standard modules. This is just asking for trouble IMO.
 
S

S.Clark

1. If you only want to open the module to the specified procdure, then use
the Open Module like you did.

2. If you want to execute the procedure, then you would simply put the sub
name in code.

3. If you want to execute a procedure in a module from a macro, then you can
use the RunCode action, but it must call a function, not a sub.
 
D

David W. Fenton

If MsgBox("Answer yes or no.", vbYesNo, "Select answer") = vbYes
Then

This is not in anyway a criticism of your fine answer, just passing
on a hint that I picked up from somebody in the newsgroups here. I
would write the line above as:

If vbYes = MsgBox("Answer yes or no.", vbYesNo, "Select answer")
Then

This puts the match value first, and all the blah, blah, blah of the
message box at the end, and for me, makes it easier to follow the
logic as I don't have to scroll out to the end (or us line
continuation characters).
 
×

יריב החביב

Thank You S.Clark,

But HOW TO ...."put the sub name in code" ?

can you write the code ?

thank's
 
B

Bernd Gilles

יריב החביב said:
Thank You S.Clark,

But HOW TO ...."put the sub name in code" ?

can you write the code ?

lets say the name of your sub is "DoSomething" and the name of the
module is "MyLittleModule".

Dummy-Code:

Public Sub DoSomething(Value As Integer)
MsgBox "Hello World " & Value
End Sub

In any form, you can call your sub by simply typing:

DoSomething 1

or to make it clearer to read:

Call DoSomething(1)

if a sub named "DoSomething" does exist in more than one module, you can
specify it by:

MyLittleModule.DoSomething 1

or

Call MyLittleModule.DoSomething(1)



clear to this point?
 
D

David W. Fenton

I must admit to being a heavy user of the continuation character.

I avoid it like the plague because of the corruption problems it has
caused in the past. It may not cause them any longer, but the habit
is ingrained at this point.
 

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