473,734 Members | 6,083 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

2 New Member
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 15307
Stewart Ross
2,545 Recognized Expert Moderator Specialist
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.

-Stewart
Jan 25 '11 #2
Ginney
2 New Member
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.UserAnsw er? 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 Recognized Expert Moderator Specialist
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.

-Stewart
Attached Images
File Type: jpg ScreenHunter_02 Jan. 27 19.47.jpg (47.2 KB, 692 views)
File Type: jpg ScreenHunter_03 Jan. 27 19.56.jpg (14.0 KB, 544 views)
Jan 27 '11 #4
ADezii
8,834 Recognized Expert Expert
@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.
http://bytes.com/topic/access/insigh...-vba-ms-access
Jan 28 '11 #5

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

Similar topics

6
3166
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 {
1
23247
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 available outside the class). I think I understand Shared (available without having to instantiate a class). So how could a Private Shared Function be called? Why give it this particular scope?
4
45162
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 writing basic programs and have noticed that the syntax for some of these functions have been set up with the keywords "Public Shared Function" (for example, the Trim function) and others are set up just as "Public
2
2227
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 trying to use a public finction which also needs the property value. How can I use the private property from within the shared public function, without having to pass the property value back into the class again? attempting to use the value now returns this error message:...
4
2296
by: sun1991 | last post by:
#include <iostream> using namespace std; class Base{ public: void ToString(){ ToStringCore(); } private: virtual void ToStringCore(){
9
4633
by: 2005 | last post by:
Hi I have a little trouble with this code. There is a calss with Class xxx{ public: ---- ----
2
1853
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 A { private:
9
1776
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 ..... Return x End Function
7
3766
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 member function as protected or public, I do not want to modify the class definition. I'm wondering what is the standard way to test private member functions in a library. Thanks, Peng
1
1598
pragathik
by: pragathik | last post by:
hi my source code is like this var oPP = new PropertyPanelIFrame(); /*construcotr*/ function PropertyPanelIFrame(){ this._module = "PropPanel"; }
0
9321
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9248
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9193
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8194
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6745
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6043
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4819
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2736
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2185
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.