在自己写的小程序中加入了备份和恢复功能后,发现原来运行正常的程序有点小问题了!我想问题应该是出在对数据库连接的关闭和打开上,但是我真的不知道到底出问题在哪里?备份和恢复的代码如下:
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,按我的理解,这就可以了,可是为什么还有问题呢!
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,按我的理解,这就可以了,可是为什么还有问题呢!
解决方案 »
- 请教!VB中如何将汉字的ASCII码的高、低8位字节分离出来,分别存在另两个字节中?在线等!
- 小数位数的显示问题
- 请教窗体停留
- 怎么样在treeview的每个nodes.text前,加一个可以打勾的方框?如何实现父节点打勾,全部子节点都打勾?当全部子节点都没有打勾时,父节点
- 请问 怎么限制 在文本框中 只能输入数字?而不能输入字母之类的
- 通过程序怎样获取任意类型文件的Content-Type?
- 编程的基础是什么?
- 一个与VB无关的问题!大家看看,给个意见,帮帮我
- 关于读取ini文件内容的问题,在线等,可行即给分,谢谢
- vb6.0+sql 2005
- 大家帮帮忙,看看怎么改,谢谢~~~~~~~~
- 如何在vb中实现网络文件传输,请高手指点,急?
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
里的第一句!
试试改成:
If rsstudentinfo.State = adStateOpen Then
rsstudentinfo.Close
End If
Private Sub Form_Load()
If rsstudentinfo.RecordCount > 0 Then
Set grdstudentinfo.DataSource = rsstudentinfo
Else
MsgBox "database error!"
End If
End Sub
中的第一行
这段代码是里面的一个窗体frmstudentinfo的!
'恢复和备份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
Set grdstudentinfo.DataSource = rsstudentinfo
Else
这里出错 说明 rsstudentinfo 关闭了, 或者没打开
rsstudentinfo是公共对象吗?
TO seakingx(抗日统一联盟:亚龙湾)
把你的邮件地址发到我的信箱
[email protected]
谢谢!
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