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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Public / Static help pls

Status
Not open for further replies.

Blackshark

Programmer
May 7, 2002
48
GB
Hi guys,

I have a database that creates jobs in our business applications. The code I use contains this at the top of the module:

Private KEASystem As KEA.System
Private KEASessions As KEA.sessions
Private KEASession As KEA.Session
Private KEAScreenn As KEA.Screen

When ever I want to use a terminal window to set up a job or run a report I use the following in a function:

Set KEASystem = CreateObject("KEA.System")
Set KEASessions = KEASystem.sessions
Set KEASession = KEASessions.Item(Forms!Main!wmissessno)
Set KeaScreen = KEASession.Screen


It seems a little inefficient to have these two sets of code sitting all round by modules. What I would like to do is to have the databases main form (that is always open) create an instance of the four objects, set their parameters and then be able to use/refer to them in different forms/modules.

I have tried changing the declarations to Public but when I then change my code in other modules to:

Set Forms!Main(KEASystem) = CreateObject("KEA.System")
Set Forms!Main(KEASession) = KEASystem.sessions
Set Forms!Main(KEASession) = KEASessions.Item(Forms!Main!wmissessno)
Set Forms!Main(KeaScreen) = KEASession.Screen

However I get an error when the code executes and I find that the KEASystem object is empty and does not support the function I am trying to execute.


So what am I doing wrong? I thought that maybe Static would come in to it somewhere but my head is sore and each time I try to get round the problem I end up hitting my head against a big brick wall.


Any assistance gratefully received.

Thanks Tim
 
I am going to make a few assumptions from your question. If I am wrong please point this out and we can further discuss your problem.

I believe you have put these declarations in the General area of a specific form. When you do that they are only available within that Form. If you make the declarations at the Database module level and use the Public declaration format then they should be available everwhere within your database application.

The Set statements will have to be executed in event procedure as you need them to be but the Public declarations should be able to be declared only once.

Give that a try and let me know.

Bob Scriver
 
That is a mess. A good example of why you do not want a bunch of public variables. I have no idea what you are doing here.
Set Forms!Main(KEASystem) = CreateObject("KEA.System")
Set Forms!Main(KEASession) = KEASystem.sessions
Set Forms!Main(KEASession) = KEASessions.Item(Forms!Main!wmissessno)
Set Forms!Main(KeaScreen) = KEASession.Screen

TRY using a Public Method INSIDE the form and call it.
Public SetKEA()
Set KEASystem = CreateObject("KEA.System")
Set KEASessions = KEASystem.sessions
Set KEASession = KEASessions.Item(wmissessno)
Set KeaScreen = KEASession.Screen
End Sub Generate Forms/Controls Resizing/Tabbing Class
Compare Code (Text)
Generate Sort Class in VB or VBScript
 
Bob,


BTW - The KEA app is a terminal program for which I am using a library to create an instance of the program and then use Sendkeys and Area functions to send key strokes and receive feedback from the app.


The declarations are made at the top of my Main form. On the Open procedure I then carry out the Sets. In the Open procedure I carry out the login procedure with the KEA application.

I am doing exactly what you suggest, however when I reference the objects (I assue this is right) Forms!Main(KEASystem) or ...(KEAScreen) the objects are Empty when the Debug screen comes up.


Erm hang on, 'Database module level', am I right in thinking this is at the top of my VB code for my Main Form. Or is it in an actual module for the database rather than one of my forms???

Thanks
Tim


John,

The code above enables me to send info to and receive info from one of our Business applications. the program (KEA) is just a terminal that I can use to log in to one of our business systems. At the moment every time I want to link to the system I am having to do the above, ie. in every form where I want to link to the application I am declaring the above four objects and then in a function setting them.

It might look a mess, but what it is doing is creating an instance of the main KEA application, then the Sessions object (when set) allows me to assign myself to a single KEA session, and the Screen obejct allows be to pull out data and send keystrokes to the application/session to control it.

Thanks Tim
 
Blackshark,

Basically what you're talking about here is 'scope'. This can be a pretty complex topic, but in a nutshell, where a variable is declared will determine where it is useable in the application.

Think of procedures, form modules, and standard modules as containers within containers. Variables are visible from where they are declared, outward.

For instance, a variable declared in a procedure (sub or function) is visible only to that procedure.

A variable declared in the general declarations section of a form module is visible to all procedures in that module, but cannot be seen by procedures in other forms or standard modules.

Variables declared in the general declarations section of a standard module are visible to all procedures in all forms/modules.

[tt]|--------------------------------------------|
| Module |
| |--------------------------------------| |
| | Form Module | |
| | |--------------| |--------------| | |
| | | Sub/Function | | Sub/Function | | |
| | |--------------| |--------------| | |
| | | |
| |--------------------------------------| |
| |
| |--------------------------------------| |
| | Form Module | |
| | |--------------| |--------------| | |
| | | Sub/Function | | Sub/Function | | |
| | |--------------| |--------------| | |
| | | |
| |--------------------------------------| |
| |
| |--------------| |--------------| |
| | Sub/Function | | Sub/Function | |
| |--------------| |--------------| |
| |
|--------------------------------------------|[/tt]

For more detailed information search for 'Understanding Scope and Visibility' in Access Help. _________
Rott Paws

...It's not a bug. It's an undocumented feature!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top