|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
DBEngine.CompactDatabase Method (DAO)
Время создания: 12.10.2019 20:38
Текстовые метки: vba_access, compact, compress, сжатие
Раздел: Разные закладки - VBA - Access - Compress
Запись: xintrea/mytetra_db_adgaver_new/master/base/1538537806i312c1wi3d/text.html на raw.githubusercontent.com
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
DBEngine.CompactDatabase Method (DAO) Last modified: September 26, 2016 Applies to: Access 2013 | Access 2016 Copies and compacts a closed database, and gives you the option of changing its version, collating order, and encryption. (Microsoft Access workspaces only).
Syntax
expression .CompactDatabase(SrcName, DstName, DstLocale, Options, password) expression An expression that returns a DBEngine object. Parameters
Remarks
You can use one of the following constants for the DstLocale argument to specify the CollatingOrder property for string comparisons of text.
You can use one of the following constants in the options argument to specify whether to encrypt or to decrypt the database while it's compacted.
If you omit an encryption constant or if you include both dbDecrypt and dbEncrypt, DstName will have the same encryption as SrcName. You can use one of the following constants in the options argument to specify the version of the data format for the compacted database. This constant affects only the version of the data format of DstName and doesn't affect the version of any Microsoft Access-defined objects, such as forms and reports.
You can specify only one version constant. If you omit a version constant, DstName will have the same version as SrcName. You can compact DstName only to a version that is the same or later than that of SrcName. As you change data in a database, the database file can become fragmented and use more disk space than is necessary. Periodically, you can use the CompactDatabase method to compact your database to defragment the database file. The compacted database is usually smaller and often runs faster. You can also change the collating order, the encryption, or the version of the data format while you copy and compact the database. You must close SrcName before you compact it. In a multiuser environment, other users can't have SrcName open while you're compacting it. If SrcName isn't closed or isn't available for exclusive use, an error occurs. Because CompactDatabase creates a copy of the database, you must have enough disk space for both the original and the duplicate databases. The compact operation fails if there isn't enough disk space available. The DstName duplicate database doesn't have to be on the same disk as SrcName. After successfully compacting a database, you can delete the SrcName file and rename the compacted DstName file to the original file name. The CompactDatabase method copies all the data and the security permission settings from the database specified by SrcName to the database specified by DstName.
Encrypted linked tables
Encrypted passwords are dependent on the file format of the database that you are using. If you are using an Access 2003 (.mdb) or earlier database, you will have one password to protect the database, and a separate password to encrypt the database. For Access 2007 (.accdb) and later (.mdb) databases, the only option is to encrypt and protect the database with one password, as the option to have two separate passwords has been removed.
You can use the following example VBA code for a command button:
Private Sub Command0_Click() Dim strSourcePath As String Dim strDestPath As String strSourcePath = "<path>\sourceDb.accdb" strDestPath = "<path>\destDb.accdb" DBEngine.CompactDatabase strSourcePath, strDestPath, dbLangGeneral & ";pwd=Access", dbVersion120, ";pwd=Access" Set CurrentDatabase = CurrentDb Set LinkedTableDef = CurrentDatabase.CreateTableDef ("My Linked Table") LinkedTableDef.Connect = "MS Access;pwd=Access";database=" & strDestPath LinkedTableDef.RefreshLink MsgBox "Finished" End Sub
The code sample below shows how to use CompactDatabase with a password (encryption key) and then link to a table in that compacted database. Note that a password must be supplied.
Private Sub CompactAndLink_Click()
Dim strSourcePath As String Dim strDestPath As String Dim strSourceTableName As String Dim strDestTableName As String Dim tdf As TableDef
strSourcePath = "<path>\<database>.accdb" strDestPath = "<path>\<database>.accdb" strSourceTableName = "<table name in destination database>" strDestTableName = "<linked table name>"
' Compact source database into new destination database with encrypted password DBEngine.CompactDatabase strSourcePath, strDestPath, dbLangGeneral & ";pwd=Access", dbVersion120, ";pwd=Access"
' Link to one of the tables in the destination database ' Password must be provided in the Connect property
Set CurrentDatabase = CurrentDb Set tdf = CurrentDatabase.CreateTableDef(strDestTableName)
With tdf .Connect = ";pwd=Access" & ";DATABASE=" & strDestPath .SourceTableName = strSourceTableName End With
CurrentDatabase.TableDefs.Append tdf
MsgBox "Database compacted and encrypted password applied. Link to table also completed."
End Sub
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Так же в этом разделе:
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|