|
||||||
|
Introduction Welcome! Classes, Types & Encapsulation Welcome to Microsoft Access Developer Level 50. In this course we will begin working with class modules in Access VBA, starting with a review of user-defined types and discussing their limitations. We will walk through creating our first class module, adding properties and public variables, and learn about encapsulation to protect data within classes. We will explore calculated and read-only properties, add methods and behaviors, and use real-world examples, including working with customer data and recordsets. You will see direct comparisons between user-defined types and class modules as we progress through each lesson. NavigationKeywordsAccess Developer, class modules, VBA programming, encapsulation, user-defined types, calculated properties, read-only properties, class methods, behaviors, recordsets, customer table example, properties vs variables, instantiating classes
IntroWelcome to Microsoft Access Developer Level 50. In this course we will begin working with class modules in Access VBA, starting with a review of user-defined types and discussing their limitations. We will walk through creating our first class module, adding properties and public variables, and learn about encapsulation to protect data within classes. We will explore calculated and read-only properties, add methods and behaviors, and use real-world examples, including working with customer data and recordsets. You will see direct comparisons between user-defined types and class modules as we progress through each lesson.TranscriptWelcome to Microsoft Access Developer Level 50 brought to you by accesslearningzone.com. I am your instructor, Richard Rost. Today, we are going to begin our journey into class modules.We have done lots and lots of VBA programming with regular modules. I have always said, this is the guy here, class modules, we are not going to be using. Well, today, we are going to start using that to build classes. We are going to review user-defined types, which you covered before in developer 40. We are going to build a class module. We are going to learn about encapsulation. We are going to create calculated and read-only properties. We are going to add methods and behaviors to a class. Then we are going to finish it all up with a real-world example using data from our customer table. This class follows Access Developer 49. While I strongly recommend you take all of my classes in order, it is not necessary, but you should have a good solid understanding of Access. The beginner series is a must. The expert series, especially the stuff on relationships. Developer 16 will be important because we are going to use recordsets today. So I do recommend you take them all. I strongly recommend you do not skip levels. Go see this page for more information on why. It is currently 2025. I am using an Access 365 subscription, which is roughly equivalent to Access 2024. The material covered in today's class should work all the way back to, I am guessing, Access 2007 or so. If you have any questions regarding the material covered in today's class, just scroll down to the bottom of the page that you are on and post your questions there. Also, take a minute to read through any other questions that have been posted as your question may have already been answered. If you want to get notified any time someone posts a question or other information about this course, click on that red subscribe button. If you have any other questions about Access in general, post them in the Access forum. I have a great group of moderators and we all love helping people out with their questions. All right, let's take a closer look at exactly what is covered in today's class. In lesson one, before we get into class modules, we are going to review user-defined types, which we did cover initially in Developer 40. We are going to go into a little more detail and we are going to talk exactly about why user-defined types are limited as compared to what class modules can do. In lesson two, we are going to create our first class module. We are going to create a new class module, give it a name, add public variables to it, instantiate the class, and assign values. Then we will talk about how it compares to user-defined types. In lesson three, we are going to learn about encapsulation - that is taking our variables and making them so that they are limited to just the class. You have to use the class to read and write the data. Not every form, not every report can read the data directly from the class. Only you can. That is called encapsulation. We will talk about that in this lesson. In lesson four, we are continuing on with our class stuff. We are going to make calculated and read-only properties. We will make properties, which are basically like functions that you can have look at the data in the class and return certain information. For example, I could say, "Get me how many years this customer has been active." It knows "customer since," it can figure that out. Stuff like that. That is all covered in lesson four. In lesson five, we are going to add methods and behavior to our class. It is no longer just about storing data and retrieving data. Now we can actually do stuff, for example, deactivating a customer. We can flip them from active to deactivated, but the logic is handled inside the class itself and we can add rules to it. For example, you cannot deactivate a customer that is less than a year old. That kind of stuff. That is all covered in lesson five. In lesson six, we are going to wrap it all up today with a real-world example. We are going to use a recordset, loop through all the customers in our table, set each customer into a class using the class logic, and then display it with the class functions so we get the full name and their customer status straight out of the class functions. Then it is going to be pretty cool. So check it out. That is all in lesson six. QuizQ1. What new concept is introduced in this video for Access development?A. Class modules B. Macros C. SQL procedures D. Web forms Q2. What topic is reviewed before starting class modules? A. User-defined types B. Creating relationships C. SQL joins D. Reports design Q3. Compared to user-defined types, what advantage do class modules provide? A. They support encapsulation and behaviors B. They allow external database connections C. They have built-in SQL support D. They automatically generate forms Q4. What is encapsulation as described in the video? A. Restricting variable access to within the class only B. Encrypting all Access data C. Limiting table access by user permissions D. Backing up a database Q5. What are properties in a class module, according to the video? A. Functions that return information based on class data B. Tables holding customer records C. External files attached to the database D. Relationships between tables Q6. What is an example of a read-only property that could be implemented in a class? A. Years a customer has been active B. Current system date C. Name of the Access application D. Maximum RAM in use Q7. What do methods and behavior allow a class to do? A. Perform actions such as deactivate a customer with rules B. Auto-generate table names C. Create linked tables D. Publish the database online Q8. What rule is given as an example for a method in the customer class? A. Do not allow deactivating a customer less than a year old B. Prevent deleting inactive customers C. Only allow name changes once per month D. Automatically update phone numbers daily Q9. In the final lesson, what real-world example is used for class modules? A. Managing customer data using a class with recordsets B. Creating dynamic forms for order entry C. Building queries to summarize sales data D. Generating reports based on product inventory Q10. Which earlier Developer Level should you review if you need to understand recordsets? A. Developer 16 B. Developer 10 C. Developer 25 D. Developer 44 Answers: 1-A; 2-A; 3-A; 4-A; 5-A; 6-A; 7-A; 8-A; 9-A; 10-A DISCLAIMER: Quiz questions are AI generated. If you find any that are wrong, don't make sense, or aren't related to the video topic at hand, then please post a comment and let me know. Thanks. SummaryToday's video from Access Learning Zone is the beginning of Microsoft Access Developer Level 50, where we start exploring class modules. Up until now, we have focused a lot on VBA programming using standard modules. I have often pointed out that class modules were something we were not yet using, but now we will finally start building our own classes.We will begin by revisiting user-defined types, which you should recall from Developer Level 40. I will cover their limitations and set the stage for using class modules, which are far more powerful. After that, I will show you how to create your first class module, assign it a name, add public variables, instantiate an object from the class, and set values. We will talk about the differences between classes and user-defined types so you understand why you would choose one over the other. Next, I will introduce the concept of encapsulation. This means that the variables of your class are locked inside it, so you need to use specific properties and methods to access or change the data - no other part of your database can alter them directly. Encapsulation helps keep your data secure and ensures that your business rules are always followed. After that, I will show you how to add calculated and read-only properties to your class. Think of properties as special functions that let you look at or manipulate data in your class. For example, you might have a property that tells you how many years a customer has been active, based on their "customer since" date. Then, we will look at adding methods and behaviors to your class. Beyond storing and retrieving data, your class can now perform actions, such as deactivating a customer. You can set up rules, like refusing to deactivate a customer who has not been with you for at least a year. All this logic sits right inside the class, keeping your business processes consistent. To wrap everything up, I will give you a practical example. We will use a recordset to loop through all customers in your table, create a class for each customer using our new logic, and then display their information with class-based functions. You will see how you can pull out full names, customer status, and more, all through the class interface. This course picks up from Developer Level 49. While it is not strictly required to follow all levels in order, I strongly recommend having a good background in Access, especially the beginner and expert series and anything covering relationships. Developer Level 16 is also important, since we will be using recordsets. Skipping levels could leave you missing key concepts, so do try to follow them in sequence. As of 2025, I am using Access 365, which lines up closely with Access 2024, but the techniques I am showing you should work in versions going back as far as Access 2007. If you have any questions about the class content, you can post them at the bottom of the course page. I also suggest reading through other questions that have already been posted because you might find your answer there. If you want notifications about updates or comments, you can subscribe. For broader Access questions, there is an Access forum staffed with helpful moderators. To sum up, here is what you will see in each lesson of this class: Lesson one revisits user-defined types from Developer 40, looking deeper into their limitations as compared to class modules. Lesson two covers the creation of your first class module. You will learn how to name it, add variables, instantiate it, assign values, and compare classes to user-defined types. In lesson three, we explore encapsulation, which ensures that class data is only accessible through defined interfaces, adding a layer of security and control. Lesson four walks through building calculated and read-only properties, allowing you to add more intelligence to your classes, such as computing how long a customer has been active. Lesson five moves on to adding methods and behaviors, letting your classes perform actions and enforcing rules like preventing deactivation of new customers. Finally, lesson six puts everything into practice with a real-world example using your customer table, recordsets, and class logic to process and display customer information in a practical way. You can find a complete video tutorial with step-by-step instructions on everything discussed here on my website at the link below. Live long and prosper, my friends. Topic ListReviewing user-defined types (UDTs)Limitations of user-defined types Creating a class module Naming and adding public variables to class modules Instantiating and assigning values to a class Comparing class modules to UDTs Understanding encapsulation in classes Making calculated properties in classes Creating read-only properties in classes Adding methods and behaviors to a class Implementing business rules in class methods Using class modules with Access recordsets Looping through records with class modules Displaying data with class module functions Real-world example using customer table data ArticleWelcome to this deep dive into class modules in Microsoft Access VBA. If you are already comfortable with regular modules and VBA procedures, this article will help you take the next step and start building your own classes using class modules. If you have used user-defined types before, you will find class modules offer many more possibilities, including encapsulation, calculated properties, and custom behaviors.To begin, let us quickly review user-defined types, or UDTs. You might recall that a UDT is a handy way to group related variables in VBA. For example, you could define a CustomerType as follows: Type CustomerType CustomerID As Integer FirstName As String LastName As String Active As Boolean End Type You can then declare a variable based on your new type: Dim Cust As CustomerType Cust.CustomerID = 5 Cust.FirstName = "Jane" Cust.LastName = "Smith" Cust.Active = True While simple and effective, UDTs are limited. They only let you group data fields together, but you cannot easily add behaviors, calculations, or special access rules. For example, you cannot create a property that automatically calculates a customer's full name from first and last names, or enforce business logic like preventing a customer from being deactivated under certain conditions. That is where class modules come in. A class in VBA is much like a blueprint for an object, allowing you to define not only properties (data) but also methods (behaviors) and custom logic. To create a class, you add a new class module in the VBA editor and give it a name, for example, clsCustomer. Let us walk through creating a simple customer class. Open the VBA editor (press Alt+F11 in Access), then insert a new class module. Rename it to clsCustomer. You can now define public variables that represent customer fields. ' In clsCustomer class module Public CustomerID As Integer Public FirstName As String Public LastName As String Public Active As Boolean Using your new class is a bit different from UDTs. You must declare a variable as the class type, then use the New keyword to create an instance. Dim Cust As clsCustomer Set Cust = New clsCustomer Cust.CustomerID = 5 Cust.FirstName = "Jane" Cust.LastName = "Smith" Cust.Active = True Now, you have an object that represents a single customer. You can set its properties and later retrieve them as needed. One of the main benefits of a class module is encapsulation. Encapsulation means you can hide the internal data of your object and force users to interact only through specific methods and properties. To do this, you typically make your data variables Private instead of Public, then expose them through Property procedures. Here is how you might encapsulate your class: ' In clsCustomer class module Private m_FirstName As String Private m_LastName As String Public Property Get FirstName() As String FirstName = m_FirstName End Property Public Property Let FirstName(Value As String) m_FirstName = Value End Property Public Property Get LastName() As String LastName = m_LastName End Property Public Property Let LastName(Value As String) m_LastName = Value End Property Now, other code can only read and write FirstName and LastName using these property procedures. You can also add logic for validation or special behavior in these procedures. You can create calculated and read-only properties in your class as well. For example, suppose you want to retrieve the full name. You can define a property like this: Public Property Get FullName() As String FullName = FirstName & " " & LastName End Property Notice there is no Property Let for FullName, making it read-only. Any code using clsCustomer can read FullName, but cannot directly set it. Beyond properties, you can add methods to define actions your class can perform. Suppose you want to add a method to deactivate a customer, but only if they have been active for more than one year. First, you might add a CustomerSince date property: Private m_CustomerSince As Date Public Property Get CustomerSince() As Date CustomerSince = m_CustomerSince End Property Public Property Let CustomerSince(Value As Date) m_CustomerSince = Value End Property Now, add a method to handle deactivation: Public Sub Deactivate() If DateDiff("yyyy", CustomerSince, Date) >= 1 Then Active = False Else MsgBox "Customer cannot be deactivated within the first year." End If End Sub This way, any attempts to deactivate a customer are checked with your business rule. As a real-world example, imagine you have a Customers table in your database. You can use a recordset to loop through all customers, create a new clsCustomer object for each one, set its properties, and call its methods or retrieve its calculated properties. For example: Dim db As DAO.Database Dim rs As DAO.Recordset Set db = CurrentDb Set rs = db.OpenRecordset("Customers") Do While Not rs.EOF Dim Cust As clsCustomer Set Cust = New clsCustomer Cust.CustomerID = rs!CustomerID Cust.FirstName = rs!FirstName Cust.LastName = rs!LastName Cust.Active = rs!Active Cust.CustomerSince = rs!CustomerSince Debug.Print Cust.FullName, Cust.Active rs.MoveNext Loop Set rs = Nothing Set db = Nothing With class modules, you gain not just a way to store data, but also to enforce your business logic, encapsulate data, create calculated fields, and define robust methods your application can use. They are a powerful tool for anyone looking to take their Access VBA skills to the next level. If you have questions as you work through these concepts, you can post them in a relevant forum or take the opportunity to review related documentation to solidify your understanding. Using class modules will make your code more organized, scalable, and easier to maintain, especially as your applications become more complex. |
||
|
| |||
| Keywords: Access Developer, class modules, VBA programming, encapsulation, user-defined types, calculated properties, read-only properties, class methods, behaviors, recordsets, customer table example, properties vs variables, instantiating classes PermaLink How To Create and Use Class Modules, User-Defined Types, and Encapsulation in Microsoft Access |