'備份數據庫到指定目錄
Private Sub Command1_Click()
Dim mfile As String, mfile2 As String
On Error Resume Next
CommonDialog1.Filter = "Access文件|*.mdb"
CommonDialog1.ShowSave
mfile = App.Path & "\db1.mdb " '要備份的文件為當前文件夾下的 db1.mdb
mfile2 = CommonDialog1.FileName '得到目標文件的路徑
If Trim(mfile2) = " " Then Exit Sub
If Dir(mfile2) <> " " Then
If MsgBox(Dir(mfile2) & " 文件已經存在,是否替換? ", vbYesNo, "警告 ") = vbNo Then Exit Sub
End If
Dim buff() As Byte, i As Long
i = FileLen(mfile)
ReDim buff(i - 1)
Open mfile For Binary As #1
Get #1, , buff
Close #1
Open mfile2 For Binary As #1
Put #1, , buff
Close #1
MsgBox "備份完畢! "
End Sub
'恢復指定數據庫
Private Sub Command2_Click()
Dim mfile As String, mfile2 As String
On Error Resume Next
CommonDialog1.Filter = "Access文件|*.mdb"
CommonDialog1.ShowOpen
mfile = CommonDialog1.FileName '得到別處的Access文件的路徑
mfile2 = App.Path & "\db1.mdb " '要覆蓋掉當前文件夾下的 db1.mdb
If Trim(mfile) = " " Then Exit Sub
If MsgBox("是否恢復數據庫? ", vbYesNo, "警告 ") = vbNo Then Exit Sub
Dim buff() As Byte, i As Long
i = FileLen(mfile)
ReDim buff(i - 1)
Open mfile For Binary As #1
Get #1, , buff
Close #1
Open mfile2 For Binary As #1
Put #1, , buff
Close #1
MsgBox "恢復完畢! "
End Sub