Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBA and Class Modules

Status
Not open for further replies.

nickjar2

Programmer
Jun 20, 2001
778
US
Can a class have multiple contructors? I know u can in java, but I cannot seem to get it to work in Access.
Example, if have a Resource Class, I want to initialise it and pass a param through to it. If a guy is currently working days, i want the class to know this so it will allocate the correct number of hours. Or if a guy works on a Friday, I need to set the basic hours to 7. I thought i could have:

Private Sub Class_Initialize(d As Date)
' Set default values for Ringway class
Me.ResourceType = "R"
Me.WeeklyHours = 39

If Weekday(CDate(rsHourBands!WorksDate)) = vbSunday Then
Me.BasicHours = 8
Else
Me.BasicHours = 7
End If

End Sub

then instantiate it by
dim test as new testClass

set test = new testClass(rst(0)),
where rst(0) is a date

Nick (Everton Rool OK!)
 
No, VB/VBA doesn't support function overloading. So you can only have one subroutine or function called "testClass", for any number of arguments.

You CAN, however, use Optional arguments that set a default value if none is provided. So this can achieve the same result using different means.

Code:
Private Sub Class_Initialize(d As Date, Optional BasicHours as Integer = 7)


I didn't look too heavily at your actual problem, but the above is an example of how optional arguments work. All optional arguments must come after all "non-optional" arguments.
 
cheers for responding, I have already tried this and I get:
Compile Error
Procedure declaration does not match decription of event or procedure ahving the same name

Any more ideas?

Cheers

Nick (Everton Rool OK!)
 
Class Initialise is an event and it's arguments cannot be altered.

To the best of my knowledge, VBA class modules do not accept constructors. You simply have to declare a new class and set properties.

Craig
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top