如下程序,在运行到cmd.Execute时出错
'== 标记数据库是否连接 ==
Private IsConnect As Boolean
'== 标记执行Connect()函数后,访问数据库的次数 ==
Private Connect_Num As Integer ''''''2010-11-12 modify by panda
Private cnn As ADODB.Connection '连接数据库的Connection对象
Private rs As ADODB.Recordset '保存结果集的Recordset对象
'连接数据库
Private Sub Connect()
'如果连接标记为真,则返回。否则会出错
If IsConnect = True Then
Exit Sub
End If
'关键New用于创建新对象cnn
Set cnn = New ADODB.Connection
cnn.ConnectionString = ConnString '设置连接字符串ConnectionString属性
'打开到数据库的连接
cnn.Open
'判断连接的状态
If cnn.State <> adStateOpen Then
'如果连接不成功,则显示提示信息,退出程序
MsgBox "数据库连接失败"
End
End If
'设置连接标记,表示已经连接到数据库
IsConnect = True
End Sub
'断开与数据库的连接
Private Sub Disconnect()
Dim Rc As Long
'如果连接标记为假,标明已经断开连接,则直接返回
If IsConnect = False Then
Exit Sub
End If
'关闭连接
cnn.Close
'释放cnn
Set cnn = Nothing
'设置连接标记,表示已经断开与数据库的连接
IsConnect = False
End Sub
'使用Connect_Num控制数据库连接
Public Sub DB_Connect()
Connect_Num = Connect_Num + 1
Connect
End Sub
Public Sub DB_Disconnect()
If Connect_Num >= CONNECT_LOOP_MAX Then
Connect_Num = 0
Disconnect
End If
End Sub
'强制关闭api方式访问的数据库,计数器复位
Public Sub DBapi_Disconnect()
Connect_Num = 0
Disconnect
End Sub
'执行数据库操作语句
Public Sub SQLExt(ByVal TmpSQLstmt As String)
'创建Command对象cmd
Dim cmd As New ADODB.Command
'连接到数据库
DB_Connect
'设置cmd的ActiveConnection属性,指定与其关联的数据库连接
Set cmd.ActiveConnection = cnn
'设置要执行的命令文本
cmd.CommandText = TmpSQLstmt
' MsgBox TmpSQLstmt
'执行命令
cmd.Execute
'清空cmd对象
Set cmd = Nothing
'断开与数据库的连接
DB_Disconnect
End Sub
'执行数据库查询语句
Public Function QueryExt(ByVal TmpSQLstmt As String) As ADODB.Recordset
'创建Recordset对象rst
Dim rst As New ADODB.Recordset
'连接到数据库
DB_Connect
'设置rst的ActiveConnection属性,指定与其关联的数据库连接
Set rst.ActiveConnection = cnn
'设置游标类型
rst.CursorType = adOpenDynamic
'设置锁定类型
rst.LockType = adLockOptimistic
'打开记录集
rst.Open TmpSQLstmt
'返回记录集
Set QueryExt = rst
End Function
能麻烦您给改一下吗?谢谢