repair and compact db

  • Thread starter Thread starter Alejandra Parra
  • Start date Start date
A

Alejandra Parra

Is it possible to repair and compact an Access data base from VB.NET code???
If it's possible, how do you do it?
 
Alejandra,

I'm not familiar with VB.Net, but I do it through VB 6.0 so I suppose VB.Net
can do it as well. Not sure how similar the two are, but here is some sample
VB 6.0 code in case it helps:

Sub Main()
Dim f, fs
Dim wrkJet As Workspace

pth = "\\ServerName\Path"
nam = "dbName"
ext = ".mdb"
lck = ".ldb"
olddb = pth & "\" & nam & ext
newdb = pth & "\" & nam & " temp" & ext
bakdb = pth & "\" & nam & " bak" & ext
lckdb = pth & "\" & nam & lck

If Dir(lckdb) = "" Then
If Dir(bakdb) <> "" Then Kill bakdb
FileCopy olddb, bakdb
Set fs = CreateObject("Scripting.FileSystemObject")
Set wrkJet = CreateWorkspace("JetWorkspace", "admin", _
"", dbUseJet)
If Dir(newdb) <> "" Then Kill newdb
DBEngine.CompactDatabase olddb, newdb
If Dir(olddb) <> "" Then Kill olddb
Else
msgbox "Database file is open. Operation aborted..."
End If
End Sub

The first step is to check the lock file to make sure the database is not
open. In that case it aborts.
If the check is successful, the database is copied to another file (dbname
bak.mdb) as is, before it is compacted, just in case, then compacted into a
temp db file (dbname temp.mdb), and finally dbname.mdb is deleted and dbname
temp.mdb renamed to dbname.mdb (the intermediate file is the standard
process Access uses when compacting from "within"a database).
I have also some additional lines of code to write to a log file as each
step is completed.

HTH,
Nikos
 
Back
Top