Using class modules in a code library 
Author Message
 Using class modules in a code library

I am attempting to move a class module from the application database file
into a code library (mda) file. At first I couldn't see the class module
from the application file but I solved that one with some help from Steve
Arbaugh's site at http://www.*-*-*.com/

However, I still can't use the class in the application file. When I try to
instantiate the class, I get an "Invalid use of New Keyword" error. Any
suggestions from anyone?

Joel Reinford



Mon, 07 Jun 2004 02:41:06 GMT  
 Using class modules in a code library
There are other ways, but my own preference is to include in the code
library a function that returns an instance of the class:

Public Function GetFoo() As Foo

    Dim f As Foo
    Set f = New Foo
    Set GetFoo = f
    Set f = Nothing

End Function

--

Brendan Reynolds


Quote:
> I am attempting to move a class module from the application database file
> into a code library (mda) file. At first I couldn't see the class module
> from the application file but I solved that one with some help from Steve
> Arbaugh's site at http://ourworld.compuserve.com/homepages/attac-cg/.

> However, I still can't use the class in the application file. When I try
to
> instantiate the class, I get an "Invalid use of New Keyword" error. Any
> suggestions from anyone?

> Joel Reinford



Mon, 07 Jun 2004 05:38:44 GMT  
 Using class modules in a code library
This is a limitation of VBA - you can not create a PublicCreatable
class. No problem - just create a "helper" function in a standard
module in the library db that returns a new instance of your class.
For a class named Dog.

Public Function NewDog() as Dog
    NewDog=new dog
end function

Often, I will also build in the parameters to set up the new instance
of the class and do it all from the helper function.

--

Sandra Daigle, Microsoft Access MVP


Quote:
> I am attempting to move a class module from the application database
file
> into a code library (mda) file. At first I couldn't see the class
module
> from the application file but I solved that one with some help from
Steve
> Arbaugh's site at

http://ourworld.compuserve.com/homepages/attac-cg/.
Quote:

> However, I still can't use the class in the application file. When I
try to
> instantiate the class, I get an "Invalid use of New Keyword" error.
Any
> suggestions from anyone?

> Joel Reinford



Mon, 07 Jun 2004 05:42:17 GMT  
 Using class modules in a code library
Of course I meant to say . . . left off the "Set" ;-)

 Public Function NewDog() as Dog
    set NewDog=new dog
 end function

--

Sandra Daigle, Microsoft Access MVP


Quote:
> This is a limitation of VBA - you can not create a PublicCreatable
> class. No problem - just create a "helper" function in a standard
> module in the library db that returns a new instance of your class.
> For a class named Dog.

> Public Function NewDog() as Dog
>     NewDog=new dog
> end function

> Often, I will also build in the parameters to set up the new
instance
> of the class and do it all from the helper function.

> --

> Sandra Daigle, Microsoft Access MVP



> > I am attempting to move a class module from the application
database
> file
> > into a code library (mda) file. At first I couldn't see the class
> module
> > from the application file but I solved that one with some help
from
> Steve
> > Arbaugh's site at
> http://ourworld.compuserve.com/homepages/attac-cg/.

> > However, I still can't use the class in the application file. When
I
> try to
> > instantiate the class, I get an "Invalid use of New Keyword"
error.
> Any
> > suggestions from anyone?

> > Joel Reinford



Mon, 07 Jun 2004 05:53:16 GMT  
 Using class modules in a code library
Hi Brendan,
GetFoo is available to be used as a var of the type as it is declared in
the function.
So all you would need is:

Public Function GetFoo() As Foo
  Set GetFoo = New Foo
End Function

Just my $.02 worth.

HTH
Stephen Lebans
http://www.lebans.com
Access Code, Tips and Tricks
Please respond only to the newsgroups so everyone can benefit.


Quote:
> There are other ways, but my own preference is to include in the code
> library a function that returns an instance of the class:

