'因为ADO2.0没有压缩数据库的功能,(ADO2.1能实现这个功能,但仅为了能压缩数据库而去打个补丁,好象不值得) 所以选用DAO来实现。
'压缩数据库的函数
Public Function CompactMdbFile(DbFile As String, Optional PassWord As String) As Boolean
Dim TempFile As String, i As Integer
'------------------------- 获取临时文件名 ---------------------------------
Do
TempFile = "c:\temp" + Trim(Str(i)) + ".mdb"
i = i + 1
Loop Until Dir(TempFile, vbHidden) = ""
'------------------------- 压缩数据库 ------------------------------------
On Error GoTo 10
If PassWord = "" Then
DBEngine.CompactDatabase DbFile, TempFile
Else
DBEngine.CompactDatabase DbFile, TempFile, , , ";pwd=" + PassWord
End If
'------------------------ 删除源文件,用压缩后的文件代替原文件 ------------
'其实ACCESS中的压缩数据库也是通过这种方式来实现的。
Kill DbFile
Name TempFile As DbFile
'----------------- 从数据库中删除表 ----------------------------------------------
'打开数据库
Set Db = DBEngine.OpenDatabase("c:\export.mdb", False, False, "ms access;pwd=1234567")
'删除表
Db.TableDefs.Delete "data"
'关闭数据库
Db.Close
'---------------- 压缩数据库 ----------------------------------------------------------
If CompactMdbFile("c:\export.mdb", "1234567") = False Then
MsgBox "Export.mdb 正在被其它程序打开,无法彻底删除该数据中的内容!", vbCritical
End If
The CompactDatabase method syntax has these parts.
Part Description
olddb A String that identifies an existing, closed database. It can be a full path and file name, such as "C:\db1.mdb". If the file name has an extension, you must specify it. If your network supports it, you can also specify a network path, such as "\\server1\share1\dir1\db1.mdb".
newdb A String that is the file name (and path) of the compacted database that you're creating. You can also specify a network path. You can't use the newdb argument to specify the same database file as olddb.
locale Optional. A Variant that is a string expression that specifies a collating order for creating newdb, as specified in Settings. If you omit this argument, the locale of newdb is the same as olddb.
You can also create a password for newdb by concatenating the password string (starting with ";pwd=") with a constant in the locale argument, like this:
dbLangSpanish & ";pwd=NewPassword"
If you want to use the same locale as olddb (the default value), but specify a new password, simply enter a password string for locale:
";pwd=NewPassword"
options Optional. A constant or combination of constants that indicates one or more options, as specified in Settings. You can combine options by summing the corresponding constants.
password Optional. A Variant that is a string expression containing a password, if the database is password protected. The string ";pwd=" must precede the actual password. If you include a password setting in locale, this setting is ignored.