Reply to comment

Check if table is part of DBC

It is easy to find out if a table belongs to the open Database Container (DBC) using INDBC() function. Sometimes it's necessary to check it w/o referring to DBC itself.

The low level file functions (LLFF) can be used to open a table as a file and check its header for backlink to the DBC. The Table Header Record Structure is documented in VFP help under Table File Structure.


Note 1 Presence of the backlink does not guarantee that table is a part of DBC.

This is sample code. Add error handling and adjust to your requirements as necessary.

? IsTableInDBC("C:\myfolder\mytable.dbf")
RETURN
 
FUNCTION IsTableInDBC
LPARAMETERS tcTableName
LOCAL lnOffset, lcBuffer, lcRetVal
* Open table low-level as a text file
lnFh1 = FOPEN(tcTableName)
IF lnFh1 < 0
	lcRetVal = "***CANNOT OPEN TABLE***"
	RETURN lcRetVal
ENDIF
* Make sure that's VFP table
lcBuffer = FREAD(lnFh1, 1)
DO CASE
CASE INLIST(ASC(lcBuffer), 0x03, 0x83, 0xF5)
	* FoxPro (pre VFP) table
	lcRetVal = "***FREE TABLE***"
CASE NOT INLIST(ASC(lcBuffer), 0x30, 0x31, 0x32)
	= FCLOSE(lnFh1)
	* not VFP table
	lcRetVal = "***NOT VFP TABLE***"
	RETURN lcRetVal
OTHERWISE
	* Continue	
ENDCASE
* Position of the first data record - 2 bytes
= FSEEK(lnFh1, 8, 0)
* Offset to the beginning of the DBC name - 263 bytes back
lnOffset = ASC(FREAD(lnFh1,1)) + ASC(FREAD(lnFh1,1)) * 256 - 263
= FSEEK(lnFh1, lnOffset, 0)
* The DBC name or 0x00's if it's free table
lcBuffer = FREAD(lnFh1, 263)
= FCLOSE(lnFh1)
IF ASC(lcBuffer) = 0
	lcRetVal = "***FREE TABLE***"
ELSE
	* table in dbc, return DBC name
	lcRetVal = LEFT( lcBuffer, AT( CHR(0), lcBuffer) - 1)
ENDIF
 
RETURN lcRetVal

Reply

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <java>, <powershell>, <tsql>, <visualfoxpro>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.
  • Web page addresses and e-mail addresses turn into links automatically.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.