Link to home
Start Free TrialLog in
Avatar of tawandat
tawandat

asked on

VB6 code to backup an Access database

Hi experts.
I've a VB6 application connecting to an Access database, and would like to offer the option to backup the Access database on logging out of the application.
Can anyone help me with VB6 code to backup the Access database (mdb or mde) file, specifying the file name for the backup?
Avatar of Shahid Thaika
Shahid Thaika
Flag of India image

You need to set reference to "Microsoft Jet and Replication Objects 2.x Library" and also add a "Microsoft Common Dialog" control to your project in addition to other references/controls.

Private Sub Command1_Click()
Dim myDatabasePath As String
Dim strFileName As String

myDatabasePath = "C:\MyProject\MyDBFile.mdb" ' path of your original db file

With CommonDialog1
    .Filter = "MS Access DB File | *.mdb"
    .ShowSave
    If Len(.FileName) > 0 Then
        strFileName = .FileName
    Else
        Exit Sub
    End If
End With

On Error GoTo Err
Dim Conn As New JRO.JetEngine
Dim strSource As String
Dim strDest As String

' Ensure file is not read only
SetAttr pFileName, vbNormal
strSource = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & myDatabasePath & ";User ID=;Password=;"
strDest = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strFileName & ";Jet OLEDB:Engine Type=5;"

Conn.CompactDatabase strSource, strDest

Set Conn = Nothing

Exit Sub
Err:
Debug.Print Err.Description
End Sub
Avatar of tawandat
tawandat

ASKER

Thanks eeshahidt.
Could you please explain the pFileName in the SetAttr line. Or is meant to read:
    SetAttr myDatabasePath, vbNormal
in which case if i'm connected to that database wont i get errors on trying to set the attribute?
ASKER CERTIFIED SOLUTION
Avatar of Shahid Thaika
Shahid Thaika
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of Wayne Taylor (webtubbs)
Wayne Taylor (webtubbs)
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Both solutions work and i'll share the points. FileCopy is simple and straightforward but the Compactdatabase offers more convenience.
Thanks a lot to the both of you!