vb 对象变量或with块变量未设置

来源:百度知道 编辑:UC知道 时间:2024/09/24 23:30:03
Private Sub ShowData()

Dim j As Integer
Dim i As Integer
Dim MsgText As String

Set mrc = ExecuteSQL(txtSQL, MsgText)
With msgList
.Rows = 1

Do While Not mrc.EOF
.Rows = .Rows + 1
For i = 1 To mrc.Fields.Count
If Not IsNull(Trim(mrc.Fields(i - 1))) Then
Select Case mrc.Fields(i - 1).Type
Case adDBDate
.TextMatrix(.Rows - 1, i) = Format(mrc.Fields(i - 1) & "", "yyyy-mm-dd")
Case Else
.TextMatrix(.Rows - 1, i) = mrc.Fields(i - 1) & ""
End Select
End If
Next i
mrc.MoveNext
Loop

End With
mrc.Close

End Sub

你好 请问下我上面的代码错在了呢??怎么改呢?

这里:
mrc.MoveNext
改成
If Not mrc.EOF() Then mrc.MoveNext

还有,最好是判断下mrc.RecordCount的值,后面的mrc.close也最好是这样写法比较安全:
If Not (mrc Is Nothing) Then
If mrc.State = adStateOpen Then mrc.Close:set mrc=Nothing
End If