Openning a Table with Missing Memo(FPT) File

topic: 

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.

$SAMPLECODE$

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71

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

Comments