Copy and compact Access DB

For those of you that use MS Access to provide dynamic content on your websites (or your customer's websites), have you ever had to make a quick change to your database?  What do you usually do?  Ftp the database to your desktop change it then upload it back ... right?  What if you database is several megs in size?  What if you have a steady stream of visitors?  I will show you a couple of scripts that will allow you to copy your live DB to a backup so that you can download the backup and a script that will copy your backup DB to Live.

Here is an usefull script to copy and compact your Access DB

<% Option Explicit %>
<%
'Dimension variables
Dim objJetEngine 'Holds the jet database engine object
Dim objFSO 'Holds the FSO object
Dim strCompactDB 'Holds the destination of the compacted database
Dim strDbPathAndName
Dim strCon

strDbPathAndName = Server.MapPath("database.mdb")
strCon = "Provider=Microsoft.Jet.OLEDB.4.0; Data source=" & strDbPathAndName
%>
<html>
<head>
<title>Compact And Repair Access Database</title>
</head>
<body>
<h2>Compact And Repair Access Database</h2>
<br />
<%
'If this is a post back run the compact and repair
If Request.Form("postBack") Then %>
<table width="80%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>
<ol>
<%

'Create an intence of the FSO object
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")

'Back up the database
objFSO.CopyFile strDbPathAndName, Replace(strDbPathAndName, ".mdb", "-backup.mdb", 1, -1, 1)

Response.Write(" <li>Database backed up to:-<br/><span>" & Replace(strDbPathAndName, ".mdb", "-backup.mdb", 1, -1, 1) & "</span><br /><br /></li>")



'Create an intence of the JET engine object
Set objJetEngine = Server.CreateObject("JRO.JetEngine")

'Get the destination and name of the compacted database
strCompactDB = Replace(strDbPathAndName, ".mdb", "-tmp.mdb", 1, -1, 1)

'Compact database
objJetEngine.CompactDatabase strCon, "Provider=Microsoft.Jet.OLEDB.4.0; Data source=" & strCompactDB

'Display text that new compact db is created
Response.Write(" <li>New compacted database:-<br/><span>" & strCompactDB & "</span><br /><br /></li>")

'Release Jet object
Set objJetEngine = Nothing




'Delete old database
objFSO.DeleteFile strDbPathAndName

'Display text that that old db is deleted
Response.Write(" <li>Old uncompacted database deleted:-<br/><span>" & strDbPathAndName & "</span><br /><br /></li>")



'Rename temporary database to old name
objFSO.MoveFile strCompactDB, strDbPathAndName

'Display text that that old db is deleted
Response.Write(" <li>Rename compacted database from:-<br/><span>" & strCompactDB & "</span><br />To:-<br /><span>" & strDbPathAndName & "</span><br /><br /></li>")


'Release FSO object
Set objFSO = Nothing


Response.Write(" The Access database is now compacted and repaired")

%></ol></td>
</tr>
</table>
<%
Else

%>
<p> Please note: If the 'Compact and Repair' procedure fails a backup of your database will be created ending with '-backup.mdb'.<br />
</p>
</div>
<form action="this_file_name.asp" method="post" name="frmCompact" id="frmCompact">
<div align="center"><br />
<Input name="postBack" Type="hidden" id="postBack" value="true">
<Input Type="submit" name="Submit" value="Compact and Repair Database">
</div>
</form><%

End If

%>
<br />
</body>
</html>

 

But this does a compact and repair plus a backup. I posted this on the forum here

Someone try it and see what ya think

Thanks for reading

Comments

Errors?

February 24, 2008 by student 101

Errors:

Database already exists line 46.

Otherwise nice idea. 

You must me logged in to write a comment.