472,331 Members | 1,740 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,331 software developers and data experts.

How and where to use Private or Public sub or function?

I'm taking an on-line course and They explain the difference between Private and Public. I understand the definition.

Problem is, the only examples they give are Private and are used on Form Modules. Some examples don't even put Private or Public in front of "sub".

Is there a particular module that you use public in? Is it not used on a Form module? Do you just go along writing Private Procedures and suddenly add a Public one?

On to Functions. Correct me if I'm wrong, but basically, if one of the already existing functions (ABS, SQR, etc) doesn't do what you want, you create a Function (calculation) of your own that does (especially if you want to use it often). So, you'll want it to be Public? Should you put all your functions in one module so that you can find them easier? If you did that, what type of Module would it be?
Jan 25 '11 #1
4 14932
Stewart Ross
2,545 Expert Mod 2GB
Form code modules are essentially 'private' in scope. Functions and subroutines defined in them are normally unavailable outside of the form concerned - although it is actually possible to call the functions and subroutines defined as public in scope within a form code module from outside of that module.

Use of the keyword 'Public' is not just a matter of suddenly adding one as a change of style. It reflects that you wish the subroutine, function or variable to be available outside of the scope of the code module concerned (for example, when writing a new custom function that you want to call from an Access query).

'Private' is used for local subroutines and functions that are building blocks for others - performing tasks that you may not wish to be available outside the scope of the code module concerned.

The code modules which can be created or opened from the Modules tab are the usual locations of public subroutines and functions. Named modules provide a means of grouping tasks by logical function, and if used consistently can be an aid to maintainability.

I use many named modules in my applications, separating custom linked table maintenance from username parsing, e-mail handling and so on. I also include a General module which contains general bespoke functions that I find useful and re-use across multiple databases (for example, to return the current financial year or the current quarter within it).

All subroutines and functions defined using the Public keyword in the publically-accessible code modules are available outside of the module concerned, whereas those defined as Private cannot be used outside of the scope of the code module in which they reside.

Variables defined as Public at the top of a public code module are global in scope and retain the last value for the variable concerned between function and subroutine calls.

You are correct that if you need to define a custom function it would be placed in a public code module and be defined using the Public keyword. As mentioned, the use of named modules provides logical grouping of such public subs or functions.

I haven't mentioned Class modules, which provide ways to implement new object classes, as these would need an article in themselves. Class modules and form modules are not directly visible to the developer from within Access itself except when using the VBA Editor.

Jan 25 '11 #2
Let me see if I got this right...

If I have a Form Module, named FrmOne, that has, say, Static UserAnswer as String and I want UserAnswer's value available to see on another form, do I make it Public Sub and refer to it from the other form as frmOne.UserAnswer? Or should it still be a Private Sub? I've done this in Access. You couldn't change the value, but if you didn't have data filled out by the inspector in one form, you couldn't proceed in another.

From your answer: "I use many named modules in my applications, separating custom linked table maintenance from username parsing, e-mail handling and so on. I also include a General module which contains general bespoke functions that I find useful and re-use across multiple databases" Are your "Named" Modules different from a "General" Module?

From your Answer: "Variables defined as Public at the top of a public code module..." Does this mean that a Public Code Module can have Private statements? What exactly makes a Public Code Module different from other non-form Modules?

I'm still confused when the course tells me that Public Module can be Public or Private. Doesn't that go against the name of the Module? - Public?

Can you simply tell me what the basic difference is between Form, Standard, and Public Modules is and when they are used?

This might help in your answering me: I originally did VBA for Access, but from the Event section (On Click, On Open, etc) choosing VBA code instead of macro. I realize now that everything I did in Access was a Form Module. I did once import in a Module that was used strictly as a Function from "Access Cookbook". Now I'm doing VBA for Esri ArcGIS. It's still VBA, but instead of being able to format the Form and it's controls from the Form Design Mode, you go always to the code window.
Jan 27 '11 #3
Stewart Ross
2,545 Expert Mod 2GB
Public code modules, as already mentioned, are those you will see from the modules tab of Access itself. By their nature they are in scope throughout the application - hence why they are called public modules. Form code modules are not in scope throughout Access; they are in general available only to the named form concerned.

Static variables retain their values between calls, but this does not change the scope rules which mean that the static variable defined in a form code module will not be available to other form code modules (nor indeed to other subs or functions in the same module). Each form's code module is in effect hidden from view of the other forms. To do what you mention, which is to share a value from one form to the other, you'd normally define the variable concerned in a public code module as a global variable using the keyword Public to indicate that it was global in scope. There would be no need to make the variable Static in that case.

