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

Crazy USED() 3

Status
Not open for further replies.

FoxWing

Programmer
Dec 20, 2004
44
GB
Hi,

Usually, I use the USED() command to detect any opened table. Normally, i works fine until this new project. When a tbale was open exclusive


WHAT I DID :
1) issued "USE 'c:\VisDev\Figures\Data\' + 'us_user'" from another Foxpro command window
2) In another foxpro, i run the codes below in a form.


MY CODES:

DBPATH='c:\VisDev\Figures\Data'

tblUs_user = DBPATH + '\' + 'us_user'


IF !USED('us_user') <----- .F.
USE &tblUs_user IN 0 <-------- Resulting File Access Denied until I closed the open table issued in step 1 above.
ENDIF

Please help.
 
The USED() function only functions within a visual foxpro session.

If you also fire up another foxpro this foxpro starts all over and does not have information about the first started foxpro session.
A table used in the first session will not show up in the second session.

Rob.
 
If you want the table used in both sessions be sure to:

- set exclusive off
- use the desired table in both sessions.

Rob.
 
Rob,

u recommend 'use the desired table in both sessions'. How can i achieve this ? I thought i was using the desired session in both lesson by issuing a 'USE'


Thanks
 
What Rob means is:

VFP program (or session) one:
SET EXCLUSIVE OFF
USE mytable


VFP program (or session) two:
SET EXCLUSIVE OFF
USE mytable


The USED('alias') function only tells you if 'alias' is already used in the current session. It cannot detect whether 'alias' is used in any other private datasession of the current vfp program, or any other vfp program (running locally or across a network).

The reason is that USED doesn't actually look at the file: it just looks in the current datasession's table of open files to see if the provided ALIAS is used (regardless of the actual file name on disk... remember you can "USE mytable ALIAS someothername"). If you have to determine if some other VFP program is using 'dbffilename', then explore using a function like:
Code:
FUNCTION CanOpen( tcFileName )
  local lnHnd 
  lnHnd = FOPEN(tcFileName,12)
  if lnHnd>0 && make sure we don't leave it opened
    FCLOSE(lnHnd)
  endif
  RETURN ( lnHnd>0 )
ENDFUNC

Of course, this function leaves a moment between the return and when the file actually can then be opened using USE where it's vulnerable to another computer opening the DBF.

Perhaps better is:
Code:
if CanOpen(myfilename)
  TRY
    USE (myFileName)
  CATCH TO oExc
  ENDTRY
endif
Simply to trap the error if the file is already in use.

- Bill

Get the best answers to your questions -- See FAQ481-4875.
 
Hi FoxWing,

What rob said, was a bit misleading, but in general he's right.

Used() just seeks through the list of aliases you see in the Data Session window (ALT+W,ALT+D). So it won't detect use of that table in any other datasession, application/ foxpro session or even another computer.

Normally it's not important to know, if a table is used somewhere else, as you normally use any table shared. Used() should only help you not opening a table twice in your foxpro/data session, as that also leads to the error "alias already used" - nor more, no less.

If you want to detect if a table is in use exclusively somewhere else you can just try to open it and catch the error, that you don't have access.

Another warning, how little Used() helps:
Code:
use table1 in 0 alias mytable
if !used("table1")
   use table1 in 0  && error!
endif

In VFP9, at least that problem is solved with a new parameter of AUSED():

Code:
AUSED([i]ArrayName[/i] [, [i]nDataSessionNumber[/i] [, [i]cTableName[/i]]])
By specifying cTablename you now can detect if that TABLE is open, and not just anything "pretending" to be that table... Byut that's just a side point and doesn't help you here, but to understand what Used() is for.

There never will be a function detecting if a table is in use somewhere else other than try...catch...finally:

Code:
TRY
  use table1 in 0
CATCH
  ? Error(), Message()
ENDTRY

Bye, Olaf.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top