Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Creating class in MS Access/VBA-Examples?

Posted on 2006-11-22
7
Medium Priority
?
3,515 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
ID: 18001617
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
ID: 18001965
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
ID: 18002061
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 65

Expert Comment

by:rockiroads
ID: 18002140
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 200 total points
ID: 18002153
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 85
ID: 18002197
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
ID: 18002211
Understood.
0

Featured Post

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
Windows Explorer let you handle zip folders nearly as any other folder: Copy, move, change, and delete, etc. In VBA you can also handle normal files and folders, but zip folders takes a little more - and that you'll find here.
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…

877 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