Solved

Late binding in VBA

Posted on 2009-07-03
11
481 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
11 Comments
 
LVL 142

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
 

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
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.
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…

708 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now