Results 1 to 4 of 4

Thread: compact .mdb from vb

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2001
    Location
    St. Albans, Herts, UK
    Posts
    259
    One small point. If the database is password protected
    VB Code:
    1. 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

  2. #2
    Addicted Member
    Join Date
    Dec 2001
    Posts
    142

    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. "

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2001
    Location
    St. Albans, Herts, UK
    Posts
    259
    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:
    1. Private Sub cmdDAOCompact_Click()
    2.     Dim dbE As Object
    3.     Dim dbDAO As Object
    4.     Dim prop As Object
    5.     Dim fAccess97 As Boolean
    6.     Dim fRepair As Boolean
    7.     Dim strSource As String
    8.     Dim strPassword As String
    9.     Dim strDestDB As String
    10.     Dim iFileStart As Integer
    11.  
    12.    
    13.     'locate database file
    14.     With cdlgFile
    15.         .DialogTitle = "Select Database to Compact"
    16.         .Filter = "Access 97/2000 (*.mdb)|*.mdb"
    17.         .CancelError = False
    18.         .InitDir = "C:\My Documents"
    19.         .ShowOpen
    20.     End With
    21.    
    22.     strSource = cdlgFile.FileName
    23.     If strSource = "" Then
    24.         MsgBox "Cancel clicked"
    25.         Exit Sub
    26.     End If
    27.    
    28.     'create DBEngine version 3.6, to open database and find version
    29.     Set dbE = CreateObject("DAO.DBEngine.36")
    30.    
    31.     strPassword = InputBox("Enter password for this database. Leave blank if no password required")
    32.     'open to get jet version
    33.     Set dbDAO = dbE.OpenDatabase(strSource, True, False, ";pwd=" & strPassword)
    34.    
    35.     'loop through properties
    36.     For Each prop In dbDAO.Properties
    37.         If prop.Name = "Version" Then
    38.             If prop.Value = "3.0" Then
    39.                 fAccess97 = True
    40.             Else
    41.                 fAccess97 = False
    42.             End If
    43.             Exit For
    44.         End If
    45.     Next prop
    46.    
    47.     'close database ready for compacting
    48.     dbDAO.Close
    49.     Set dbDAO = Nothing
    50.    
    51.     If fAccess97 Then
    52.         'check if user wants to repair first
    53.         If MsgBox("Access 97 Databases can be repaired separately from the .Compact method." & vbCrLf & "Do you want to repair first?", vbQuestion + vbYesNo) = vbYes Then
    54.             fRepair = True
    55.         End If
    56.         'need to use DBengine 3.5 so close version 3.6
    57.         Set dbE = Nothing
    58.         Set dbE = CreateObject("DAO.DBEngine.35")
    59.     End If
    60.    
    61.    
    62.  
    63.     'get path/file name of source, add "Backup of " to file name for temporary filename
    64.     iFileStart = InStrRev(strSource, "\", , vbTextCompare)
    65.     strDestDB = Mid$(strSource, 1, iFileStart) & "Backup of " & Mid$(strSource, iFileStart + 1)
    66.    
    67.     On Error GoTo CompactErr
    68.     If fRepair Then
    69.         dbE.RepairDatabase strSource
    70.     End If
    71.     If Len(strPassword) > 0 Then
    72.         dbE.CompactDatabase strSource, strDestDB, , , ";pwd=" & strPassword
    73.     Else
    74.         dbE.CompactDatabase strSource, strDestDB
    75.     End If
    76.     Kill strSource
    77.  
    78.     DoEvents
    79.    
    80.     'rename to original filename
    81.     Name strDestDB As strSource
    82.  
    83.    
    84.     MsgBox strSource & vbCrLf & "Repaired and Compacted Successfully"
    85.    
    86.     Exit Sub
    87.    
    88. CompactErr:
    89.     MsgBox strSource & vbCrLf & "Compaction error:" & vbCrLf & dbE.Errors(0).Number & " " & dbE.Errors(0).Description
    90. 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

  4. #4
    Addicted Member
    Join Date
    Dec 2001
    Posts
    142

    Talking 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
  •  



Click Here to Expand Forum to Full Width