Auto initialize Class Module (1 Viewer)

kleky

Just gettin' by..
Local time
Today, 05:23
Joined
Apr 11, 2006
Messages
43
Does anyone know how you automatically initialize a newly instantiated class?

So when you create a new instance it will run a procedure to set attributes.

Cheers people :)
 

pr2-eugin

Super Moderator
Local time
Today, 05:23
Joined
Nov 30, 2011
Messages
8,494
I am not sure if I follow you, but when a Instance of a Class is created, the Class_Initialize() is auto run anyway..
 

kleky

Just gettin' by..
Local time
Today, 05:23
Joined
Apr 11, 2006
Messages
43
Cheers, I'm just starting out with classes!

Don't suppose you know how i'd use another class from within it, such as ADODB?

I've added "Implements ADODB" but that throws an error

Ultimately, I'm trying to create a class that deals with security, in that it obtains the logged on user ID and connects to a security database to obtain any held access rights, using an ADODB recordset

many thanks
 

MarkK

bit cruncher
Local time
Yesterday, 21:23
Joined
Mar 17, 2004
Messages
8,185
ADODB is not a class, it is a whole library of classes. A recordset is a class, or a field, and so on.

To run code inside a custom class VBA offers two events, Initialize and Terminate, that you can handle. An example of code that handles the Initialize event is as follows . . .
Code:
Private Sub Class_Initialize()
   MsgBox "This code runs when this class is initialized."
End Sub

If you want your class to expose an instance of a different class it is common to use a Property Get procedure, with code that might look like . . .
Code:
Private m_rst As DAO.Recordset

Property Get Recordset As DAO.Recordset
   if m_rst is nothing then set m_rst = currentdb.openrecordset("SomeTable")
   set Recordset = m_rst
End Property
. . . which "lazy loads" the object, so it is only ever created if a consumer requests it.
 

kleky

Just gettin' by..
Local time
Today, 05:23
Joined
Apr 11, 2006
Messages
43
Thanks Lagbolt,

I put all my code for using a recordset in the initialise event and now it works. I just thought I needed to declare libraries to use them.

Happy days :)
 

Users who are viewing this thread

Top Bottom