Openning a Table with Missing Memo(FPT) File

A table with missing FPT file can be opened if memo file flag in the header of the table is set to 0. All but memo (general, blob) fields can be accessed after that. An attempt to access a memo field will generate an error.


CLEAR 
CLOSE DATABASES ALL
 
LOCAL oExp AS Exception
 
lcTableName = SYS(2023) + "\temp.dbf"
 
CREATE TABLE (lcTableName) ( c1 C(10), m2 M)
INSERT INTO (lcTableName) VALUES ( "A", "A")
INSERT INTO (lcTableName) VALUES ( "B", "B")
INSERT INTO (lcTableName) VALUES ( "C", "C")
INDEX ON c1 TAG c1
USE
 
ERASE( FORCEEXT(lcTableName, "FPT"))
 
TRY
 USE (lcTableName)
 USE
CATCH TO oExp 
 ? oExp.ErrorNo, oExp.Message
ENDTRY
* You've to pass the full table name including extension 
? RemoveFptFlag (lcTableName)
 
* Now try to open table again
USE (lcTableName) EXCLUSIVE 
* Only none-memo fields can be accessed
? c1
TRY
 ? m2
CATCH TO oExp 
 ? oExp.ErrorNo, oExp.Message
ENDTRY
 
RETURN 
*-------------------------------
 
FUNCTION RemoveFptFlag 
LPARAMETERS tcTableFullName
LOCAL lcBuffer, lcRetVal, lnFptFlag
 
* 0x02 - table has a Memo field 
lnFptFlag = 0x02
 
* Open table low-level as a text file
lnFh1 = FOPEN(tcTableFullName,12)
IF lnFh1 < 0
  lcRetVal = "***CANNOT OPEN TABLE***"
  RETURN lcRetVal 
ENDIF
 
* Position of the flags byte
= FSEEK(lnFh1, 28, 0)
lcBuffer = FREAD(lnFh1, 1)
IF BITAND(ASC(lcBuffer), lnFptFlag) = 0
  * table doesn't have FPT file
  lcRetVal = "***Table doesn't have FPT file***"
  RETURN lcRetVal 
ENDIF
 
* Write new flag value back
= FSEEK(lnFh1, 28, 0)
FWRITE(lnFh1, CHR(BITAND(ASC(lcBuffer), BITXOR(0xFF, lnFptFlag))))
 
= FCLOSE(lnFh1)
lcRetVal = ""
RETURN lcRetVal

Không có nhận xét nào:

Đăng nhận xét