Solved

Function, Private Function, and Public Function

Posted on 2011-09-07
12
340 Views
Last Modified: 2012-05-12
Can anyone give me a "layman's" explanation of the differences between:
  Function -
  Private Function -
  Public Function -

Why would you use one over the other ...  I see this in example code and don't understand the differences.
0
Comment
Question by:callstate
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +2
12 Comments
 
LVL 120

Accepted Solution

by:
Rey Obrero (Capricorn1) earned 250 total points
ID: 36499144
Private Function  ' can be called FROM the module it is located

Public Function  ' can be called anywhere in your application, from form, query

also take not that if you place a function in a regular module without Public of Private, it is a public function
0
 
LVL 61

Expert Comment

by:mbizup
ID: 36499155
Private Function - visible only within the module it resides in
Public Function - Visible throughout your application

Function - Depends... if it is in a standard module (in the modules collection), it is visible throughout your application.  If it is in a form's module it is only visible within the context of that form.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 36499176
Function
Private Function


Are identical in meaning. If you leave off "Private" a function is private by default. A private function is one that is only accessible to the class in which the function is defined.

Public Function
A public function is one that is accessible to any caller, including the class defining the function itself.

In order to call a public function, you must have a reference to an instantiated object, that is, an object for which you said something like:

Set obj = New SomeArbitraryClassName()

Open in new window


Note the use of the "New" keyword.

If your functions occur inside Modules, then you typically won't (if ever) have an instantiated object to call the function against. It would basically be a global function. The same is true for a private function, except that you would only be able to call the private function from within that same module.
0
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 36499179
It seems I may be mistaken by the "Function" version. Too much .NET for me, I guess   : \
0
 

Author Comment

by:callstate
ID: 36499299
OK ... so PRIVATE means only visible to the code in the same form, module, etc. and PUBLIC means visible across the entire project?  (I am using AC07 VBA)

and ... FUNCTION by itself defaults as Public or Private?

and ... Why would you WANT a Private vs a Public function, why not make them all Public?
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 36499376
callstate,

 did you read my post?

 *also take note that if you place a function in a regular module without Public of Private, it is a public function
0
 

Author Comment

by:callstate
ID: 36499392
capricorn1 says "take note that if you place a function in a regular module without Public of Private, it is a public function"

kaufmed says "If you leave off "Private" a function is private by default.
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 36499430
callstate, try it so you will know which is right
0
 
LVL 75
ID: 36499444
Right from the Help file:

If not explicitly specified using Public, Private, or Friend, Function procedures are public by default.
0
 
LVL 75

Assisted Solution

by:DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform)
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform) earned 250 total points
ID: 36499465
"Why would you WANT a Private vs a Public function, why not make them all Public?"

Only if ...for some reason, you did not want a certain Function 'visible' from outside the scope of where it exists.  And this is likely only going to matter if more than one person (developer) is working on the project.  And also, in a regular Class Module - that you specifically create (as opposed to Form/Report modules, which are also Class modules) ... Private vs Public becomes more important, because you likely only want certain Functions (aka 'Methods') to be visible external to the Class.

mx
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 36499468
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 36499509
kaufmed says "If you leave off "Private" a function is private by default.
I did, but I recanted later: http:#36499179
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question