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

Close single table

Status
Not open for further replies.

DarrenWard

IS-IT--Management
Feb 15, 2002
217
GB
I have several forms that are capable of calling each other.

Some of the forms share the use of tables, other forms use tables that are exclusive to it's self, and other forms use a combination of both.

I open the tables in the FormSet Init event. And use :-
if Used("TableName") = .f.
use "TableToUse" in 0 order ..... alias TableName
endif

When a form closes I am trying to close any tables that are no longer required (given that the form could have been called by another form that requires some tables). So if I recorded what tables were opened for thisform can I just close them? I can only find a way of closing all tables not just one.

Is this the best way to handle the tables? or should I just open all of them at the start and have done with it?

Dazz (Like Harry Potter's schooling, I can't do anything without a spell checker)
 
Dazz,
While I'm not sure that I agree with your philosophy on how you are dealing with table across multiple forms, I can help you with your problem.
Lets say, Form1 opens these tables:

CUSTOMER
INVOICES

And from 2 Opens these tables:
ADDRESSES
PHONES
But also needs
CUSTOMER

So, in form 2, in the FormSetInit, you would then issue:

IF NOT USED('ADDRESSES')
USE ADDRESSES IN 0
ThisForm.CloseAddr = .T.
ENDIF
*
IF NOT USED('PHONES')
USE PHONES IN 0
ThisForm.ClosePhone = .T.
ENDIF

Then, in your 2nd Form's Destroy Event, you can put in:

IF ThisForm.CloseAddr
SELECT ADDRESSES
USE
ENDIF
*
IF ThisForm.ClosePhones
SELECT PHONES
USE
ENDIF

The nice thing about this is, since the "Close" values only get set if this form "Opens" them, then it takes care of closing them. But if you had a 3rd form that "Opened" them as well, and that for is still present and needs them, then IT would have the "Close" variables, and would close them at that point.
Now, all of this is academic, if you are not using Default Data Sessions. That is normally the case, if you are calling child tables from a parent. Use DEFAULT data session, and it "Gets" the tables that were open (and their current record pointers) of the parent it was called from. If you are using "Private" data sessions, all of the forms create their "Own Version" of the table being open. It will also originate the record pointer on the parent table, and position childeren in relation to the parent. (Most of the time, I would reccomend this approach, and then you don't have to worry about it.)
If you design your application to be a bit more robust, your forms will deal with the "Open/Close" tables issues automatically. I write no code to do this programatically. But, my suggestion above should work for you. Just make sure that you add "CloseAddr" and "ClosePhones" (or whatever table names you have, and need to use) as properites to your form, and initialize them to .F. (which is actually their default, so just creating them should cover that requirment.)

Best Regards,
Scott

Please let me know if this has helped [hammer]
 
Wouldn't AutoClose Tables in the DataEnvironment take care of that?
 
Thanx Scott,

I will have a play with your suggestions.

I would not say that this is my 'philosophy' but being very new to VFP it is the only way that I could find of doing what I wanted to achieve.

If you have a suggested better way, I am always happy to learn.

Dazz (Like Harry Potter's schooling, I can't do anything without a spell checker)
 
Also, instead of
Code:
SELECT tablename
USE
you can use the shorter:
Code:
USE IN tablename

I usually protect closes so that if somewhere else closed it you don't get a crash:
Code:
IF Used('tablename')
  USE IN tablename
ENDIF
or the shorter and quicker:
Code:
USE IN (iif(USED('tablename'),'tablename',0))
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top