Solved

Creating class in MS Access/VBA-Examples?

Posted on 2006-11-22
7
3,448 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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
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 50 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 84
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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

In the previous article, Using a Critera Form to Filter Records (http://www.experts-exchange.com/A_6069.html), the form was basically a data container storing user input, which queries and other database objects could read. The form had to remain op…
It took me quite some time to sort out all the different properties of combo and list boxes available from Visual Basic at run-time. Not that the documentation is lacking: the help pages are quite thorough and well written. The problem was rather wh…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …

929 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

10 Experts available now in Live!

Get 1:1 Help Now