Solved

Late binding in VBA

Posted on 2009-07-03
11
498 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 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: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 

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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

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…
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.
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…

679 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