Solved

Late binding in VBA

Posted on 2009-07-03
11
514 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 85
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
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 

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 93

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 85
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 85
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 85

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

Ready to get started with anonymous questions?

It's easy! Check out this step-by-step guide for asking an anonymous question on Experts Exchange.

Question has a verified solution.

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

Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
If you need a simple but flexible process for maintaining an audit trail of who created, edited, or deleted data from a table, or multiple tables, and you can do all of your work from within a form, this simple Audit Log will work for you.
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
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 …

635 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