Calling Public Sub

  • Thread starter Thread starter google3luo359
  • Start date Start date
G

google3luo359

Hello.

This is very basic but I don't get it at present.

It dawned on me yesterday that I was using the exact same 20 lines or
so of code in several forms. Kind of wasteful. Got me thinking about
Public subs. Not sure how to use them.

I created a Module (NewYear) and in that module created a Public Sub
NewYearRoutines().

Now have I so far been correct? If so I don't know how to call the
Public Sub.

In my Form's Private Sub I tried:

NewYear
Call NewYear
NewYearRoutines()
Call NewYearRoutines()

But none of them have worked.

TIA Ric
 
Call NewYearRoutines() would be the only appropriate one out of the 4
possibilities you've presented. Of course, it depends on whether you defined
any parameters to the routine.

Unfortunately, "none of them have worked" doesn't give much information to
go on. What exactly happens when you try Call NewYearRoutines()? Do you get
an error? If so, what's the error?
 
.... What exactly happens when you try Call NewYearRoutines()? Do you get
an error? If so, what's the error?

Hi Doug,

Sorry about that.
I don't think I defined any params, I pasted my code below.
I always get: 'Sub or function not defined.'


****Code*****

DoCmd.OpenReport "rptUpdateInstruct", acPreview

While SysCmd(acSysCmdGetObjectState, acReport,
"rptUpdateInstruct") = acObjStateOpen
DoEvents 'Do Nothing Wait for Closing
Wend

If MsgBox("Are you sure you are ready to proceed?", vbOKCancel,
"*** Update Student List ***") = vbCancel Then
Cancel = True
GoTo Exit_NewYearRoutines

Else

DoCmd.OpenQuery "DelStudentsTempQry", acNormal, acEdit
DoCmd.TransferText acImportDelim, "StudentsCSVis", "StudentsTemp",
"d:\School\Aep\StudentsCSV.txt", False

DoCmd.OpenQuery "StudentsAppendQry", acNormal, acEdit
DoCmd.Close
 
You sure you created a module named NewYear? No chance you accidentally
created a Class Module, or a Form Module?

What is the code you've posted supposed to be? I don't see any mention of
NewYearRoutines in that code (other than the GoTo Exit_NewYearRoutines)

