zadams
asked on
Creating class in MS Access/VBA-Examples?
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.
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.
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
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
ASKER
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.
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.
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
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.
ASKER
Understood.
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.