How to instantiate a class when you know its name (Microsoft Office XP)

P

Peter Ignarson

Hi, I am using Microsoft Access XP and have the following question.

This works.

Dim o As Object

Set o = New ClassTest

But I know the name, as a string, of the class I want to instantiate (I got
the string from using the Application.Modules property). So I want something
like this fictitious code

Set o = InstanceOfClassWhereNameIs("ClassTest")

I do need to do this. Can it be done? (in Java I would use reflection)

Thank you
Pete
 
A

Albert D. Kallal

No, but since you have to add all the class modules, then just make a
general function in a standard module that is a class wrapper. This wrapper
can return any instance of a class:

It would look like:

Public Function RClass(strC As String) As Object

' return a class object based on a passed string
Select Case strC

Case "zoo2"

Dim c1 As New clsZoo2
Set RClass = c1


Case "zoo3"
Dim c2 As New clsZoo3
Set RClass = c2

End Select

End Function

So, we can now at runtime resolve what object gets returned...

Public Sub test88()

Dim strI As String
Dim oj As Object

strI = InputBox("what class")
If strI = "" Then Exit Sub

Set oj = RClass(strI)

oj.CoolMethod


End Sub
 
Top