'Public' and 'Private' Prefix, when do I need them?

muppetgeoff

New member
Local time
Today, 01:51
Joined
Aug 9, 2004
Messages
9
Hi Everyone,

I hope this isn't a really stupid question :)

Basically, I've searched and searched, but can't seem to find an answer to the following:

If I create a Sub or Function that is bound to a Form, so It's only accessible by that form (private). So what is the difference between typing just 'Sub' or 'Function', and typing 'Private Sub' or 'Private Function' ?

The same question the other way round, when I create a seperate VBA Module for my Public procedures, what is the dufference between typing just 'Sub' or 'Function' and typing 'Public Sub' or 'Public Function' ?

Is the use of the 'Public' and 'Private' used to visually differentiate between Public and Private procedures, and actually makes no difference?

Then a final, but possibly pointless question is; If I create a 'Private' procedure in a Module, can it then be referenced by any Public procedure contained in that Module?

Hope some one can help :)

Many thanks in advance,

Geoff
 
Last edited:
In a forms module, if you declare a sub or function with the public keyword, then it is accessible from all other code, as long as the form is open. Not as "real" publics, but as a form property:

myvar=forms("myform").myformpublicfunction(someparameter)
call forms("myform").myformpublicsub

I use this from time to time, when there's a need for certain routines for instance only in a main/subform setup, so there's no need to create a "real" public sub or function.

In a standard module, sub equals public sub, function equals public function. If you need it private, use the private keyword. I prefer to always declare with the keyword (public, private...) too so that it is visually/documented/explicitly stated what scope the function/sub has. I like to think that it is more than "only visually distinguish", pehaps in terms of future versions perhaps requiring explicit declarations, changing the behaviour etc, so I see it also as driving with seatbelts on, a safety measure.

Declaring a sub or function within a module using the Private keyword, makes it "private" to the module, so that it can be called from any subs or functions within the module, but is not accessible from "outside" it (private scope within the module).
 
Hi Roy,

Brilliant, thank you for clarifying that :)

Pretty much what I had assumed, but interesting to know about the 'Public' form Procedure, nice one :)

Kind regards,

Geoff
 

Users who are viewing this thread

Back
Top Bottom