Create Property in Microsoft Access by using Class Module

 This article we will discus about how to create owner property in Ms Access. VBA supports Object-Oriented Programming (OOP). Okay sometime we no need to use textbox directly for paste value and insert data to table or for calculate...etc
Now I will show how to do it.After reading this you will :
  • Understand how to create class module 
  • how to use Let and get Property 

Create Class Module 

To being go to Access click on Tab Create and you can see Module, Class Module, Visual Basic

Click Class Module  or in the project pane you can create new Class Module follow with figure 1.0

Class module
Figure 1.0 : How to create Class Module 

Figure 1.1 : Use Project pane to create Class Module 
        With this file created, you then save it using an appropriate class name; for this example, use clsPerson  because you are now working with Class Module. And so now I will declaration variables on top of the class.
           Option Compare Database
           Option Explicit

             Private m_id As String
             Private m_name As String
             Private m_age As Integer
             Private m_phone As String

and next step we will provide user access all of private variables.. how to access it? Let's start with property

Create Let and Get Property

After we create private variables or attribute for your class you need to create a mechanism to read or write the value. let's start :

Click on Tab Insert --> Procedure 


Figure 1.2 Use the Add Procedure dialog box to create a new private or public property.
It will fail to add the property correctly to the class. The code that is created needs appropriate data types to be specified for the return type of the property and the parameter type passed to the property.


you use the Get statement to read an object property from the internal private variable, and the Let statement to assign a value to the internal private variable. An object can have a number of  variables, but you might only need to make a few of these available to the user.
Follow this code for create Property :

Public Property Get StudentId() As String
            StudentId = m_id
End Property

Public Property Let setStudentId(ByVal myid As String)
            m_id = myid
End Property

Public Property Get StudentName() As String
            StudentName = m_name
End Property

Public Property Let setStudentName(ByVal myname As String)
            m_name = myname
End Property
Public Property Get StudentAge() As Integer
            StudentAge = m_age
End Property

Public Property Let setStudentAge(ByVal myage As String)
            m_age = myage
End Property


So now let's start with our form test

Private Sub Command0_Click()
    Dim stu As clsStudent
 
    Set stu = New clsStudent
    ' Create Object stu from Class clsStudent
     stu.setStudentId = "001"
     stu.setStudentName = "Jonh Smith"
     stu.setStudentAge = 52
     MsgBox " ID = " & stu.StudentId & vbCrLf & "Name= " & stu.StudentName & vbCrLf & "Age = " & stu.StudentAge


End Sub







No comments

Powered by Blogger.