Setting the default value of a Class in VBA.

There is no keyword in VB6 that allows you to set the default value of a class. Code like this for example will error out.

[VBA]
Public Sub Test()

Dim x As New MyClass

x.Name = “Ross”
x.Name2 = “Dave”

MsgBox x

End Sub
[/VBA]

However it can be done, even if it’s a bit of a pain! Export the class and add the following code in note pad, as the first line under the function heading:

Attribute Name.VB_UserMemId = 0

The value must be 0, and the Attribute must be the name of the function, get, etc. you want to use as the default. The notepad file might look some thing like this!

MyClassPic.PNG

Save the changes in notepad, in the IDE delete and re-import the class. The code will now work! Amazing! If you make any major changes to the class code you will have to redo the process, that’s the PITA bit!

Example Workbook: DefaultTest.xls

Submit a Comment