Private subs and functions are only within scope in the code module in which they are contained - they are not visible to other modules, even if defined in publicly-accessible code modules.

The "Named" Modules I refer to are simply a means of grouping public code modules by function; each one is either a public code module or a class module. The screenshot attached should make this clearer. It shows several public code modules along with one class module, ExcelRepository.

Public code modules can indeed have private subs, functions and variables. These are only in scope (that is, visible or accessible) to other subs and functions within the same code module.

"I'm still confused when the course tells me that Public Module can be Public or Private. Doesn't that go against the name of the Module? - Public?"
The public module itself is visible throughout the application. It is what is contained within it which can be flagged as private, not the module itself.

Form and Report modules are special types of class modules - object implementations, if you like. When implementing objects one of the main principles is that the means by which the objects are implemented is hidden from the user, except where the designer intended the user to interact with the class concerned, by supplying object methods and properties for the user to work with.

There are three tree-like sections withing the VBA editor window, showing the Microsoft Access Class Objects (the Form and Report code modules) which are essentially private in scope, the Modules section which lists defines the public (or Global) code modules which can be seen by all parts of the application, and the Class modules which are also public in scope. These implement user-defined objects. A screenshot is attached.

The main thing I think you need to revise is about the scope rules for variables, subs and functions. You are confusing whether or not a module is publicly accessible with the scope of its contents. Public modules can contain private routines that are NOT available outside of that module. We have an Insights article on this topic which I have linked here for you:

Variable Scope In VBA for MS-Access.

Attached Images
File Type: jpg ScreenHunter_02 Jan. 27 19.47.jpg (47.2 KB, 677 views)
File Type: jpg ScreenHunter_03 Jan. 27 19.56.jpg (14.0 KB, 533 views)
Jan 27 '11 #4
8,832 Expert 8TB
@Ginney - Not to confuse you any more, but by Declaring Variables and Procedures within a Form's Code Module as 'Public', essentially makes them Properties and Methods of that Form. Let's say you create a Public Function in a Form's Code Module (Form1) named fReturnDate():
Expand|Select|Wrap|Line Numbers
  1. Public Function fReturnDate() As Date
  2.   fReturnDate = Date
  3. End Function
Now, as long as Form1 is open, this Function can be treated as a Method of Form1, by executing it in the following manner from any Form:
Expand|Select|Wrap|Line Numbers
  1. MsgBox "The Current Date is: " & Forms!Form1.fReturnDate
A similar scenario applies to Variables Declared as Public within a Form.

P.S. - The following Link may help you, and lift some confusion.
Jan 28 '11 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

by: vijay | last post by:
Hello I wanted to understand a contradictory design of C++ class A {public: virtual void f(){ cout<<" base f"<<endl; } }; class B:public A...
by: Edward | last post by:
I have trouble with some of the concepts of OOP, and am struggling currently with Private Shared Functions. I think I understand Private (not...
by: Chris | last post by:
Hello, I'm just getting started with VB and am new to the group, so please excuse what may seem to be a rudimentary question. I've been...
by: Jon Paal | last post by:
In a 2.0 vb code class, I have a private property and a shared public function The property value has already been passed into the class, now I am...
by: sun1991 | last post by:
#include <iostream> using namespace std; class Base{ public: void ToString(){ ToStringCore(); } private: virtual void ToStringCore(){
by: 2005 | last post by:
Hi I have a little trouble with this code. There is a calss with Class xxx{ public: ---- ----
by: rajasekaran.psg | last post by:
hi there, i am a Rajasekaran, a final yr it student,, i am having a doubt regarding the above subject can you guys help me out,, eg: class...
by: Gilbert | last post by:
Hi, In the code-behind, i have this function: Public Function myfunction(ByVal myvar As Object) As String dim x as string = myvar ........
by: PengYu.UT | last post by:
Hi, I want to write a test function to test a class. The class has a private member function that need to be tested. Although I could modify the...
by: pragathik | last post by:
hi my source code is like this var oPP = new PropertyPanelIFrame(); /*construcotr*/ function PropertyPanelIFrame(){ this._module =...
by: tammygombez | last post by:
Hey everyone! I've been researching gaming laptops lately, and I must say, they can get pretty expensive. However, I've come across some great...
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.