|
|
Title | Use DAO to compact an Access database |
Description | This example shows how to use DAO to compact an Access database in Visual Basic 6. |
Keywords | DAO, compact, database, Access |
Categories | Database |
|
|
When you click the Make Values button, the program adds a bunch of records to a database. When you click Delete Values, it deletes them. At this point, the database contains a lot of unused space.
When you click the Compact button, the program uses DAO's DBEngine object to compact the database and copy it into a temporary file. It then deletes the original database and renames the temporary file to the original database's name.
|
|
Private Sub cmdMakeValues_Click()
Dim db As DAO.Database
Dim num_values As Long
Dim i As Long
Dim txt As String
num_values = CLng(txtNumValues.Text)
Set db = DBEngine(0).OpenDatabase(txtDatabase.Text)
txt = "'" & String$(254, "x") & "'"
For i = 1 To num_values
db.Execute "INSERT INTO ValueTable VALUES(" & i & _
", " & txt & ")"
If i Mod 10 = 0 Then Me.Caption = i
Next i
db.Close
MsgBox "File size: " & FileLen(txtDatabase.Text)
End Sub
Private Sub cmdDeleteValues_Click()
Dim db As DAO.Database
Set db = DBEngine(0).OpenDatabase(txtDatabase.Text)
db.Execute "DELETE FROM ValueTable"
db.Close
MsgBox "File size: " & FileLen(txtDatabase.Text)
End Sub
Private Sub cmdCompact_Click()
Dim db_name As String
Dim temp_name As String
db_name = txtDatabase.Text
temp_name = db_name & ".temp"
DAO.DBEngine.CompactDatabase db_name, temp_name
Kill db_name
Name temp_name As db_name
MsgBox "File size: " & FileLen(db_name)
End Sub
|
|
|
|
|
|