user225269 user225269 - 1 year ago 137 Question

How to backup ms access database in

How to backup ms access database in
We're gonna make a system for a certain company but our teacher doesn't teach us at all, please help. Any idea on how to do it?

Answer Source

The simplest way to make a backup of an Access-Database (or any one-file-databases) is to simply copy the file. But watch out for Exception because the file might be locked.

System.IO.File.Copy( _
    "C:\Your\original\database.mdb", _
    String.Format("D:\BackUps\{0:yyyyMMdd}.mdb", Date.Today) _

Additionally you could allow the user to specify the location and filename to which the file should be copied to. That's a rather trivial approach with simply utilizing the System.Windows.Forms controls OpenFileDialog and SaveFileDialog.

Using openDialog As New OpenFileDialog()
    openDialog.CheckFileExists = True
    openDialog.CheckPathExists = True
    openDialog.Filter = "Microsoft Access Database (*.mdb)|*.mdb"
    openDialog.RestoreDirectory = True

    Using saveDialog As New SaveFileDialog()
        saveDialog.CheckFileExists = False
        saveDialog.CheckPathExists = True
        saveDialog.FileName = Date.Now.ToString("yyyyMMdd") & ".mdb"
        saveDialog.Filter = "Microsoft Access Database (*.mdb)|*.mdb"
        saveDialog.RestoreDirectory = True

        If openDialog.ShowDialog() = Windows.Forms.DialogResult.OK AndAlso saveDialog.ShowDialog() = Windows.Forms.DialogResult.OK Then
            If File.Exists(openDialog.FileName) Then
                File.Copy(openDialog.FileName, saveDialog.FileName)
            End If
        End If
    End Using
End Using

In case you wonder what those RestoreDirectory property is doing: Those two dialogs are moving the current directory of the application to the designated paths, which might yield interesting effects later on if you assume a still unchanged current directory. To prevent this behavior, we set this property.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download