Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
USE EXCLU in multiuser system?
Message
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Miscellaneous
Thread ID:
00294535
Message ID:
00294885
Views:
29
>What's the best technique for getting exclusive USE on tables in a multi-user environment? I can't seem to trap Error no. 3 ("File in use" after a USE EXCLU call) in a form. Is it untrappable?
>
>I want exclusive use for REINDEX and ZAP calls.
>
>TIA,

Someone gave me help on this that worked great and I will pass to you.

First create a waserror.prg that can be added to your project code below:

* WasError.PRG
* Example: oCheck = NEWOBJECT('WasError','WasError.PRG')
* IsSuccess = oCheck.CheckCommand('USE MyTable EXCLUSIVE')
* oCheck.Release()
*
* Class to determine if a command results in an error
DEFINE CLASS WasError AS Relation
NoError = .T. && Flag when there's an Error.
ErrorNumber = 0
ErrorMessage = ''

PROCEDURE CheckCommand
LPARAMETERS Command
THIS.NoError = .T.
&Command.
RETURN THIS.NoError
ENDPROC

PROCEDURE Error
LPARAMETERS nError, cMethod, nLine
DEBUGOUT STR(nError) + " " + MESSAGE()
WITH This
.NoError = .F.
.ErrorNumber = m.nError
.ErrorMessage = MESSAGE()
ENDWITH
RETURN
ENDPROC

PROCEDURE Release
RELEASE THIS
ENDPROC

ENDDEFINE


I have a method on my base form class called IsTableUsed that looks like this:

* This method will be passed a parameter of a full path to
* a table that we want to check if the table is opened somewhere else
* this is accomplished by trying to open the table exclusive
*
* Input fullpath to the table needing tested (extension dbf or not)
* Return 1 if the table is being used
* 2 if the table is free to use
* 3 if the file cant be worked with
*
LPARAMETERS tcFullFileName
LOCAL lcFileToTest
* Did the file passed have a 'DBF' extension
IF EMPTY(JUSTEXT(tcFullFileName))
* No extension so lets add dbf to it
lcFileToTest = ALLTRIM(tcFullFileName)+'.dbf'
ELSE
IF UPPER(JUSTEXT(tcFullFileName)) # 'DBF'
* We have an extension but it is not a dbf so we cant do anything with it
* We will treat it as bad
RETURN 3
ELSE
* They have sent us a dbf
lcFileToTest = ALLTRIM(tcFullFileName)
ENDIF
ENDIF

IF NOT FILE(lcFileToTest)
* Cant find the file
RETURN 3
ENDIF

* Now we can test for this file
* so lets save a few environment settings
* Save where we were
lnSelectAreaSave = SELECT()

IF NOT EMPTY(DBC())
lcSaveDataBase = DBC()
ELSE
lcSaveDataBase = ''
ENDIF

SELECT 0

* Lets build our command that we can then send to the oCheck.CheckCommand method
LOCAL lcUseCommand, loCheck, lErrorFlag
lcUseCommand = 'USE '+ALLTRIM(lcFileToTest)+' EXCLUSIVE'
loCheck = NEWOBJECT('WasError','WasError.PRG')
lErrorFlag = loCheck.CheckCommand(lcUseCommand)
loCheck.Release()

IF lErrorflag
* We got the table exclusive so lets close it
* and return
USE
lnReturnValue = 2
ELSE
* We could not get the table exclusive
lnReturnValue = 1
ENDIF

SELECT(lnSelectAreaSave)

IF NOT EMPTY(lcSaveDataBase)
SET DATABASE TO (lcSaveDataBase)
ENDIF

RETURN lnReturnValue


I think you can see how the waserror.prg is used. Now to explain what this got me was the ability to trap the error and not have VFP's error message come up. As my own application error handler was interfereing with trying to trap an error. I hope you can understand this code. Believe me it works for me and I hope you can make it work for you.
Bret Hobbs

"We'd have been called juvenile delinquents only our neighborhood couldn't afford a sociologist." Bob Hope
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform