Results 1 to 5 of 5
  1. #1
    RMittelman is offline Advanced
    Windows 10 Access 2016
    Join Date
    Dec 2010
    Location
    Simi Valley, CA
    Posts
    211

    Create a Class Instance by Name in Access VBA

    So I have a "Reference" DB which contains many of my utility functions and classes. I, well, reference this database from my other databases to make use of the contained modules and/or classes. Since I can't instantiate a class object in this DB from the client DB directly, I have a class factory module in the reference DB with this method:

    Code:
    Public Function CreateClass(className As String) As Object
    
        Dim myClass As Object
        
        Select Case className
            Case "clsChilkat"
                Set myClass = New clsChilkat
            Case "clsExcel"
                Set myClass = New clsExcel
            Case "clsFileDialog"
                Set myClass = New clsFileDialog
            Case "clsInfo"
                Set myClass = New clsInfo
            Case "clsOutlook"
                Set myClass = New clsOutlook
            Case "clsXml"
                Set myClass = New clsXML
        End Select
        Set CreateClass = myClass
        Set myClass = Nothing
    
    
    End Function
    In my client DB, I instantiate the class like this:
    Code:
    Dim oInfo As clsInfo
    Set oInfo = CreateClass("clsInfo")
    This is working perfectly so far, except it's subject to errors if I add a new class to the reference DB and forget to add a Case for it in the above code. What I'd like to do is remove the need for the Select Case block and somehow create the class object just from its string name. The client DB would still be coded as above, but is there a way to create a class instance in VBA code directly from the name of the class, similar to CallByName?



    Thanks...

  2. #2
    Edgar is online now Competent Performer
    Windows 8 Access 2016
    Join Date
    Dec 2022
    Posts
    274
    What happens is you just do this?
    Code:
    Public Function CreateClass(className As String) As Object
         Set CreateClass = CreateObject(className)
    End Function

  3. #3
    orange's Avatar
    orange is offline Moderator
    Windows 10 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    I am not proficient with classes, BUT I did have a question about using a class housed in a library database in a different database.
    This was part of the interaction that help resolve the issue I had.

    Your issue may be different, but Markk's solution worked for me.

  4. #4
    RMittelman is offline Advanced
    Windows 10 Access 2016
    Join Date
    Dec 2010
    Location
    Simi Valley, CA
    Posts
    211
    Quote Originally Posted by Edgar View Post
    What happens is you just do this?
    Code:
    Public Function CreateClass(className As String) As Object
         Set CreateClass = CreateObject(className)
    End Function
    Thanks Edgar. I tried that, and got the following error:
    ActiveX component can't create object


  5. #5
    RMittelman is offline Advanced
    Windows 10 Access 2016
    Join Date
    Dec 2010
    Location
    Simi Valley, CA
    Posts
    211
    Quote Originally Posted by orange View Post
    I am not proficient with classes, BUT I did have a question about using a class housed in a library database in a different database.
    This was part of the interaction that help resolve the issue I had.

    Your issue may be different, but Markk's solution worked for me.
    Thanks orange, but I couldn't find anything in that article or any of its links that answered the question. I'm trying to use something like CallByName, but not to get/set properties or call Subs or Functions.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 5
    Last Post: 04-29-2019, 03:16 AM
  2. Replies: 1
    Last Post: 10-12-2017, 11:42 AM
  3. Replies: 5
    Last Post: 10-11-2013, 07:29 AM
  4. Create new form instance at the module level
    By DevSteve in forum Modules
    Replies: 1
    Last Post: 09-11-2012, 11:47 AM
  5. Create a new Word-instance, probs with Citrix
    By chappy72 in forum Access
    Replies: 1
    Last Post: 01-11-2012, 05:30 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums