Improve company productivity with a Business Account.Sign Up

x
• Status: Solved
• Priority: Medium
• Security: Public
• Views: 686
• Last Modified:

# How to fix orphan DBF

I want to remove the backlink to the DBC, in order to open the DBF, although it is orphan. The DBF is unknown, and it should be opened programatically, without user intervention.

There is no need to recreate the DBC, only to open the DBF. I have tried the code below, but not successful. Anybody that can give a solultion?

LPARAMETERS cFile
close databases all
cFiel = ForceExt(cFil, 'DBF')
nHnd = FOPEN(cFil, 2)

cStr = Fread(nHnd, 400)
nPos1 = 0
nPos2 = At('.dbc', cStr) + 2
jj = nPos2
for jj=nPos2 to 1 step -1
cTkn = Substr(cStr, jj, 1)
if cTkn = Chr(13)
nPos1 = jj
endif
next
for jj=nPos1 to nPos2
=Fseek(nHnd, jj)
=Fwrite(nHnd, Chr(0))
next
=Fseek(nHnd, 28)
=Fwrite(nHnd, Chr(0))
=Fclose(nHnd)
0
krhag28
Asked:
• 5
• 3
1 Solution

Commented:
The code needs a small fix:
``````LPARAMETERS cFile

close databases all
cFil = ForceExt(cFile, 'DBF')
nHnd = FOPEN(cFil, 2)

cStr = Fread(nHnd, 400)
nPos1 = 0
nPos2 = At('.dbc', cStr) + 2
jj = nPos2
for jj=nPos2 to 1 step -1
cTkn = Substr(cStr, jj, 1)
if cTkn = Chr(13)
nPos1 = jj
endif
next
for jj=nPos1 to nPos2
=Fseek(nHnd, jj)
=Fwrite(nHnd, Chr(0))
next

*-- This code removes MEMO file and CDX indication
*=Fseek(nHnd, 28)
*=Fwrite(nHnd, Chr(0))

=Fclose(nHnd)
``````

BUT be careful!  If user selects non DBF file having DBF extension it will overwrite it...  Above code also does not work for tables having too many columns...

So it would need some additional tuning. I can provide better code later.
0

Commented:
I have tried it on a DBF which has CDX as well as DBC backlinks, and unfortunately the DBF seems to get corrupt, Foxpro reports Not a table...

I attach the table after manipulation with your code (including the last lines).

(txt extension because of upload restrictions)
Accounts.txt
0

Commented:
Yes, this is the case I've mentioned above... Fortunately, the fix is easy. Open the table in Hex editor and write 30h to the first byte. The Hex editor is a part of VFP tools:

DO HOME()+'\tools\hexedit\hexedit.app'

=====================  SOLUTION to the question ==============================
To remove the database reference from DBF table is also very easy because VFP offers command to do this work:

FREE TABLE d:\path\table.DBF

I should look at the help earlier...

Remember the free table cannot support database stuff like long column names, triggers, field validations etc. You should copy the DBF file (+FPT and CDX) to some temp folder as the first step.
0

Commented:
Thanks for your comments. However, what I need is code to remove references completely without user intervention - using hex editor is not an option in this case.
0

Commented:
I will test the FREE TABLE command...
0

Commented:
Free table seems to work fine :)  so it was more simple than expected.
0

Author Commented:
Simple solution, but nevertheless useful!
0

Commented:
FREE TABLE should work but I'll post the updated code as I promised. The biggest problem of the code is it does not read the table structure but just the first 400 bytes so if you select any table having approx. 13 or more columns it must corrupt the table header...

The Hex editor was not the solution for your users but for you only.
0

Commented:
Thanx.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

## Featured Post

• 5
• 3
Tackle projects and never again get stuck behind a technical roadblock.