> Public Function GetFoo() As Foo

>     Dim f As Foo
>     Set f = New Foo
>     Set GetFoo = f
>     Set f = Nothing

> End Function

> --

> Brendan Reynolds



> > I am attempting to move a class module from the application database
file
> > into a code library (mda) file. At first I couldn't see the class
module
> > from the application file but I solved that one with some help from
Steve
> > Arbaugh's site at

http://ourworld.compuserve.com/homepages/attac-cg/.

- Show quoted text -

Quote:

> > However, I still can't use the class in the application file. When I
try
> to
> > instantiate the class, I get an "Invalid use of New Keyword" error.
Any
> > suggestions from anyone?

> > Joel Reinford



Mon, 07 Jun 2004 09:26:46 GMT  
 Using class modules in a code library
Yes, I've got into the habit of avoiding "= New", but I have to admit that
in this particular situation, I can't actually think of any real
justification for avoiding it! :-)

Most of my functions of this type tend to be a little more complex than this
example, though. I use optional arguments to the function to set properties
of the object, so that in the calling code I can create an instance of the
object and set the properties in one line of code:

Public Function GetFoo(Optional ByVal Arg1 As String, Optional ByVal Arg2 As
String) As Foo

    Dim f As Foo

    Set f = New Foo
    With f
        .Prop1 = Arg1
        .Prop2 = Arg2
    End With
    Set GetFoo = f
    Set f = Nothing

End Function

--

Brendan Reynolds


Quote:
> Hi Brendan,
> GetFoo is available to be used as a var of the type as it is declared in
> the function.
> So all you would need is:

> Public Function GetFoo() As Foo
>   Set GetFoo = New Foo
> End Function

> Just my $.02 worth.

> HTH
> Stephen Lebans
> http://www.lebans.com
> Access Code, Tips and Tricks
> Please respond only to the newsgroups so everyone can benefit.



> > There are other ways, but my own preference is to include in the code
> > library a function that returns an instance of the class:

> > Public Function GetFoo() As Foo

> >     Dim f As Foo
> >     Set f = New Foo
> >     Set GetFoo = f
> >     Set f = Nothing

> > End Function

> > --

> > Brendan Reynolds



> > > I am attempting to move a class module from the application database
> file
> > > into a code library (mda) file. At first I couldn't see the class
> module
> > > from the application file but I solved that one with some help from
> Steve
> > > Arbaugh's site at
> http://ourworld.compuserve.com/homepages/attac-cg/.

> > > However, I still can't use the class in the application file. When I
> try
> > to
> > > instantiate the class, I get an "Invalid use of New Keyword" error.
> Any
> > > suggestions from anyone?

> > > Joel Reinford



Mon, 07 Jun 2004 18:42:49 GMT  
 Using class modules in a code library
It seems to me that the helper function method would be messy since you'd be
creating an object in both the library code and the callling application
without a good way to destroy the object that the library instantiated.

Fortunately, I got the answer I like better from Steve Arbaugh. For anyone
else who wants to do this, here's how. I'm working in Access 2000 but I
believe this answer would apply to 97 and 2002 as well.

The way to make this happen is to create a plain text file with these two
lines.
VB_Creatable = True
VB_Exposed  = True

In the VBE:
1) Insert a new class module
2) From the Insert menu choose File and insert the text file
3) Add the class module code

Now the class module can be instantiated directly from the calling
application without any helper functions.
It seems as though you have to repeat this process each time. For example, I
tried to save a "starter" class module that would have these attributes set
this way. My idea was that I would do a  "Save As" from the Access window so
I wouldn't have to keep inserting the text file. However, that didn't work
for me.

Steve's website got me started in the right direction. It's at
http://ourworld.compuserve.com/homepages/attac-cg/ if anyone else would like
to see his comments.

Best wishes for the holidays!


