Jalaj

January 8, 2007

Not a Database File Error in DBF

Filed under: Application, Database, File Formats, FoxPro, Tweaking, Visual Basic — Jalaj @ 10:49 am

Low Level Handling of DBF Files is one of the most viewed post here. While I mentioned recovering database there, that was not taken there and the post turned into getting Database information. I also had intention of showing the database records too along with the other file formats, that will be taken later.

This post indends to address the “Not a Database File” error which database files ocassionally show up when accessing them. Your database file turned to “Not a Database File”. Now what? Restore your last backup to find that it was a month old and you would loose a lot of data if reverted!

This error simply occurs, if you can recall, when improper shutdown of the application occurs for any reason, while the FoxPro was writing to the file. The application though changed the number of records in the header of DBF file, failed to add the record to it, and forced to close (may be power down or so). Now the next time FoxPro accesses the file it finds number of records in header to be more than those actually in database and shows up this unexpected result “Not a Database File”.

How to rectify it? You can simply do it if change the header to show number of records equal to or less than those physically attached with DBF file currently.

Create a VB Project, add two command buttons named cmdOpen and cmdRepair (initially disabled) and a MS-Common Dialog Control named dlgOpen. Add reference to “Microsoft ActiveX Data Objects 2.6 Library”

dbferror.jpg

Dim byteArray As Variant
Dim dblNoOfRecords As Double
Dim dblHeaderLength As Double
Dim dblRecordLength As Double
Dim dblActualRecords As Double

Since our sole intention is to check for correctness of “No of Records” in the header, the same is done in function that fires when “Open” button is clicked. Accordingly the caption in repair button is changed to reflect the same and enabled / disabled as required. The Actual number of records is calculated by subtracting header size from the file size and the dividing it with the record length.

Private Sub cmdOpen_Click()

    dlgOpen.DialogTitle = "Select DBF file"
    dlgOpen.Filter = "DBF FIles|*.dbf"
    dlgOpen.DefaultExt = "dbf"

    dlgOpen.ShowOpen
    If Trim(dlgOpen.FileName) <> "" Then

        Dim objStream As New ADODB.Stream

        objStream.Type = adTypeBinary
        objStream.Open

        objStream.LoadFromFile Trim(dlgOpen.FileName)
        byteArray = objStream.Read()

        dblNoOfRecords = byteArray(4) + byteArray(5) * 256 + byteArray(6) * 256 ^ 2 + byteArray(7) * 256 ^ 3
        dblHeaderLength = byteArray( 8) + byteArray(9) * 256
        dblRecordLength = byteArray(10) + byteArray(11) * 256

        dblActualRecords = Int((objStream.Size - dblHeaderLength) / dblRecordLength)

        If dblNoOfRecords > dblActualRecords Then
            cmdRepair.Enabled = True
            cmdRepair.Caption = “Repair the Database”
        Else
            cmdRepair.Enabled = False
            cmdRepair.Caption = “No Error in Database”
        End If

        Set objStream = Nothing

    End If

End Sub

In case any error is encountered the no of records in the header is simply required to be changed as per no of records physically attached, for which we will modify the byteArray that was read. The byteArray after making modification is overwritten on the file to rectify the error “Not a Database File”

Private Sub cmdRepair_Click()

    Dim objStream As New ADODB.Stream

    objStream.Type = adTypeBinary
    objStream.Open

    byteArray(4) = dblActualRecords Mod 256
    byteArray(5) = Int(dblActualRecords / 256) Mod 256
    byteArray(6) = Int(dblActualRecords / 256 ^ 2) Mod 256
    byteArray(7) = Int(dblActualRecords / 256 ^ 3) Mod 256

    objStream.Write byteArray
    objStream.SaveToFile Trim(dlgOpen.FileName), adSaveCreateOverWrite
    objStream.Close

    cmdRepair.Enabled = False
    cmdRepair.Caption = "Repair the Database"

    Set objStream = Nothing

End Sub

While the process mentioned here only rectifies “Not a Database File” error, there are other errors too which can make the database useless. One of such error is encountered when memo file is missing. The database fails to open, and the way out is that you should place the memo file (.dbt for dBASE and .fpt for FoxPro) from old backups in the folder where the DBF file is kept. We will get to it in more detail sometime later.

2 Comments »

  1. Please send me solution.

    Naushad

    Comment by Naushad Ali — April 28, 2007 @ 11:47 am

  2. Thanks you very much for posting this - it is great - i have looked thru many sites and tried this one - wanted to repair the .dbf without having to download software. Unfortunately it shows that the file in question does not need to be repaired - so I will go back to the drawing board. But will save this for future reference.

    Comment by ann — January 11, 2008 @ 1:13 am

RSS feed for comments on this post.

Leave a comment

Blog at WordPress.com.