在自己写的小程序中加入了备份和恢复功能后,发现原来运行正常的程序有点小问题了!我想问题应该是出在对数据库连接的关闭和打开上,但是我真的不知道到底出问题在哪里?备份和恢复的代码如下:
Private Sub sjbf_Click()
    Dim bpath, epath As String
    If Dir(App.Path & "\Backup\", vbDirectory) = "" Then
     MkDir App.Path & "\Backup\"
    End If
    If MsgBox("确定要进行数据备份?", vbYesNo + vbQuestion, "提示") = vbYes Then
        Cn.Close
        bpath = "" & App.Path & "\test.mdb"
        epath = "" & App.Path & "\Backup\" & Year(Date) & "-" & Month(Date) & ".mdb"
        FileCopy bpath, epath
        MsgBox "备份完毕  ", vbInformation, "数据备份"
        Cn.Open
    Else
        Exit Sub
    End If
End SubPrivate Sub sjhf_Click()
    With CommonDialog1
            .DialogTitle = "打开"
            .CancelError = False
            .InitDir = "" & App.Path & "\Backup\"
            .Filter = "数据库文件(*.mdb)|*.mdb|All Files(*.*)|*.*"
            .ShowOpen
            If Len(.FileName) = 0 Then
                Exit Sub
            End If
            sfile = .FileName
    End With
    If MsgBox("确定要用备份数据覆盖当前数据吗?", vbYesNo + vbQuestion, "提示") = vbYes Then
        Cn.Close
        FileCopy sfile, App.Path & "\test.mdb"
        MsgBox "恢复完毕  ", vbInformation, "数据恢复"
        Cn.Open
    Else
        Exit Sub
    End If
End Sub
代码当中用了Cn.Close,Cn.Open,按我的理解,这就可以了,可是为什么还有问题呢!

解决方案 »

  1.   

    调试时,错误指示在
    Private Sub Form_Unload(Cancel As Integer)
    rsstudentinfo.Close
    Set rsstudentinfo = Nothing
    rsclassinfo.Close
    Set rsclassinfo = Nothing
    rsstudentclass.Close
    Set rsstudentclass = Nothing
    conn.Close
    Set conn = Nothing
    End Sub
    里的第一句!
      

  2.   

    估计执行这句之前rsstudentinfo已经close
    试试改成:
    If rsstudentinfo.State = adStateOpen Then
       rsstudentinfo.Close
    End If
      

  3.   

    这次运行,发现报的错跟上次一样,可是错误的指示到了
    Private Sub Form_Load()
    If rsstudentinfo.RecordCount > 0 Then
      Set grdstudentinfo.DataSource = rsstudentinfo
    Else
     MsgBox "database error!"
    End If
    End Sub
    中的第一行
    这段代码是里面的一个窗体frmstudentinfo的!
      

  4.   

    '
    '恢复和备份MDB数据库
    '函数名:BakResumeMdb
    '参数:P_Cnn ADODB连接,SourFileName 源文件名,ObjFileName 目标文件名,
    '     Provider Provider参数(视JET版而定,默认是4.0),UserID 用户名,
    '     UserPwd 密码,WorkType 操作类型(0 备份,1 恢复)
    '返回值:TRUE 成功,FALSE 失败.
    '注:当WorkType=0时,源文件名是要备份文件,目标文件名是备份文件.
    '   当WorkType=1时,源文件名是备份文件,目标文件名要恢复的文件.
    Public Function BakResumeMDB(P_Cnn As ADODB.Connection, _
                           SourFileName As String, _
                           ObjFileName As String, _
                           Optional Provider As String = "Microsoft.Jet.OLEDB.4.0", _
                           Optional UserID As String = "admin", _
                           Optional UserPwd As String = "", _
                           Optional WorkType As Long = 0) As Boolean
        
        Dim Yjro As New JRO.JetEngine
        Dim WorkPath As String
        Dim FileCon As New SmFileCls
        
        On Error Resume Next
        '/关闭连接
        P_Cnn.Close: Set P_Cnn = Nothing
        DoEvents
        '/-------------------------------
        '/压缩
        Yjro.CompactDatabase "Provider=" & Provider & SourFileName & ";" & _
                             "Jet OLEDB:Database Password=" & UserPwd & ";" & _
                             "User ID=" & UserID & ";", _
                             "Provider=" & Provider & ";Data Source=" & ObjFileName & ";" & _
                             "Jet OLEDB:Database Password=" & UserPwd & ";" & _
                             "User ID=" & UserID & ";"
        DoEvents
        '/删除旧文件,将压缩后的文件COPY到旧位置
        If Not (FileCon.FileCheck(SourFileName) And FileCon.FileCheck(ObjFileName)) Then
           If WorkType = 0 Then
              '/备份。
              '重新连接
                'Call CreateMdbConn(P_Cnn, SourFileName, , UserID, UserPwd)
           Else
              '/恢复
              '这里加入代的代码,重新连接. 'Call CreateMdbConn(P_Cnn, ObjFileName, , UserID, UserPwd)
           End If
           Err.Number = -1
        End If
        Set FileCon = Nothing
        Set Yjro = Nothing: Err.Clear
        BakResumeMDB = (Err.Number = 0)
    End Function
      

  5.   

    來不級看了,要斷網了,先copy了,明天再說
      

  6.   

    If rsstudentinfo.RecordCount > 0 Then
      Set grdstudentinfo.DataSource = rsstudentinfo
    Else
    这里出错 说明 rsstudentinfo 关闭了, 或者没打开 
    rsstudentinfo是公共对象吗?
      

  7.   


    TO seakingx(抗日统一联盟:亚龙湾) 
     把你的邮件地址发到我的信箱
    [email protected]
    谢谢!
      

  8.   

    我看了, 没什么错误呀, 就是退出的时候提示对象关闭时, 无法使用是frmmain的unload 事件中的代码有问题'这个是你原来的
    Private Sub Form_Unload(Cancel As Integer)
    'rsstudentinfo.Close
    If rsstudentinfo.State = adStateOpen Then
       rsstudentinfo.Close
    End IfSet rsstudentinfo = Nothingrsclassinfo.Close '在这里出错了, 因为rsclassinfo已经关闭了, 不能响应close方法了
    Set rsclassinfo = Nothingrsstudentclass.Close
    Set rsstudentclass = Nothing
    conn.Close
    Set conn = Nothing
    End Sub这个是我改的Private Sub Form_Unload(Cancel As Integer)
    'rsstudentinfo.Close
    If rsstudentinfo.State = adStateOpen Then
       rsstudentinfo.Close
    End IfSet rsstudentinfo = NothingIf rsclassinfo.State = adStateOpen Then
        rsclassinfo.Close
    End If
    Set rsclassinfo = NothingIf rsstudentclass.State = adStateOpen Then
        rsstudentclass.Close
    End If
    Set rsstudentclass = Nothing
    conn.Close
    Set conn = Nothing
    End Sub