VB 数据备份、还原

来源:互联网 发布:教育云数据什么意思 编辑:程序博客网 时间:2024/05/16 08:25

新建一个窗口,添加2个Command控件,1个Text控件,1个Drive控件,1个File,进行数据的备份和还原,代码如下:

Private Sub Command1_Click()
    msg = MsgBox("是否确定需要备份?", 1, "数据备份")
 Text1.Visible = True
 Drive1.Visible = True
  Text1.Text = "备份至"
End Sub

Private Sub Command2_Click()
    msg = MsgBox("数据还原会复盖电脑里面的数据,是否继续?", 4, "数据还原")
 Text1.Visible = True
 File1.Visible = True
 Drive1.Visible = True
  Text1.Text = "数据来自"
End Sub

Private Sub Drive1_Change()
 Dim s As String
      '备份数据库
If Text1.Text = "备份至" Then
      s = Drive1 & "/db1.mdb"
  On Error GoTo errprompt
  Me.MousePointer = 11
  FileCopy App.Path & "/db1.mdb", s
  Text1.Visible = False
  Drive1.Visible = False
  Me.MousePointer = 0
  MsgBox "数据已备份完毕!"
End If
 
If Text1.Text = "数据来自" Then
  File1.Path = Drive1.Drive         '改变驱动盘后文件跟着改变显示,这句非常重要。
     s = Drive1 & File1
        '还原数据库
  On Error GoTo errprompt
  Me.MousePointer = 11
FileCopy s, App.Path & "/db1.mdb"
 Text1.Visible = False
 Drive1.Visible = False
 File1.Visible = False
  Me.MousePointer = 0
  MsgBox "数据已还原完毕!"
End If

errprompt:
    Me.MousePointer = 0
  Select Case Err.Number
    Case 57
      MsgBox "磁盘已坏!", vbCritical
    Case 70
      MsgBox "磁盘写保护,重新开始!", vbCritical
      Unload Me
    Case 71
      MsgBox "磁盘未准备好!", vbCritical
  End Select

End Sub

原创粉丝点击