Seeking articles/examples set up Library file in 2010 (1 Viewer)

jdraw

Super Moderator
Staff member
Local time
Today, 12:23
Joined
Jan 23, 2006
Messages
13,409
I have created library files (mda) in 2003. I have tried to use that library with Access 2010. It is referenced and shows up in the Project window.
When I try to reference a class module from the library file using the new keyword, I get an error (commented out in red below). However, if I use a class module clsTimer, that is resident in my main database, the New keyword is acceptable.

cStringX can be seen in the library, and methods/properties seen via Object browser. Intellisense show these as well.

Can someone point me to reference materials and examples related to library files using 2010, and/or the issue with the new keyword??

Code:
'---------------------------------------------------------------------------------------
' Procedure : demoClass_CstringX
' Author    : mellon
' Date      : 08/04/2016
' Purpose   :Trying this old class module cStringX.
' Issues with the mda library; having syntax issues trying to use NEW
'---------------------------------------------------------------------------------------
'
Sub demoClass_CstringX()
 Dim omystring As cStringX
[COLOR="Red"] 'Set omystring = New cStringX[/COLOR]
   On Error GoTo demoClass_CstringX_Error

 Set omyString = cStringX
 Dim otime As clsTimer
 Set otime = New clsTimer
 omyString.Value = "This is a test string to show the functionality of this class"
 Debug.Print "String value : " & omyString.Value
 Debug.Print "String reversed : " & omyString.Reverse
 Debug.Print "String Length : " & omyString.Length
 Debug.Print "String Starting " & omyString.startingchar
 Debug.Print "String Starting 6 chars :" & omyString.startingchar(6)
 Debug.Print "String Ending 5 chars :" & omyString.endingchar(6)
 Debug.Print "String contains (zz) " & omyString.contains("zz")
 Debug.Print "String contains nc " & omyString.contains("nc")
 Debug.Print "String has char at position 7: " & omyString.charat(7)
 Debug.Print "String upper " & omyString.toProper
 Debug.Print "Time taken(ticks): " & otime.EndTimer
   On Error GoTo 0
   Exit Sub

demoClass_CstringX_Error:

    MsgBox "Error " & Err.Number & " in line " & Erl & " (" & Err.Description & ") in procedure demoClass_CstringX of Module Module4"
End Sub
 
Last edited:

MarkK

Super Moderator
Local time
Today, 09:23
Joined
Mar 17, 2004
Messages
7,761
AFAIK you need to write a public function in the library database that returns the new instance. So if you have a clsCustomClass in a library database, you should include, in a standard module in that library, a public function like . . .
Code:
Public Function NewCustomClass() as clsCustomClass
   Set NewCustomClass = New clsCustomClass
End Function
. . . which returns a new instance of the class.

I believe that in VBA you can only directly create (using the 'New' keyword) VBA classes that are defined in the current project.
 

jdraw

Super Moderator
Staff member
Local time
Today, 12:23
Joined
Jan 23, 2006
Messages
13,409
Thanks Markk.

My testing was leading me to your point
I believe that in VBA you can only directly create (using the 'New' keyword) VBA classes that are defined in the current project.

Do you have any links/articles regarding creation of Library (code file) fro A2010? My googling effort is drawing a real blank.

I was trying to adjust a class module based on this
http://access.mvps.org/access/modules/mdl0034.htm

I exported the class, modified the Exposed and Creatable values to True (as I had read) using Notepad++, then inserted the revised text(class module code) into a new class. All seemed fine, but the VERSION and Begin End

Code:
VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
END
Attribute VB_Name = "cStringEX"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = True.....
........

Showed up in red --invalid outside a procedure...

I have a feeling things have changed between 2003 and 2010, but I can't (haven't yet) find a good reference for details/sample.
 
Last edited:

MarkK

Super Moderator
Local time
Today, 09:23
Joined
Mar 17, 2004
Messages
7,761
I think you can set those properties in the IDE using the Properties windows.

1) Click main menu View->Properties Window (or hit <F4>)
- opens the Properties Window
2) Select the class you want to expose in the Project Explorer.
- Note the "Instancing" property of the selected class in the Properties Window!
3) Change the Instancing property from "1 - Private" to "2 - PublicNotCreatable"
- Once set, the class will be visible as a valid type in databases that reference this library file.
- But you still need a public function in the Library file to return a new instance of the class.
 

jdraw

Super Moderator
Staff member
Local time
Today, 12:23
Joined
Jan 23, 2006
Messages
13,409
Thanks again Markk.
Yes, I just tried the Properties window and saw the option. I just dug out some old code from when I was working (retired 8+yrs).

It's funny, I do remember having to create a function to do something like you advised.
I don't recall details, but having done as you said, I'm saying to myself --jeez I think someone had told me that years ago.

Thanks again works great!

Still looking for good reference material re classes etc.

Seems to be hidden away????
 

MarkK

Super Moderator
Local time
Today, 09:23
Joined
Mar 17, 2004
Messages
7,761
Check out this site . . .
http://www.cpearson.com/excel/classes.aspx
It says Excel, but it's VBA, and everything applies to Access. It's a sort of "Class Modules 101" for VBA.

And yes, agreed, there is not much reference material for OOP principles in VBA. Mostly I theorize that if people want to do OOP solutions they migrate to languages that support OOP better than VBA, but to my mind, it's still worthwhile, if you can get your head around it, to use classes in VBA. I encapsulate all business logic for a type, say cCustomer, in a class module in any system I write. It becomes THE single location where business logic for that type resides. Then I can instantiate that type on the Customer Form, or the Customer Subform, or the Customer report, etc... That way my UI is never bound directly to any business logic. All the business logic is in the class, etc...

Also, if you have other questions in this vein, I'd be happy to take a crack at answering, or at least participate in any discussion of the use of classes if that would help.

All the best,
 

jdraw

Super Moderator
Staff member
Local time
Today, 12:23
Joined
Jan 23, 2006
Messages
13,409
Thanks once again Markk.

I did put a list of links in this thread
http://www.access-programmers.co.uk/forums/showthread.php?t=270135
and when I went back to it, I see you have added a lot of info.

I'm going to look at the samples you added and some of the other links.

I don't have specific questions at the moment, but, being retired and not working in any development, it's more of an interest thing.

After a little review, I can see that you and Chris (stopher) have a real handle on this stuff. Much superior to the rest of us "dabblers".

It would be good to see a list of youtube or other videos on classes and their use.

jdraw
 
Last edited:

Users who are viewing this thread

Top Bottom