主题:备份Access数据库
silencer
[专家分:0] 发布于 2009-02-14 13:41:00
[size=2]代码如下:[/size]
Private Sub backup_Click()
Dim i As String
On Error Resume Next
With cdlog1
.DialogTitle = "数据备份"
.InitDir = App.path
.FileName = "backup.mdb"
.Filter = "(数据库)*.mdb|*.mdb"
.CancelError = True
.ShowSave
i = .FileName
End With
If Right$(App.path, 1) <> "\" Then spath = App.path & "\"
ssource = spath & "db2.mdb"
sdest = i
If Err.Number <> cdlCancel Then
On Error GoTo sjbf_error
If Dir$(i) <> "" Then
s = MsgBox("文件已存在,确认替换它!", vbYesNo + vbQuestion)
If s = 6 Then
FileCopy ssource, sdest
Kill sdest
DBEngine.CompactDatabase ssource, sdest
MsgBox "数据备份成功!", vbInformation
Else
backup_Click
End If
Else
FileCopy ssource, sdest
DBEngine.CompactDatabase ssource, sdest
MsgBox "数据备份成功!", vbInformation
End If
End If
Exit Sub
sjbf_error:
If Err = 70 Then
MsgBox "数据库正在使用,请关闭所有数据窗口,从新开始备份", vbExclamation
Else
MsgBox Err.Description, vbExclamation
End If
End Sub
[b]为什么老是提示"数据库正在使用,请关闭所有数据窗口,从新开始备份",如何解决错误70,谢谢[/b]
回复列表 (共10个回复)
沙发
merry05 [专家分:8920] 发布于 2009-02-16 14:44:00
在NTFS下么?
板凳
silencer [专家分:0] 发布于 2009-02-16 15:58:00
问题已经解决!
结贴!
3 楼
老大徒伤悲 [专家分:29120] 发布于 2009-02-16 18:37:00
是怎么解决的?
能不能说出来供大家参考呢?
4 楼
silencer [专家分:0] 发布于 2009-02-16 20:03:00
我换了一个备份方法
代码如下:
’备份数据库
Dim mfile As String, mfile2 As String
On Error Resume Next
CommonDialog1.Filter = "Access文件(*.mdb)|*.mdb"
CommonDialog1.ShowSave
mfile = App.path & "\db2.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 "备份完毕!"
'恢复数据库
Dim mfile As String, mfile2 As String
On Error Resume Next
CommonDialog1.Filter = "Access文件(*.mdb)|*.mdb"
CommonDialog1.ShowOpen
mfile = CommonDialog1.FileName '得到别处的Access文件的路径
mfile2 = App.path & "\db2.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 "恢复完毕!"
5 楼
老大徒伤悲 [专家分:29120] 发布于 2009-02-17 11:26:00
谢谢你与大家共享。
6 楼
silencer [专家分:0] 发布于 2009-02-17 13:19:00
大家互相学习!
7 楼
zxyu000 [专家分:0] 发布于 2009-03-07 16:48:00
不错的,学习学习。
8 楼
我是大喊三 [专家分:3010] 发布于 2009-03-07 21:58:00
敢情你自己打开又写了一个?那用SHELL执行COPY命令如何?
9 楼
莫名其妙 [专家分:90] 发布于 2009-03-14 11:35:00
楼主的办法太复杂。用复制文件语句就可以办到的呀
10 楼
silencer [专家分:0] 发布于 2009-03-15 20:14:00
COPY的话,在数据库打开的情况下,是无法执行语句的!!
我来回复