Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Check for Exclusive Open?
Message
 
 
To
13/01/2003 17:16:28
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Miscellaneous
Thread ID:
00741115
Message ID:
00741272
Views:
20
>Nadya,
>
>You can use
>
>
?used("tablealias") and isexclusive("tablealias")
>
>to check for exclusive use.
>

Unfortunately, this doesn't work. The used and isexclusive functions check the current DS only. If the table opened in other DS or on another computer, we would not know. So, trapping for error is the only way to find out...
>
>>>Marvin,
>>>
>>>Leroy's suggestion will work almost all the time, possibly even all of the time. But it does have a whole in it...someone could theoretically open the file in the short period of time between the function call and your attempt to USE the table.
>>>
>>>The best method is to do what Garret has suggested. You can make a function that does this for you. Something like this:
>>>
>>>
>>>lcOldError = on("error")
>>>llOpenError = .f.
>>>on error llOpenError = .t.
>>>use table1.dbf exclusive in 0
>>>on error &lcOldError
>>>if !llOpenError
>>>   * we opened the file exclusively
>>>else
>>>   * could not exclusively open file
>>>endif
>>>
>>>
>>>>What is the best way to check to see if a table can be open exclusively before opening it exclusively and getting an error?
>>>>
>>>>In this case I want to do an ocassional PACK if there are no other users using the database, but of course use summary excl produces an error if other users are using the db.
>>>>
>>>>Thanks!
>>
>>
>>********************************************************************
>>*  Description.......: UseTble && Opens the passed table
>>*  Calling Samples...: UseTble('TranMstr','WorkFile')
>>*  Parameter List....: tcTable, tcAlias, tcMode, tcTagOrder, tnBufferMode
>>*  Created by........:
>>*  Modified by.......: Nadya Nosonovsky 05/01/2001 11:34:13 AM
>>********************************************************************
>>function UseTble && Opens, but doesn't select a table
>>lparameters tcTable, tcAlias, tcMode, tcTagOrder, tnBufferMode
>>* tcTable - required: name of table to open (with or without full path)
>>* tcAlias - optional: alias name, if different from tcTable
>>* tcMode - optional:  mode in which table should be opened, like shared noupdate
>>* tcTagOrder - optional: Tag name
>>* tnBufferMode - optional: Set buffer mode
>>local lcTableName, lnPos, llDBCPrefix, lcDBCName, lcPath
>>* Check parameters first
>>if vartype(m.tcTable)<>'C' or empty(m.tcTable)
>>* First parameter is required
>>	return .f.
>>endif
>>lcPath = justpath(m.tcTable)
>>lnPos=rat('!',m.tcTable)
>>if m.lnPos>0 && DataBase Name was specified && example: Support!PostOff
>>	lcTableName=addbs(m.lcPath)+juststem(substr(m.tcTable,m.lnPos+1)) && PostOff
>>	lcDBCName=upper(left(m.tcTable,m.lnPos-1)) && DBC Name
>>	llDBCPrefix=.t.
>>else
>>	lcTableName=m.tcTable
>>endif
>>if vartype(m.tcAlias)<>'C' or empty(m.tcAlias)
>>	tcAlias=juststem(m.lcTableName)
>>endif
>>
>>if vartype(m.tcMode)<>'C' or empty(m.tcMode)
>>	tcMode='shared'
>>endif
>>local lcCheckFile
>>if empty(justext(m.lcTableName))
>>	lcCheckFile=forceext(m.lcTableName,"dbf")
>>else
>>	lcCheckFile=m.lcTableName
>>endif
>>if !file(m.lcCheckFile)  && File doesn't exist
>>	=messagebox(m.lcCheckFile +' does not exist!. Can not proceed...',48,'Error')
>>	return .f.
>>endif
>>local lnOldSelect
>>lnOldSelect=select()
>>*!*	if !used(m.tcAlias) or ; && Alias is not already in use or
>>*!*		(m.llDBCPrefix and not juststem(cursorgetprop("DATABASE",m.tcAlias))==m.lcDBCName) or ; && it's not the same database
>>*!*		(!empty(m.lcPath) and m.lcPath<>justpath(dbf(m.tcAlias)))
>>local lcOldError, lnErr, lcOldSetExclusive
>>use in select(m.tcAlias) && Close table && Nadya Nosonovsky 11/20/2001 02:58:33 PM allways
>>lnErr=0
>>lcOldError=on('error') && Save current Error Handler
>>lcOldSetExclusive=set('exclusive') && Save current exclusive status
>>set exclusive off && This should be always set this way
>>on error lnErr=error()
>>use (m.tcTable) again &tcMode in 0 alias (m.tcAlias)
>>on error &lcOldError && Restore previous Error Handler
>>if m.lcOldSetExclusive='ON'
>>	set exclusive on && Restore it back to original settings
>>endif
>>do case
>>case m.lnErr=0 && Everything is OK
>>case m.lnErr=1705  && File Access is denied
>>	=messagebox(m.tcTable +' is opened by another user. Can not proceed...',48,'Error '+transform(m.lnErr))
>>	return .f. && Table could not be opened (already used exclusively)
>>case m.lnErr = 1707 && Structural CDX is not found
>>     retry	
>>otherwise && Another error
>>	=messagebox(m.tcTable+' could not be opened!',48,'Error '+transform(m.lnErr))
>>	return .f.
>>endcase
>>select (m.tcAlias) && If it's here, everything is all right
>>if not empty(m.tcTagOrder) and vartype(m.tcTagOrder)='C' and tagno(m.tcTagOrder)>0
>>	set order to tag (m.tcTagOrder) in (m.tcAlias)
>>endif
>>if vartype(m.tnBufferMode)='N' and between(m.tnBufferMode,1,5)
>>	set multilocks on
>>	=cursorsetprop('Buffering',m.tnBufferMode) && Note, we can not set buffering mode less than 3 on a view - no check for this
>>endif
>>select (m.lnOldSelect) && Return back to the original area
>>return .t.
>>
>>BTW, there is a catch with USE tablename exclusive in 0 again. If you use keyword AGAIN, you may end up having table opened, but not exclusively, if it was opened before in shared mode.
If it's not broken, fix it until it is.


My Blog
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform