Solved

Function, Private Function, and Public Function

Posted on 2011-09-07
12
336 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
  • 4
  • 3
  • 2
  • +2
12 Comments
 
LVL 119

Accepted Solution

by:
Rey Obrero 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
 
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 119

Expert Comment

by:Rey Obrero
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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 

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 119

Expert Comment

by:Rey Obrero
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 - Access MVP)
DatabaseMX (Joe Anderson - Access MVP) 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 119

Expert Comment

by:Rey Obrero
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
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 …

911 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now