Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

standalone classes for multiple access DB's to share common code 3

Status
Not open for further replies.

1DMF

Programmer
Jan 18, 2005
8,795
GB
Hi,

I have a few MS applications for different departments, each has identical code for certain features.

Each time a change is made in one I have to make the same change in all the others.

How do i take the VBA code out of MS access so it is a stand alone class that all the DB's use so I then only ever need to make changes in one place and all the applications use the class in a proper OO manner.

Would I create a separate MS DB which the others link to? or import from?

Do i turn the class module into a DLL or a .vb file?

all advice appreciated


Thanks,
1DMF

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Google Rank Extractor -> Perl beta with FusionCharts
 
So in the library database with my dummy class, I can put the function in a standard module that returns a dummyClass
Code:
Public Function NewDummy(Optional theName As String = "No Name", Optional theWeight As Double = 12.5, Optional theShipDate As Variant = Null) As DummyClass
  If Not IsDate(theShipDate) Then theShipDate = Now
  Set NewDummy = New DummyClass
  NewDummy.MyClassInitialize theName, theWeight, CDate(theShipDate)
End Function
In my calling database
Code:
Public Sub testClass()
  'dimension as an object not a DummyClass
  Dim dcDummy2 As Object
  'initialize
  Set dcDummy2 = dbDummyClass.NewDummy("DC", 100, #1/1/2011#)
  'you do not get intellisense below
  dcDummy2.PrintObjectInfo
End Sub
 
[lol] no wonder I was banging my head on the wall.

Seems odd the way you can reference an external MDA/MDE/MDB with classes yet by default you can't create instances of the class!

Kinda defeats the whole point of OOP doesn't it!

So in Access not only do you have to write the standard property / accessor methods, you have to write accessor methods to create instances of your classes, very conveluted indeed.

Again thanks for your most valuable help in all this!




"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Google Rank Extractor -> Perl beta with FusionCharts
 
Aggh anothing, if I edit the referenced library code from within the main DB , it allows me to.

But when I save and exit, when I re-open the DB all the new code is missing?

What's going on , why does it appear to allow me to edit the referenced library code if it then doesn't save it?

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Google Rank Extractor -> Perl beta with FusionCharts
 
I would look at the second method to expose your classes. This then gives your intellisense and fully exposes the class. I do not know why MS does not expose the classes, but there must be a reason because the fix would be so easy.
 
looks like i'll have to give that a whirl, i still can't create a global var using the other method because you can't call the routine outside a function or event handler.

you just get invalid outside procedure!

Why does it always seem like it's 2 steps forward 1 step back!

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Google Rank Extractor -> Perl beta with FusionCharts
 
can you show what you are trying to do? And where you are trying to do it? If you declare a global variable in a standard module of the library you can reference it in the calling db. However, that is probably not what you are saying, and that would really violate good practice. One of the big ideas of OO is not to expose any global variables and all access to the object is through let, get, sets.
 
collections for my classes?

do you mean being able to create a collection which holds instances of my classes?

what i'm trying to do is this...

Code:
Public oUser As Object
oUser = newUser

this is placed in the main DB module code.

right at the top where you define all application global vars and declare additional functions from the window's API

the idea being as soon as the DB opens a user object is created available within the whole application.

ie. a global user object.

the function newUser is in the library DB as module code and is defined like so...

Code:
Public Function newUser() As clsUser

    newUser = New clsUser
    
End Function

Thus creating a 'constructor' accessor method as you described earlier.

clsUser is in the library DB as a class module

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Google Rank Extractor -> Perl beta with FusionCharts
 
I seem to be going round in circles here.

I;ve exported the class and reimnported changing the VB_Exposed attribute, and now the main DB can see the class module and declare variables as the class type.

I've also come to the conclusion thatyou can only assign values to constants in the module header.

But I've now added the following in the main DB with a Form_Open event and used
Code:
oUser = New clsUser
but that now errors with
invalid use of New keyword
so although I can now see the class I can't instatiate any objects of the class?

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Google Rank Extractor -> Perl beta with FusionCharts
 
In VB you can dimension at the top, which only reserves memory

public Ouser as clsUser

or you can dimension and initialize in the same line, thus creating the object
public oUser as new clsUser

you cannot

public oUser as clsUser
oUser = new clsUser


you can dimension at the top and then initialize within a procedure

public oUser as clsUser

public sub somSub
'notice the Set keyword
set oSuer = new clsUser
end sub
 
should say
public sub someSub()
'notice the Set keyword
set oUser = new clsUser
end sub
 
hmm totally confused on this, all that VB_Exposed seems to have done is change the 'instancing' setting from 1-Private to 2-PublicNotCreatable

So although I can see the class and declare variables of the class type I cante create any objects of the class?

And why did I need to export , edit and import, this setting is available in the IDE.

Clarity on all this is appreciated.

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Google Rank Extractor -> Perl beta with FusionCharts
 
Is your last post of 12:07 before seeing my post of 12:04.

Also where is the setting in the IDE, and what version of Access? This feature may be new.
 
lol , yes and i was just coming back to post my faux pas , as I worked out i'd forgot the Set !! d'oh

So you make it visible , but not creatable and then write a bespoke accessor (contructor) method and pass it back, wow , very conveluted.... But it now works, thank you , thank,you, thank you!

Now to the next question.

How do I access the forms in the 'library' from the main DB?

The library DB has a Login form in it , but when I try to open it from the main DB I get a form doesn't exist error

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Google Rank Extractor -> Perl beta with FusionCharts
 
So you make it visible , but not creatable and then write a bespoke accessor (contructor) method and pass it back
Now you are confusing me.
Once you edit it with "method 2", it should be fully exposed. You should be able to create without a problem. You may need a bogus initialize event to be able to create it with initial properties.

I am not sure if you want to open a form in another database, that is a different issue. You have to open the library database using standard automation to do that.
 
OK, I need to recheck tomorrow, I may have had the missing 'Set' problem and confused myself!

Currently it is working with the dummy 'constructor' function
Code:
Public Function newUser() As clsUser    
Set newUser = New clsUser    
End Function
so in my main DB I had
Code:
Set oUser = newUser

I'll look into this 'standard automation', I thought once you created a reference to the 'Library' MDB you got access to everything!

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Google Rank Extractor -> Perl beta with FusionCharts
 
I think once you edit the class using method 2 you do not have to use the function (although you still can). You should be able to do something like this in your calling database because the class should now be exposed.

public oUser as new clsUser

now be able to use oUser anywhere.

By setting a reference to another database you get access to the code without having to actually open the database. In order to get to the physical forms, you would have to open an instance of that database. Lots of code on the net to open another instance and control the open instance using "automation".
 
nope doesn't seem to work
Code:
Public oUser As New clsUser

Errors with
invlaid use of New keyword
, the intelisense pops up after the 'New' and my class name is in the list, but the code refuses to compile with that error?

The instancing option in design view on the class module is either
1 - Private
2 - PublicNotCreatable

So you either can't see it, or you can see it but not instantiate it?

I'm using MS02003 , perhaps that's the problem?

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Google Rank Extractor -> Perl beta with FusionCharts
 
hmmm, I'm not sure how you manage to get it to work as I just found this MS-KB stating it can't be done exactly as I am finding and you need to create a helper 'constructor' method as I was doing...


Next, since the Instancing property of the class is PublicNotCreatable, the project must provide a way for a client to instantiate the object. Add a new function in a standard module:

Option Explicit
Public Function New_clsEmployee() As clsEmployee
Set New_clsEmployee = New clsEmployee
End Function

where clsEmployee is the name of the class of interest. Also, this should not be a private module.
No wonder I was going round in circles!

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Google Rank Extractor -> Perl beta with FusionCharts
 
This automation seems rather long winded and it seems you need to hard code the MDB path?

I simply created a function in the 'library' DB module code, like so
Code:
Function getForm(frm As String, Optional args As String)
    DoCmd.OpenForm frm, , , , , , args
End Function
so now from the main DB I simply call the fucntion with myFormName & myOpenArgs and the form in the library DB is now opened :)

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Google Rank Extractor -> Perl beta with FusionCharts
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top