Using Classes In VBA - Starting Point

J

Johnny Liner

Hello,



I have created a class module in Access 2003. I have created a function
within that class module as follows.



Sub Main()

.Run some code here to set variables.

End Sub





===========



Now from a Regular module I have the following code.



Function test() as String

Dim myClass as New customClass



Test = myClass.myVariable



End Function



When I run this, I get nothing. however, if I do the following..



Function test() as String

Dim myClass as New customClass

myClass.Main

test = myClass.myVariable

End Function



When I run this new version of the code, I get the value I am seeking.
I was under the assumption that when a class is instantiated (aka Dim
myClass as NEW <someclass>) then the Sub Main() function is
automatically run. Isnt there a way in Access 2003 that when a class is
instantiated, it will auto run code within a module with a specific
name? I thought that name would be something like (Sub New) but Sub New
doesn't work in Access 2003 - so, I tried Sub Main().



Thanks in advance for the insight,

Johnny
 
A

Andy Pope

Hi,

Don't believe the sub Main will be run automatically.
But you can call it from the Initialize event in the class, which is
trigger when the New keyword is used.

Private Sub Class_Initialize()

Main

End Sub

Cheers
Andy
 
Top