Do you have [Public] Sub NewYearRoutines() at the beginning of your routine
(the Public is actually optional, as it's the default behaviour). Do you
have End Sub at the end?
 
Douglas said:
You sure you created a module named NewYear? No chance you accidentally
created a Class Module, or a Form Module?

Not sure.
From Objects, I selected Modules. Then New. Up opened a VB box to write
code.
At first I entered Public Sub NewYearRoutines()
but after trying all the four things mentioned before, I started
renaming it to

Function NewYearRoutines()
On Error GoTo Err_NewYearRoutines
DoCmd.OpenReport "rptUpdateInstruct", acPreview etc...

But that didn't help either. Same error message.

What is the code you've posted supposed to be? I don't see any mention of
NewYearRoutines in that code (other than the GoTo Exit_NewYearRoutines)

I cut off the starting code and ending code which is:

Public Sub NewYearRoutines()
On Error GoTo Err_NewYearRoutines
DoCmd.OpenReport "rptUpdateInstruct", acPreview etc...
......
......
Exit_NewYearRoutines:
Exit Sub

Err_NewYearRoutines:
MsgBox Err.Description
Resume Exit_NewYearRoutines

End Sub

Do you have [Public] Sub NewYearRoutines() at the beginning of your routine
(the Public is actually optional, as it's the default behaviour). Do you
have End Sub at the end?

I think I answered your question above?

TIA Ric
 
in the Private Sub in your form, you can call the public Sub (or Function,
it doesn't matter which it is) as

NewYearRoutines

or

Call NewYearRoutines

when the procedure you're calling doesn't have any arguments, then don't
include the opening/closing parens at the end of the procedure name.

and btw, if the procedure you're calling *does* have arguments, then you
only need to include the parens when you use the "Call" keyword, or when the
procedure call is part of an expression (those are instances where you're
calling a Function procedure that returns a value to the expression). and if
that's confusing, don't worry about it right now; just try the syntax shown
above.

hth
 
NewYearRoutines
or

Call NewYearRoutines

Thanks for helping Tina. I already tried that and just did again and
got:

'Sub or function not defined'

My command button to call the Function is below:

Private Sub OpenNewYearInstruct_Click()
On Error GoTo Err_OpenNewYearInstruct_Click

Call NewYearRoutines

Exit_OpenNewYearInstruct_Click:
Exit Sub

Err_OpenNewYearInstruct_Click:
MsgBox Err.Description
Resume Exit_OpenNewYearInstruct_Click

End Sub
 
hmm, well, i see that you've had a dialog going with Doug in this thread. i
read the rest of the thread, and didn't see anything that looked to be an
obvious problem. are you *sure* that the *current* name of the public
procedure is "NewYearRoutines" (without the quotes, of course)?

if your db is Access97 or newer, i'll take a quick look at it, if you want
to send it to me. to do so: just make a copy, delete any proprietary data
from the copy, compact the copy, zip to under 1 MG if necessary, and get my
email address from http://home.att.net/~california.db/tips.html#aTip11.
follow the directions for the Example, and also change the 2 to a 1. refer
to the newsgroups in the email subject line, or it'll get deleted as spam.

hth
 
okay, having looked at your db, i see that the "NewYearRoutines" function in
not in a standard module - it's in a *class* module. i've no experience in
using class modules, but i'm guessing that's the problem; you're trying to
call the function from the class module as though it were a Public function
in a standard module.

i also noticed that your code does not compile - in the class module you
have a reference to "Me.TimerInterval" (sans quotes); again, my lack of
experience here, but i don't think you can use the Me keyword except in form
or report modules.

you also have a compile error in fmPassAdmin, due to referring to a control
that doesn't exist (.txtPassword) in your unbound form. you have the
identical issue in fmPassTeach. it's important to compile your code as you
write it (Debug | Compile on the menu bar in the VBA window), and also good
practice to require explicit variable declaration in all modules (class
modules may be an exception - i don't know...). to set that up for all new
modules in all your databases, from the VBA window click Tools | Options |
Editor tab and checkmark the box next to Require Variable Declaration. in
existing modules, you'll have to add it manually, directly under the Option
Compare Database statement at the top of each module, example:

Option Compare Database
Option Explicit

hopefully Doug or one of the other MVPs, or another VBA-skilled developer,
will step in here and help you with your class module. if you don't get any
additions to this thread in another 24 hours or so, suggest you start a new
thread - and make sure you specify that the code in question is in a *class*
module rather than a standard module.

hth
 
Tina: Unless Ric is actually using the class for something, just move the
code into a proper module.
 
Douglas said:
Tina: Unless Ric is actually using the class for something, just move the
code into a proper module.


HI Doug,

Can you please explain where I go in Access to create a general
module?

I just checked the db I sent to Tina and the module at the top left
says:
(General) and on the right says NewYearRoutines.
The function starts off by:
Function NewYearRoutines()

If I click at the top where it says general, I get a dropdown to choose
'Class'.
Choosing Class gives a new sub at the bottom with:

Private Sub Class_Initialize()

TIA Ric
 
Douglas said:
Tina: Unless Ric is actually using the class for something, just move the
code into a proper module.


I can't believe I actually got this thing to work finally!
Not sure exactly how I did it but I created a general module and it
works now.

On the compile thing. How can I compile code that I've already written
for a form?
When I go back into the form I don't see a menu choice to compile the
form.

TIA Ric
 
I can't believe I actually got this thing to work finally!
Not sure exactly how I did it but I created a general module and it
works now.

On the compile thing. How can I compile code that I've already written
for a form?
When I go back into the form I don't see a menu choice to compile the
form.

It's under the Debug menu in the VB Editor. Note that Compiling is an
all-or-nothing proposition.
 
Douglas said:
It's under the Debug menu in the VB Editor. Note that Compiling is an
all-or-nothing proposition.

Thanks Doug, by all-or-nothing do you mean it must compile the entire
file or nothing? You can't just compile one form's code right?

I saw where I have been sloppy in my coding.
To save time I had gotten into the habit of copying forms that looked
good.
I'd just change the name of the title or a few other things, but never
thought that all of the code from the original form was hanging around
in the copy too!

So I have a lot of checking to do now! It's a bit tricky because you
don't want to delete necessary code.

Ric
 
Thanks Doug, by all-or-nothing do you mean it must compile the entire
file or nothing? You can't just compile one form's code right?

I saw where I have been sloppy in my coding.
To save time I had gotten into the habit of copying forms that looked
good.
I'd just change the name of the title or a few other things, but never
thought that all of the code from the original form was hanging around
in the copy too!

So I have a lot of checking to do now! It's a bit tricky because you
don't want to delete necessary code.

Comment out, rather than delete.
 
Douglas said:
...delete necessary code.

Comment out, rather than delete.


Thanks Doug. Is that because the code might be needed in the future?
It wouldn't add too much to the size of the file?

Ric
 
Thanks Doug. Is that because the code might be needed in the future?
It wouldn't add too much to the size of the file?

Minimal in the overall scheme of things.

Once you know you don't need it (because the code compiles cleanly), you can
then delete it.
 
Back
Top