Edited by Charlotte to activate link
I posted this to the AccessD List, but it is really a VB Add-in I wrote. I’ve found it pretty handy, so I just wanted to share it. You can download it at http://www.wolfwares.com[/url%5D in the VB section.
It’s free, in fact, if you want the source, just give me a holler.
So, now that I am on the third paragraph, maybe you’d like to know what it is. Simple. A Class module is a code ‘object’, right? When you deal with most objects (Class modules excluded), you usually get a ‘Properties’ property, which allows you to do many things. You can set and retrieve property values dynamically (by referring to their number or name), you can find out how many properties you have, you can find out the type of properties they are, etc. Now, when you write you’re own class, you don’t have a ‘Properties’ property built in. Let’s say we write this class:
Option Explicit
Public FirstName As String
Public LastName As String
Dim strInitial As String
Property Let Initial(strEnter)
strInitial = strEnter
End Property
Property Get Initial()
Initial = strInitial
End Property
And let’s save that class as clsName. Then write the following code:
Private Sub Command1_Click()
Dim cn As clsName
Set cn = New clsName
cn.FirstName = “John”
cn.LastName = “Doe”
cn.Initial = “M.”
Debug.Print “First Name: ” & cn.FirstName
Debug.Print “Initial: ” & cn.Initial
Debug.Print “Last Name: ” & cn.LastName
Set cn = Nothing
End Sub
When we run that code, we get:
First Name: John
Initial: M.
Last Name: Doe
That’s pretty simple. However, the more properties a class has, the longer a routine like that would take to write. However, the Class Properties Property Add-in will turn our Class Module’s code into this:
(For the sake of space, I’ve stripped the comments it puts. KEEP the comments in the class modules, so it can update your classes when necessary)(I am attaching the code to a text file.
That’s beefed up our simple class quite a bit. However, it does this automatically. (It also adds another class to your project, but obviously it only needs to do that once). Now that we have a Properties property, we can change our Command1 code to this:
Private Sub Command1_Click()
Dim cn As clsName
Dim i As Long
Set cn = New clsName
cn.FirstName = “John”
cn.LastName = “Doe”
cn.Initial = “M.”
For i = 1 To cn.Properties.Count
Debug.Print cn.Properties(i, intPropertyName) & “: ” & cn.Properties(i)
Next i
Set cn = Nothing
End Sub
And the Immediate Window displays:
FirstName: John
LastName: Doe
Initial: M.
Obviously in this example, we aren’t saving a lot of code behind our button, but I am sure you can see what this can do to help out in some situations. Anyway, enjoy, and let me know if you run into problems with it.
Drew