Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Repair DBF File corrupted by power failer

0.00/5 (No votes)
21 Aug 2007 1  
Repair the DBF file which corrupted by any cause.
Screenshot - Screen2.jpg

Introduction

Repair DBF files corrupted by any cause, like Power failer, or abnormally PC shutdown

Background

Usually when power fails and that time the DBF file is in use, the DBF file corrupts and when you try to open the file, It give the message Not a Database file.

Using the code

Before you start add OpenFileDialog component and Activex Data Component References to your project. and assign below Local variables at Form Level.

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

In OpenDialog Open Click event write below code. The code will first check whether the DBF file is corrupted Or Not. If it is corrupted then Repair DBF Button will set to True. Otherwise "No Error in Database" message displays on screen.

Private Sub cmdOpen_Click()
  DialogBox.DialogTitle = "Select DBF File"
  DialogBox.Filter = "DBF Files|*.dbf"
  DialogBox.DefaultExt = "dbf"
  
  DialogBox.ShowOpen
  If Trim(DialogBox.FileName) <> "" Then
    txtFileName.Text = DialogBox.FileName
    Dim objStream As New ADODB.Stream

    objStream.Type = adTypeBinary
    objStream.Open

    objStream.LoadFromFile Trim(DialogBox.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.Visible = True
      Label2.Caption = "Error Found... Click on Repair Database"
    Else
      'cmdRepair.Enabled = False
      'cmdRepair.Caption = "No Error in Database"
      Label2.Caption = "No Error in Database"
    End If
    Set objStream = Nothing
  End If
'showerror:
'  MsgBox "Error Description : " & Err.Description & Chr(13) & _
'         "Error Number : " & Err.Number
End Sub

Copy below code in Repair Click Event.

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(DialogBox.FileName), adSaveCreateOverWrite
  objStream.Close
  cmdRepair.Visible = False
  Set objStream = Nothing
  Label2.Caption = "Repair Succesfully Completed"
End Sub

Summary

Very usefull Utility to recover your Data at any moment.

About Bhaskar Shetty

BCA (Bachelor of computer application) more than 8+ years exprience in Software Development / Analyst / Implementation

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here