what's the use of PublicNotCreatable?

hey guys, i wanted to create a resource library / resource repository for my code in Access and i was thinking of using PublicNotCreatable. however after playing around with it for awhile and reading some articles on it i realised that it's meant for a different reason - not to emulate a static class or abstract class.

from my understanding it's used by a function in its containing project to be created and passed to its caller. completely doesn't make sense to me at all. below is the reference article i read.


question time!

1) how come the PublicNotCreatable class can be created by a function in its containing project? shouldn't it then be ProtectedCreatable?
2) what's the use of this enforced additional function layer to create this object?
3) how do i create a static class in vba?
4) and can PublicNotCreatable behave as a static class or is the only way to create a static class to use a singleton pattern to implement this? ok i know it's still an instance but at least it's a single instance right? = )
Who is Participating?
BitsqueezerConnect With a Mentor Commented:

I cannot really explain why an external class module cannot be instantiated directly (even your article says "Might seem strange, but that's the way it is." as explanation...), maybe because a separate VBA file will be executed in an own context.

VBA is not a real object oriented language, it's only an "object based language" which uses some methods of a real OOP language so don't expect too much of it. I would say, in general it's OK what VBA offers, more would be too much for the most VBA programmers (the target customer of Access in Microsoft's eyes is not the professional C++ or Java programmer, but an Office user, so we can be glad that there IS a kind of object orientation in VBA).

Public means here: You can use such class in any project, by simply adding the file to the VBA references. The class methods and properties are offered externally so you can use this like a DLL. But as they restricted it to be instantiated in the running VBA context you are forced to use the "function bridge" which instantiate the object "in the other VBA" and returns the completed object to the using VBA.

A static class in VBA is simply a standard module. That's a class module which is automatically instantiated at the time you leave a row after writing it. If you define a Public variable in a standard module you can immediately use it. You do not have all the same possibilities of a real static class in OOP languages but always remember: It's not OOP here.

A kind of an abstract class is the concept of the Interface class in VBA. You can use the keyword "Implements" in a class module to insert an interface class into a class module. The interface class only defines empty subs, properties and so on and contains no code (but it CAN also contain code if you want).

You can handle any class (including external) as a static class by using a simple object variable/function in a standard module (which is a static class):

Private objMyClass As clsMyClass

Public Function MyClass() As clsMyClass
    If objMyClass Is Nothing Then Set objMyClass = New clsMyClass
    Set MyClass = objMyClass
End Function

Open in new window

With this you can always directly instantiate a single object of a class and you always get the same object back. Moreover, you can go on instantiating independent object variables additionally.

The possibility of using external code libraries in all Office applications is a great thing I think because you can use anything which is a class module in other applications. That includes also Access Forms or Reports which are also nothing else than class modules with a graphical user interface. Means: You can reuse standard forms (like your own message box created as a form in a library file) in other applications on the same way.


Helen FeddemaCommented:
A number of versions ago, there was a Code Library add-in for Access that could be used for this purpose, but it was dropped the next version.  I keep code segments in plain text files in a folder.  Crude, but it works, and it won't go obsolete.
developingprogrammerAuthor Commented:
i see - so how do you refer to the code in your text file then? thanks Helen!
Get 10% Off Your First Squarespace Website

Ready to showcase your work, publish content or promote your business online? With Squarespace’s award-winning templates and 24/7 customer service, getting started is simple. Head to Squarespace.com and use offer code ‘EXPERTS’ to get 10% off your first purchase.

developingprogrammerAuthor Commented:
whao Christian, this is definitely a lot to digest cause we're talking more about a meta level now which i am not too familiar with, but i get your drift and just need to understand the implementation of it. but what you're saying is extremely powerful and reuseable. let me spend a bit more time on this and reply you with intelligent pointed answers = ) thanks Christian and you're amazing!! = ))
aikimarkConnect With a Mentor Commented:
If you share many modules/classes across your Access projects, you will want to create some code that will import the latest copies of the (text) files into your VBProject object.

If you can package your code into an ActiveX object, then you only need to instantiate that object to get at its methods and properties.
developingprogrammerAuthor Commented:
guys i need a bit more time to digest this. i'll get back to yall! = )
developingprogrammerAuthor Commented:
guy need more time as still not at the finishing stages of the project yet sorry!
developingprogrammerAuthor Commented:
hey Christian and aikimark,

thanks so much for your help and once again i'm so sorry for the delayed response.

Christian i think you've very clearly answered my question here in terms of why it is PublicNotCreatable - probably cause it runs in its own context = ) so i'm closing this question as per what the admins have advised me to do.

however i've got more questions which i will ask at a later date - saving this in my personal knowledgebase.

basically i think it's a bit fuzzy in my head now as to what constitutes the domain of creating reuseable components which we can pass around.

the things in my head now are as per below - we've got a few terms, how do they relate to each other and how do they form the realm and choices of how we implement reuseable components.

will ask at a later date but here they are below. thanks Christian and aikimark once again for being so patient with me!! = )

activex object
import the latest copies of the (text) files into your VBProject object.
add ins
external code libraries
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.