|
-
Jan 2nd, 2002, 03:40 PM
#1
Thread Starter
Hyperactive Member
One small point. If the database is password protected
VB Code:
DBEngine.CompactDatabase Location, strTempFile, , , ";pwd=YourPassword"
Graham, www.gab2001uk.com VBExplorer Forum Moderator VBExplorer
www.gab2001uk.com For comparing and contrasting DAO with ADO
Code for Creating, Copying, Compacting, Replicating, Synchronising Access 97/2000 databases plus showing Schemas and using .Seek
-
Jan 2nd, 2002, 03:43 PM
#2
Addicted Member
dbengine`
I checked the code line by line .. and it kicks out to the error handler at the dbengine line.. Do I have to dim the dbengine. .. the intelesense doesnt come up when I do a "dbengine. "
-
Jan 2nd, 2002, 03:46 PM
#3
Thread Starter
Hyperactive Member
Have you added a reference to DAO v 3.51 for Access 97 or 3.6 for 2000 and above?
Alterative method. You do not need to add any references, but you do need a Common Dialog control to locate your file, and rename it cdlgFile
VB Code:
Private Sub cmdDAOCompact_Click()
Dim dbE As Object
Dim dbDAO As Object
Dim prop As Object
Dim fAccess97 As Boolean
Dim fRepair As Boolean
Dim strSource As String
Dim strPassword As String
Dim strDestDB As String
Dim iFileStart As Integer
'locate database file
With cdlgFile
.DialogTitle = "Select Database to Compact"
.Filter = "Access 97/2000 (*.mdb)|*.mdb"
.CancelError = False
.InitDir = "C:\My Documents"
.ShowOpen
End With
strSource = cdlgFile.FileName
If strSource = "" Then
MsgBox "Cancel clicked"
Exit Sub
End If
'create DBEngine version 3.6, to open database and find version
Set dbE = CreateObject("DAO.DBEngine.36")
strPassword = InputBox("Enter password for this database. Leave blank if no password required")
'open to get jet version
Set dbDAO = dbE.OpenDatabase(strSource, True, False, ";pwd=" & strPassword)
'loop through properties
For Each prop In dbDAO.Properties
If prop.Name = "Version" Then
If prop.Value = "3.0" Then
fAccess97 = True
Else
fAccess97 = False
End If
Exit For
End If
Next prop
'close database ready for compacting
dbDAO.Close
Set dbDAO = Nothing
If fAccess97 Then
'check if user wants to repair first
If MsgBox("Access 97 Databases can be repaired separately from the .Compact method." & vbCrLf & "Do you want to repair first?", vbQuestion + vbYesNo) = vbYes Then
fRepair = True
End If
'need to use DBengine 3.5 so close version 3.6
Set dbE = Nothing
Set dbE = CreateObject("DAO.DBEngine.35")
End If
'get path/file name of source, add "Backup of " to file name for temporary filename
iFileStart = InStrRev(strSource, "\", , vbTextCompare)
strDestDB = Mid$(strSource, 1, iFileStart) & "Backup of " & Mid$(strSource, iFileStart + 1)
On Error GoTo CompactErr
If fRepair Then
dbE.RepairDatabase strSource
End If
If Len(strPassword) > 0 Then
dbE.CompactDatabase strSource, strDestDB, , , ";pwd=" & strPassword
Else
dbE.CompactDatabase strSource, strDestDB
End If
Kill strSource
DoEvents
'rename to original filename
Name strDestDB As strSource
MsgBox strSource & vbCrLf & "Repaired and Compacted Successfully"
Exit Sub
CompactErr:
MsgBox strSource & vbCrLf & "Compaction error:" & vbCrLf & dbE.Errors(0).Number & " " & dbE.Errors(0).Description
End Sub
Last edited by gab2001uk; Jan 2nd, 2002 at 03:53 PM.
Graham, www.gab2001uk.com VBExplorer Forum Moderator VBExplorer
www.gab2001uk.com For comparing and contrasting DAO with ADO
Code for Creating, Copying, Compacting, Replicating, Synchronising Access 97/2000 databases plus showing Schemas and using .Seek
-
Jan 2nd, 2002, 03:53 PM
#4
Addicted Member
ah ha
ah ha ....sorry never taught of it .. its been a long day .. thanks
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|