Solved

Late binding in VBA

Posted on 2009-07-03
11
503 Views
Last Modified: 2013-11-28
How do I use late binding with custom-made classes in Access VBA?

I've created a couple of classes and want to be able to instantiate a object during execution based on a value selected in a list on a form. So if a user select item_1 I want to create an object of class clsItem1, item_2 results in an object of clsItem2, ....

I'm trying to avoid select...case-statements.
0
Comment
Question by:De_Cisse
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
11 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 24771050
you can create an object with a string giving the name:

Dim c as Object
set c = CreateObject("yourclassstring") 
is that what you are looking for?

Open in new window

0
 

Author Comment

by:De_Cisse
ID: 24771119
Something like that, but CreateObject doesn't seem to work with custom-made classes (error 429: ActiveX component cannot create object)
0
 
LVL 84
ID: 24772032
What is the benefit of using late binding with your class module? Late binding can help with referencing issues (i.e. you have ObjectA.Version1 on MachineA, but ObjectA.Version2 on Machine2). With custom classes (which I assume would be included directly in your project), what's the need for late binding?

0
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 

Author Comment

by:De_Cisse
ID: 24772208
I use it in this context:

the application has different reports that can be exported to excel. These exports are all defined in different classes. I use a table to stock all available reportsexports with their corresponding classnames.

Users can select an export from a list and a method of the class is invoked.

Advantages:
      - reduced code (no if ... then/ select ... case needed)                    
      - when a new report is available a new class is imported in Access and a new entry is made in a table. No changes in the code are necessary.
0
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 24772264
Hello De_Cisse,

If I understand you correctly, then the CreateObject approach will work if and only if your classes are built
into a compiled DLL, and you then register the DLL.

What I would do in your case is create a "library" db that contains the class code, and then set a reference
to that library db in whatever other fornt-end db files as need the code.  That way, changes to the class
code are still made in just one place.

Regards,

Patrick
0
 

Author Comment

by:De_Cisse
ID: 24772474
Do I still need to compile the classes to dll if I use the suggested library-db? Because if I simply reference the db in my current project I'm still unable to instantiate objects for these classes.

If I need to compile: what's the best way to do?

0
 
LVL 84
ID: 24772480
I see ... I've never used this approach (although it is a novel idea), so have never had to test your specific scenario.

How does each report class differ, from the viewpoint of Properties, Functions etc? I realize that each would work with a specific set of tables/queries to build the import/export, but does each class implement the same Functions, Properties, etc? I'm trying to determine if these are basically generic classes with the only difference being dynamic values (like where to export, what tables to use, etc), or if they are truly unique classes. VBA is quite limited in terms of true OO concepts, but it does support some of the simpler concepts.

Can you post a sample db with a couple of your classes and sufficient objects to make this work? If you can, please specify exactly how we are to use it ...
0
 
LVL 84
ID: 24772486
Referencing the library would still require you to revisit your code in the referencing database ... IOW, if you add "clsAuditReport3" to your referenced database, you'd still have to build/change code in the "main" database to make use of that class.
0
 

Author Comment

by:De_Cisse
ID: 24772569
It is indeed based on OO-principles:

each class has the same properties and same functions, but the implementation of the main-function (exportData) is different for each class: one is a straigth export from a table to excel, the seconde is an export of some fields from multiple tables to a predefined excel-sheet, .....

If I would use full-OO I would create 1 masterclass (or an interface) and use inheritance (or implements). But this doesn't work with Access, so I kind of hoped I could use the workaround.

If my explanation is not clear enough, I'll provide a small demo-mdb.
0
 
LVL 84

Accepted Solution

by:
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 500 total points
ID: 24772699
Implements is supported in VBA:

http://msdn.microsoft.com/en-us/library/aa165323(office.10).aspx

That's what I was thinking when I read your comment (i.e. Implements) ... I've used Implements in Access/VBA and it seems to work just as you'd expect.
0
 

Author Comment

by:De_Cisse
ID: 24782904
Implements can't help me out here, because I still have to determine which type of 'subclass' I need. It seems impossible to instantiate objects using a string value.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question