为什么提示vb 实时错误‘91’对象变量或 With 块变量没有设置?

Public Function viewData(ByVal txtSQL As String, dataGridAll As DataGrid) As Integer
Dim rstData As ADODB.Recordset
Dim result As String
'检索需要的信息
result = ExecuteSQL(txtSQL, rstData, False)
'设置datagrid的数据源
If rstData.RecordCount <> 0 Then
Set dataGridAll.DataSource = rstData
viewData = rstData.RecordCount
Else
MsgBox "还没有数据!", vbOKOnly + vbExclamation, "警告"
viewData = 0
End If
End Function
数据库采用sql2000
'函数名:ExecuteSQL
'说 明:执行SQL语句
'参 数:SQL As String, rst As ADODB.Recordset, Optional enableWrite As Boolean
'返回值:SQL语句执行成功——true,失败——false
' ******************************************************************************
Public Function ExecuteSQL(ByVal SQL As String, rst As ADODB.Recordset, _
Optional enableWrite As Boolean = True) As Boolean
Dim con As New ADODB.Connection
Dim sTokens() As String
On Error GoTo Execute_Error
sTokens = Split(SQL)
Set con = New ADODB.Connection
con.Open ConnectString
Set rst = New ADODB.Recordset
If enableWrite Then
rst.Open Trim$(SQL), con, adOpenStatic, adLockOptimistic
Else
rst.Open Trim$(SQL), con, adOpenStatic, adLockReadOnly
End If
ExecuteSQL = True
Exit Function
Execute_Error:
ExecuteSQL = False
Exit Function
End Function

忽然笑
浏览 231回答 1
1回答

四季花海

ExecuteSQL 这个函数贴出来,估计是数据库没链接上Public Function viewData(ByVal txtSQL As String, dataGridAll As DataGrid) As Integer&nbsp;Dim rstData As ADODB.Recordset&nbsp;Dim result As String&nbsp;'检索需要的信息&nbsp;result = ExecuteSQL(txtSQL, rstData, False)&nbsp;if result=true then'设置datagrid的数据源&nbsp;If rstData.RecordCount <> 0 Then&nbsp;Set dataGridAll.DataSource = rstData&nbsp;viewData = rstData.RecordCount&nbsp;Else&nbsp;MsgBox "还没有数据!", vbOKOnly + vbExclamation, "警告"&nbsp;viewData = 0&nbsp;End If&nbsp;elsemsgbox "数据库链接失败"end ifEnd Function
打开App,查看更多内容
随时随地看视频慕课网APP