Creation classes in Access
The general definitions
First we shall be defined: what is the object in understanding of object-oriented programming. In the most simple understanding the object is any thing and as well as in world around, the object has properties (" a stone firm ") and operations which are carried out above these properties (a stone can be broken). Almost as business and with objects in the world of programs is. For example, as object it is possible to consider a file, as property — its size or a name, and as operations — reading or record. So, there is an object and there are properties — that is operation? Any user procedure or the function changing or simply working with properties of object. For example, procedure of initialization of fields of record or procedure of a conclusion to the screen of value of fields. There is a natural desire to unite data and ways of their processing in a single whole so that it was clear, what procedures are intended for processing the certain data. Thus, we closely have approached to the central concepts of object-oriented programming — to concepts and a class.
Incapsulation and classes
Object-oriented programming (further OOP) - the methodology of the programming most popular now, being development of structural programming. Central idea OOP is incapsulation, Structurization of the program on modules of the special kind, uniting data and procedures of their processing, and internal data of the module can be processed only by the procedures stipulated for it. In VBA the name the module of a class or simply class is accepted. Each class has the internal part named by realization, and the external part named by the interface.
The class thus, represents new type of the data, allowing to create new variables of this type - objects (sometimes them still name copies of a class). The object consists of elements which can be as actually given, Values of the certain type of data, and the functions realizing operations above elements - data. Elements - data refer to as properties of a class, elements - methods of a class. Such description serves as a pattern for creation in the program of concrete objects of the given class, the concrete names.
Embedding
Besides incapsulation, OOP on VBA it is characterized by one more important property - embedding. Embedding is a mechanism of generation of new classes with use existing. Let the class And then at the announcement of a new class In it свойствами there can be objects of a class is constructed And. In this case speak, that the class And is built in a class In, and the class And is the parent, and a class In - the descendant. Embedding - the transitive attitude, т. е. It is possible to create any way long chain of the enclosed objects forming hierarchy of parents and descendants. So, With it is possible to build in object of a class a new class In which is object of a class of A.Dlya of appendices Office XP the situation is characteristic, when it is necessary to specify 6 - 8 levels of an enclosure to reach the necessary object. For example, to hide the panel of tools Standard in document Word, it is necessary to execute following giving:
Word. Application. ActiveDocument. CommandBars (2) .Visible = False
The mechanism of embedding is very convenient. The primogenitor of family of classes can set some fundamental properties and methods, and numerous descendants, having parental roots (properties and methods), introduce own highly specialized properties and methods.
Inheritance
Along with embedding, there is one more way to use existing classes at creation new, it refers to as inheritance. At inheritance it is underlined, that again created on the basis of a class And the class In contains all (or the some people) methods and properties of a class And, and also own methods and properties. Distinction between embedding and inheritance only syntactic. At the embedding used in VBA for the reference to a method or property of the built in class, we use a name of a field of this class. At inheritance (characteristic for With and C ++) the name of property or a method of a parental class can be used directly.
Creation of a class
Syntax classes in VBA are made out in the form of special modules of classes (the name of a class is a name of the module) where in section Declarations the description of properties of a class is located, and there is a description of methods of a class further. Syntax descriptions of properties and methods practically do not differ from the description of ordinary variables and procedures. Cases of difference or features of the use will be separately stipulated. For creation of the module of a class in Access it is necessary to execute following actions in a window of a database:
Insert - Class Module - " the Name of a class "
It is necessary to choose such name which will be easy to be distinguished for a class, and to reflect the information on what objects are described with the given class of objects.
Procedures of a class
In Access all procedures of a class share on three groups:
- Procedures - methods
- Procedures - properties
- Procedures - replica on events
Procedures - methods
Syntax of the announcement of such procedures does not differ from standard, except for use of keyword Friend. Keyword Friend, as well as keywords Private and Public, is intended for restriction of area of visibility of a method. If Private does a method visible only inside of the module, a Public - for all modules of all projects Friend takes of an intermediate position between them: it does(makes) visible a method only in that project where the class has been described.
Procedures - properties
As is known, definition of a class in VBA consists of two разделов:реализации and the interface. One of the most difficult problems for programmers - beginners in OOP consists in definition of what members of a class to do closed (to include in section of realization), and what, on the contrary, opened (to include in section of the interface). It is possible to consider as the general rule that the is known to less program about realization of a class, the better, It is desirable to hide by means of keyword Private as a lot of properties of a class in section of realization, and access to properties to carry out through special Public - methods, the interface of a class is possible.
Concealment of the information is a concealment of details of realization of functions, a class or even the program. In conditions of concealment of the information the programmer works with functions and classes as with black boxes. In other words, transferring function some value in the form of entrance parameter, the programmer knows only result which will be received on an output of this function. Concealment of the information in реализационную a part of a class and access to it through the functional interface raises reliability of the program. There are two ways of creation of properties:
- More simple of them consists in creation in section of descriptions of the module of a class of the variable declared by means of keyword Public. Thus, users of object will have an opportunity to establish and receive the value kept in this variable, property is accessible both to reading, and for record. The name of a variable is defined by a name of property which is used in your program, therefore, as well as in case of names of classes, it is necessary to apply the names reflecting contents of variables. Though use of the general variables for definition of properties is the most simple way, it has some lacks:
The created class does not have any opportunity to define when external process has changed value of property. It can play the negative role in that case when you need to limit values to the certain interval or to execute any action at change of value.
It is impossible to impose restrictions on values of property or to execute other check of data. For example, you can want to limit value of the property representing age of the person, positive real numbers.
It is impossible to create the properties intended only for reading. In programs often there are situations when it is necessary to return value of property, not appropriating it, especially when such values are based on other data.
- To cope with these lacks, it is necessary to use the second way of creation of property, namely, creation of property by means of procedure Property, their only three: Property Let, Property Set, Property Get. We shall examine them more in detail:
Property Let - procedure of record which allows to appropriate(give) to property new values
[{Public|Private|Friend}] [Static] Property Let Procedure_Name ([Parametrs_List] Value)
[Operator_block_1]
[Exit Property]
[Operator_block_2]
End Property
Syntax is simple and clear. In the beginning there are operators of the announcement of visibility of procedure - properties and unessential keyword Static. By the way, though use of keyword Private is admissible, its presence brings to nothing sense of procedure - properties
Procedure_Name - is a name of the procedure changing property. By the way, it is desirable, that the name repeated in all three procedures-properties if they deal with the same property.
Parametrs_List - is a list of the additional parameters participating in procedure. Syntax of the list of parameters absolutely same, as well as at the list in ordinary procedure. Pay attention that all three procedures-properties having same name, are obliged to have the list of parameters.
Value - is the most important obligatory parameter which value is transferred(transmitted) property.
Further goes Operatr_block in which it is necessary to make giving to value of property of value of actual parameter value.
Property Set - procedure of record which allows to appropriate to property and property - the reference new values. Separate procedure is necessary, as - the reference(link) specifies property object, and giving to objects differs from giving to variables of simple types.
[{Public I Private|Friend}] [Static] Property Set Procedure_Name ([Parametrs_List] Links_Object)
[Operator_block_1]
[Exit Property]
[Operator_block_2]
End Property
Here only one difference from the previous procedure: instead of parameter value, costs parameter Link_Object which represents a name of the reference to the object, appropriated to property - to the reference).
Property Get - procedure of reading which allows to read out value of property. This procedure применима both to simple properties, and to properties-references.
[{Public I Private|Friend}] [Static] Property Get Procedure_Name
( [Parametrs_List] ) As Date_Type
[Operator_block_1]
[Procedure_Name = expression]
[Exit Property]
[Operator_block_2]
End Property
КApparently, Property Get is a function, and for it operate(work) all syntax rules of functions. We shall remind only one: the type of the value returned by function, should coincide with type of value expression.
Creation of a copy of a class
Having defined a class, having set it(him) some properties and methods, it is possible to use it in program VBA. But for this purpose all over again it is necessary to create a new copy of a class. It is impossible to refer simply to variables or to cause procedures of the module of a class thus as it is done in the standard module: VBA will give out a mistake of compilation " Подпроцедура or function is not found " as opportunities to find procedure in global space of names do not exist. Procedure remains "hidden" until you will not create a new copy of a class and then it is possible to cause it only as a method of the created copy of a class. For creation of a new copy of a class it is necessary to declare an objective variable which is used for storage of the reference to the created copy of a class. For the variables applied to the reference to the user classes, the same rules, as for the variables referring objects VBA or the appendices operate. They can be described, using reserved words Dim, Private, Public and Global. Following step is creation of a new copy of object and preservation of the reference to it in a variable. For this purpose operator Set in a combination to keyword New is used.
Creation of a copy of a class. A name of class User, a name of a copy of a class tmpuser.
Dim tmpuser As User
Set tmpuser = New User
Notice, that type of data in this example is the name of a class which has been certain earlier. Though such syntax can seem superfluous, for creation of a new copy of object it is necessary to use keyword New in operator Set. If it to not make, at attempt to apply any property or a method of this class, VBA will give out the message on a mistake of performance 91 - " the objective variable or a variable of the block with is not set ". By means of operator Dim it is not enough For creation of a new copy of object of the simple description of an objective variable.
Implicit creation of a copy of a class
The announcement of a variable and creation of a new copy of a class can be united in one operator. Here is how it will look:
Dim tmpuser As New User
After such announcement the variable can be used at once, addressing to properties and methods of object which it specifies. Thus the object is created not by operator Dim, and later, at the first reference to a variable. Though the described way of implicit creation of objects also is convenient, as allows to save one line of a code, it has one lack: because of that that in the complex appendix you do not know precisely when the object will be created, debugging of such appendix can be complicated. Therefore it is recommended to create always object obviously, by means of separate operator Set New.
The author: Admin
Add: 12.01.2007