I have a site that uses classic ASP and a database in Access 2007 format (.accdb). It is encrypted with a password and is about 300 MiB in file size.
The site works fine by itself but every now and then the database gets corrupted (the error is “unrecognized database format”). It can be fixed easily by opening the database in MS Access, then it will be repaired. Problem is it can take days before I notice the database is corrupted and during that time the site will be useless. Sometimes it takes months between each corruption, other times only a week or two.
What I want is the site to be able to call the “Compact and Repair Database” function itself every now and then (once a day or so) to keep the database in a working condition.
My question is how do I do this, make it repair itself?
I found this article: How do I compact and repair an ACCESS 2007 database by .NET code?
…but I don’t understand how I can make that work for me. I only know classic ASP and Java.
Can anyone write a little isolated ASP code that does this: “open connection to password-protected database”, “repairs the database”, “close the connection”.
The site and database is on a dedicated server which I have full control over so I can implement any solution that exists.
Thank you very much!
I got the answer I needed in another question I asked:
Why can't I use "CompactDatabase" in DAO.DBEngine.36 using VBscript?
The user “HansUp” gave me the following code:
strLckFile = "C:\Access\webforums\foo.laccdb"
strSrcName = "C:\Access\webforums\foo.accdb"
strDstName = "C:\Access\webforums\compacted.accdb"
strBackup = "C:\Access\webforums\foobackup.accdb"
strPassword = "foo"
Set objEngine = CreateObject("DAO.DBEngine.120")
Set objFSO = CreateObject("Scripting.FileSystemObject")
If Not (objFSO.FileExists(strLckFile)) Then
If (objFSO.FileExists(strBackup)) Then
objFSO.DeleteFile strBackup
End If
If (objFSO.FileExists(strDstName)) Then
objFSO.DeleteFile strDstName
End If
objFSO.CopyFile strSrcName, strBackup
End If 'LckFile
(The code got a few extra line breaks when I copied it for some reason, follow the link at the beginning of this post for a little cleaner version of the code if you wish.)
It compresses a Access 2007 database to a Access 2007 database (no format change) AND it also repairs any corruption (inconsistent state) in the database! Just what I was looking for. =)