Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Late binding in VBA

Posted on 2009-07-03
11
Medium Priority
?
531 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 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
Configuration Guide and Best Practices

Read the guide to learn how to orchestrate Data ONTAP, create application-consistent backups and enable fast recovery from NetApp storage snapshots. Version 9.5 also contains performance and scalability enhancements to meet the needs of the largest enterprise environments.

 

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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
Instead of error trapping or hard-coding for non-updateable fields when using QODBC, let VBA automatically disable them when forms open. This way, users can view but not change the data. Part 1 explained how to use schema tables to do this. Part 2 h…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

877 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