Class instancing

  • Thread starter Thread starter Günter Brandstätter
  • Start date Start date
G

Günter Brandstätter

Hi all,

I wrote a new class-module in an Access-database mod.mdb which I wanted to
use in another database by referencing to mod.mdb. When I now want to use
the class module in my new database, I am not able to create an instance of
my class-module, because its instancing is set to private.
The only other possibility is to set its instancing to "Public NotCreatable"
which does not help at all, because I *want* to create new instances of my
class.
How should I code to use my class-module from other databases?

any answer appreciated
Günter
 
Hi, Günter.

To make classes "Public," follow the steps listed on this Web page:

http://www.mvps.org/access/modules/mdl0034.htm

Please note that Microsoft does not support this method, so future versions
of Access may not be able to use this method.

HTH.

Gunny

See http://www.QBuilt.com for all your database needs.
See http://www.Access.QBuilt.com for Microsoft Access tips.

(Please remove ZERO_SPAM from my reply E-mail address, so that a message
will be forwarded to me.)
 
Hi Gunny,

thank you for your answer. It solves my problem partially. Now the class is
visible to other databases, but I am still not able to create an instance of
it. I also tried to set the other attributes in the exported code to true,
but that also did not help.

Do you know what else I could do??

Günter
 
Hi, Günter.
visible to other databases, but I am still not able to create an instance of
it. I also tried to set the other attributes in the exported code to true,
but that also did not help.

Attribute VB_Exposed is the only attribute you need to change to get
external databases to see it. What syntax are you using to instantiate the
class object?

HTH.

Gunny

See http://www.QBuilt.com for all your database needs.
See http://www.Access.QBuilt.com for Microsoft Access tips.

(Please remove ZERO_SPAM from my reply E-mail address, so that a message
will be forwarded to me.)
 
Hi Gunny,

I see the class from other databases, my syntax to instantiate is
Dim myObject As New myClass
and the error I get is that the reserved word 'New' is not allowed.
I also tried
Dim myObject As Object
Set myObject = CreateObject("myClass")
makes no difference.
When I open the code of the class-module in the original database its
instancing property is set to '2 - Public NotCreatable'. This is, what
causes my problem. I will try to make a DLL-library out of my class, but
this might take some time because I'm not very familiar with Visual Studio.

Günter
 
Hi, Günter.

I see what the problem is. The VB_Creatable attribute is automatically
being changed to False in your version of Access. Different versions of
Access have different effects on the module attributes. I don't know why
there's no warning on that Web page about these pitfalls and the
work-arounds. That Web page has been there for quite some time, since at
least September 1999.

Which version of Access are you using? Access 2003 doesn't have any
work-arounds that I can see to get around the automatic changing of the
VB_Creatable attribute, but there are two work-arounds for the automatic
changing of the VB_Exposed attribute from Access 2K. Access 97 is not
affected, and I haven't tested Access XP.

As I see it, your alternatives are to:

1.) Create the DLL and use this as a Reference library in your database
applications.
2.) Create two public functions in a module in the library database that
a.) creates an instance of the object and b.) destroys that object when
finished. This can cause problems, though. It's very difficult to
guarantee that the object will be destroyed, even with proper error
handling. Only a single instance of the class can be instantiated at a
time, so a collection of these objects is out of the question.
3.) Use a different version of Access where work-arounds are available.
4.) Copy the class from the library database and paste it into any project
that needs the class. (This is very difficult to maintain concurrent
versions if you start using this class in multiple projects. I don't
recommend it.)

HTH.

Gunny

See http://www.QBuilt.com for all your database needs.
See http://www.Access.QBuilt.com for Microsoft Access tips.

(Please remove ZERO_SPAM from my reply E-mail address, so that a message
will be forwarded to me.)
 
Hi Gunny,
thank you for your explications. Unfortunately, I *am* using Access 2003.
What I am presently trying to do is to create a DLL, but it is difficult
because this object uses data-connection and I don't know anything about
this issue in DLLs.
The solution I will engage is to copy this module into all databases where
it is needed.

Thanks for your time,
Günter
 
Günter Brandstätter said:
I see the class from other databases, my syntax to instantiate is
Dim myObject As New myClass
and the error I get is that the reserved word 'New' is not allowed.
I also tried
Dim myObject As Object
Set myObject = CreateObject("myClass")
makes no difference.
When I open the code of the class-module in the original database its
instancing property is set to '2 - Public NotCreatable'. This is, what
causes my problem.


I don't understand your problem well enough to participate
in this discussion, but I was wondering if you could get
what you want by creating a public function in a standard
module in the library mdb:

Public Function CreateClass() As MyClass
Set CreateClass = New MyClass
End Sub
 
You're welcome! I learned some valuable information, too, because all of
our library classes are in Access 97 and 2K, so an upgrade to 2003 is going
to involve more work than I thought.

Gunny

See http://www.QBuilt.com for all your database needs.
See http://www.Access.QBuilt.com for Microsoft Access tips.

(Please remove ZERO_SPAM from my reply E-mail address, so that a message
will be forwarded to me.)
 
Hi, Marsh.
I was wondering if you could get
what you want by creating a public function in a standard
module in the library mdb:

Public Function CreateClass() As MyClass
Set CreateClass = New MyClass
End Sub

This is a common solution, but one of the downsides is that it is difficult
to guarantee that the object gets destroyed. Günter's class uses data
connections, so this can become critical in that a set of the finite number
of TableIDs available (only 2048 in Access 2003) will become reserved for
this object and never released from memory, as well as large amounts of
memory that can become reserved by RecordSets created by this object that
are never released from memory, either.

Another downside is that one can only instantiate a single object of the
library class at a time, so collections or arrays of this object aren't
possible, although Günter's class may not need this capability.

HTH.

Gunny

See http://www.QBuilt.com for all your database needs.
See http://www.Access.QBuilt.com for Microsoft Access tips.

(Please remove ZERO_SPAM from my reply E-mail address, so that a message
will be forwarded to me.)
 
Back
Top