VB实时错误'91'对象变量或With块变量未设置

来源:百度知道 编辑:UC知道 时间:2024/06/07 14:04:14
Private mCnn As ADODB.Connection
Private mRst As ADODB.Recordset

Private Sub Form_Load()
On Error GoTo ErrorLoad
Dim strCnn As String

Command1.Caption = "查询SQL数据"

strCnn = "UID=sa;PWD=;Driver={SQL Server};Server=dddd;Database=okmd;dsn= ,,Connection=adConnectAsync"
Set mCnn = New ADODB.Connection
mCnn.CursorLocation = adUseClient
mCnn.ConnectionString = strCnn
mCnn.Open '打开数据源

Exit Sub
ErrorLoad:
MsgBox "错误编号:" & Err.Number & vbCrLf & "错误信息:" & Err.Description, vbCritical, "连接错误"
mCnn.ConnectionString = ""
Command1.Enabled = False
End Sub

Private Sub Command1_Click() '问题出在这一段
On Error GoTo ErrorLoad
Dim mStr As String

If mRst.State = adStateOpen Then mRst.Close: Set mRst = Nothing

mStr = "select * from link"
Set mRst = New ADODB.Recordset
mRst.Open mStr,

试试吧:

Private Sub Command1_Click()
On Error GoTo ErrorLoad
Dim mStr As String
Dim mRst As New ADODB.Recordset

If mRst.State = adStateOpen Then mRst.Close: Set mRst = Nothing

mStr = "select * from link"

mRst.Open mStr, mCnn, adOpenKeyset, adLockReadOnly
Set DataGrid1.DataSource = mRst

Exit Sub

ErrorLoad:
MsgBox "错误编号:" & Err.Number & vbCrLf & "错误信息:" & Err.Description, vbCritical, "连接错误"
End Sub