Solved

Creating class in MS Access/VBA-Examples?

Posted on 2006-11-22
7
3,443 Views
Last Modified: 2011-08-18
Hi everybody!
I have not a big expiriance in access programming and interesting about Object oriented programming with VBA. Has any examples about classes in Access/VBA which I can using in my Access program?
Thanks in advanced.
0
Comment
Question by:zadams
7 Comments
 
LVL 15

Expert Comment

by:Simon Ball
Comment Utility
intersting.

the closest i get to proper OOp is using a module with public functions in which act as methods...

I'd be interested to see what other experts c0mment on this as i am currently learning JAVA OOp as part of my MSc.  I know you can use VB6 or .NET to write classes so I assume the sameis possible in VBA in access/excel, but I am not sure where that would be useful in Access.

You can declare an object, and set it equal to something to instansiate an object in accesss, so you can probably write you own objects too.
0
 
LVL 65

Expert Comment

by:rockiroads
Comment Utility
How familiar are u with OO ?

Each class has a constructor/destructor, Access has its equivalent
each class u create has the following functions

Private Sub Class_Initialize()

End Sub

Private Sub Class_Terminate()

End Sub


when u instanstiate a class
eg
dim x as new clsMylass

the initialize method is called


Your class may have members, in which case u need to create the accessor functions i..e your get/set functions
in access its Public Property Let and Get

eg

set a member

Public Property Let MyField(sVal as String)
    MyField = sVal
End Property


then u can define public functions and procedures  which become methods of your class


thats it
0
 

Author Comment

by:zadams
Comment Utility
rockiroads,
could You send any example. For example, I have Subscribers Database in a little telecom company. What class may be usefull in my case?
Thanks.
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 65

Expert Comment

by:rockiroads
Comment Utility
simple example

here is a class. saved as clsTest



Dim sName As String

Private Sub Class_Initialize()
    sName = "not set"
End Sub

Public Property Let Name(ByVal sNewName As String)
    sName = sNewName
End Property

Public Property Get Name() As String
    Name = sName
End Property





This is how u use it

    Dim x As New clsTest
   
    Debug.Print "Initial Value", x.Name
   
    Debug.Print "Set it to fred"
    x.Name = "fred"
   
    Debug.Print "Value is now", x.Name

0
 
LVL 65

Accepted Solution

by:
rockiroads earned 50 total points
Comment Utility
I dont know your DB, classes are used to identify an entity, then all methods/properties of that entity are defined in that class.

Personally Access has implemented classes but there is no real OO approach here. What u can do in classes, u can do in modules as well, simply by defining them either as public or private
0
 
LVL 84
Comment Utility
IMO, a great book one this subject is Visual Basic 6 Business Objects by Rockford Lothka. He goes into great detail about classes and how to build them, and while the book is specific to VB6 virtually all concepts are viable in Access as well.

Rocki is correct in that Access doesn't hold with the "true" definition of OO, but it's close enough to be useful on many levels. I don't necessarily agree with using modules in place of classes ... you cannot, for example, instantiate an instance of a module, but you can with a class, so if you have a Customer class, you could have a dozen or more different instances of various Customers ... you couldn't do that with a Module.
0
 

Author Comment

by:zadams
Comment Utility
Understood.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Most if not all databases provide tools to filter data; even simple mail-merge programs might offer basic filtering capabilities. This is so important that, although Access has many built-in features to help the user in this task, developers often n…
Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now