Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
Solved

# How to fix orphan DBF

Posted on 2014-02-06
Medium Priority
674 Views
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
Question by:krhag28
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• Learn & ask questions
• 5
• 3

LVL 43

Expert Comment

ID: 39838482
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

Expert Comment

ID: 39838514
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

LVL 43

Accepted Solution

pcelba earned 2000 total points
ID: 39838701
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

Expert Comment

ID: 39838907
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

Expert Comment

ID: 39838909
I will test the FREE TABLE command...
0

Expert Comment

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

Author Closing Comment

ID: 39838942
Simple solution, but nevertheless useful!
0

LVL 43

Expert Comment

ID: 39838951
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

Expert Comment

ID: 39839073
Thanx.
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Visual FoxPro (short VFP) is a programming language with itâ€™s own IDE and database, ranking somewhat between Access and VB.NET + SQL Server (Express). Product Description: http://msdn.microsoft.com/en-us/vfoxpro/default.aspx (http://msdâ€¦
Here in this article, you will get a step by step guidance on how to restore an Exchange database to a recovery database. Get a brief on Recovery Database and how it can be used to restore Exchange database in this section!
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastrâ€¦
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signalâ€¦
###### Suggested Courses
Course of the Month8 days, 5 hours left to enroll

#### 610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.