Click here to Skip to main content
15,867,686 members
Articles / Programming Languages / VBScript
Article

Repair DBF File corrupted by power failer

Rate me:
Please Sign up or sign in to vote.
1.67/5 (3 votes)
21 Aug 2007 54.2K   2K   11   7
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


Written By
Web Developer
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
PraiseRepair DBF File corrupted by power failure Pin
rambabujalli21-Sep-22 3:59
rambabujalli21-Sep-22 3:59 
Question.dbf .dbb .nxt Pin
hardi12311-Feb-13 7:16
hardi12311-Feb-13 7:16 
QuestionDBF Repair - Succesful Pin
Satya_d2-Feb-13 19:45
Satya_d2-Feb-13 19:45 
GeneralMy vote of 3 Pin
download only22-Sep-10 18:40
download only22-Sep-10 18:40 
GeneralVirus Detected While Unzipping. Pin
Ronald7724-Nov-09 16:03
Ronald7724-Nov-09 16:03 
GeneralMissing information Pin
Jörgen Sigvardsson21-Aug-07 4:34
Jörgen Sigvardsson21-Aug-07 4:34 
GeneralRe: Missing information Pin
Bhaskar Shetty21-Aug-07 4:59
Bhaskar Shetty21-Aug-07 4:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.