Quote:
> This is a limitation of VBA - you can not create a PublicCreatable
> class. No problem - just create a "helper" function in a standard
> module in the library db that returns a new instance of your class.
> For a class named Dog.

> Public Function NewDog() as Dog
>     NewDog=new dog
> end function

> Often, I will also build in the parameters to set up the new instance
> of the class and do it all from the helper function.

> --

> Sandra Daigle, Microsoft Access MVP



> > I am attempting to move a class module from the application database
> file
> > into a code library (mda) file. At first I couldn't see the class
> module
> > from the application file but I solved that one with some help from
> Steve
> > Arbaugh's site at
> http://ourworld.compuserve.com/homepages/attac-cg/.

> > However, I still can't use the class in the application file. When I
> try to
> > instantiate the class, I get an "Invalid use of New Keyword" error.
> Any
> > suggestions from anyone?

> > Joel Reinford



Wed, 09 Jun 2004 06:18:34 GMT  
 Using class modules in a code library
I'm not criticising Steve's alternative, but if you look at the examples I
posted, you'll see that they do destroy the object that was instantiated in
the library.

--

Brendan Reynolds


Quote:
> It seems to me that the helper function method would be messy since you'd
be
> creating an object in both the library code and the callling application
> without a good way to destroy the object that the library instantiated.

> Fortunately, I got the answer I like better from Steve Arbaugh. For anyone
> else who wants to do this, here's how. I'm working in Access 2000 but I
> believe this answer would apply to 97 and 2002 as well.

> The way to make this happen is to create a plain text file with these two
> lines.
> VB_Creatable = True
> VB_Exposed  = True

> In the VBE:
> 1) Insert a new class module
> 2) From the Insert menu choose File and insert the text file
> 3) Add the class module code

> Now the class module can be instantiated directly from the calling
> application without any helper functions.
> It seems as though you have to repeat this process each time. For example,
I
> tried to save a "starter" class module that would have these attributes
set
> this way. My idea was that I would do a  "Save As" from the Access window
so
> I wouldn't have to keep inserting the text file. However, that didn't work
> for me.

> Steve's website got me started in the right direction. It's at
> http://ourworld.compuserve.com/homepages/attac-cg/ if anyone else would
like
> to see his comments.

> Best wishes for the holidays!



> > This is a limitation of VBA - you can not create a PublicCreatable
> > class. No problem - just create a "helper" function in a standard
> > module in the library db that returns a new instance of your class.
> > For a class named Dog.

> > Public Function NewDog() as Dog
> >     NewDog=new dog
> > end function

> > Often, I will also build in the parameters to set up the new instance
> > of the class and do it all from the helper function.

> > --

> > Sandra Daigle, Microsoft Access MVP



> > > I am attempting to move a class module from the application database
> > file
> > > into a code library (mda) file. At first I couldn't see the class
> > module
> > > from the application file but I solved that one with some help from
> > Steve
> > > Arbaugh's site at
> > http://ourworld.compuserve.com/homepages/attac-cg/.

> > > However, I still can't use the class in the application file. When I
> > try to
> > > instantiate the class, I get an "Invalid use of New Keyword" error.
> > Any
> > > suggestions from anyone?

> > > Joel Reinford



Wed, 09 Jun 2004 20:51:43 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. Code Library and Class Modules

2. Q: Code Module vs UserControl (Form, Class) module.

3. Q: Code Module vs UserControl (Form, Class) module.

4. Q: Code Module vs UserControl (Form, Class) module.

5. VB6 class module turns into regular module using VB6 IDE

6. Using a UDT in a Class method, how to make a class module public

7. Class modules 97 - Refering to array within a custom class module

8. Useing Class modules in Library mda

9. Accessing procedures of a class module from another class module

10. Binary compatibility, Public Class, Name conflicts with existing module, project, or object library

11. Code / Class libraries for VB5...?

12. Create class module with code

 

 
Powered by phpBB® Forum Software