How to Create Class Modules - learnit

Home Top Ad

Post Top Ad

Sunday, May 30, 2021

How to Create Class Modules

How to Create Class Modules

What is VBA-Access?

VBA stands for Visual Basic for Applications (the application being of course Microsoft Access) and is the technology and tools used to program and automate this application.


It’s not only used just within the framework of Microsoft Access Database but other applications too including Microsoft Excel, Microsoft Word, Microsoft Outlook to name a few.It has the power to communicate with other applications beyond the Microsoft range and even the Microsoft Windows operating system across other platforms.So, learning the principles of VBA using Access as the tool environment will stand you in good stead for the other applications should you wish to program and code them in the future.The only difference between other applications when wanting to use VBA will simply be learning to load and work with different libraries.Typical reasons why you would use Microsoft Access VBA include being able to


  1.Manage smaller re-usable procedures (globally).

  2.Create you own defined functions.

  3.Hold variables in memory (globally).

  4.Attach code to an event i.e. a button on a form.

  5.Manage more powerful procedures that an Access macro lacks.

  6.Communicate with other applications and platforms.

  7.Gracefully handle errors (when they happen).


How to Create Class Modules

Understanding Class Modules

Whenever you create event procedures behind a form or report, you are creating a class module. A class module is the specification for a user-defined object in your database, and the code you write in the module defines the methods and properties of the object. Of course, forms and reports already have dozens of methods and properties already defined by Access, but you can create extended properties and methods when you write code in the class module attached to a form or report.


You can also create a class module as an independent object by clicking the Class Module button in the Macros & Code group on the Create tab or by clicking Class Module on the Insert menu in the VBE


As previously discussed, you define a method in a class module by declaring a procedure (either a function or a sub) public. When you create an active instance of the object defined by the class module, either by opening it or by setting it to an object variable, you can execute the public functions or subs you have defined by referencing the function or sub name as a method of the object.


Property Get

Use a Property Get procedure to return a property value for the object defined by your class module. When other code in your application attempts to fetch the value of this property of your object, Visual Basic executes your Property Get procedure to return the value. Your code can return a data value or an object.


Syntax

[Public | Private | Friend] [Static] Property Get propertyname
([< arguments >]) [As datatype]
[< property statements >]
[propertyname = < expression > ]
[Exit Property]
[< property statements > ]
[propertyname = < expressio n> ]
End Property

Example
Option Compare Database
Option Explicit
Private id As Integer
Private name As String
Private Salary As Integer
Public Property Get sid() As Integer
id = 1
sid = id
End Property
Public Property Get SSalary() As Integer
Salary = 400
SSalary = Salary
End Property
Public Property Let SName(value As String)
name = value
End Property
Public Property Get SName() As String
SName = name
End Property

Please Watching My Video is Below

No comments:

Post a Comment

Post Top Ad