我想把数据库中的一些列放在msflexgrid控件中,但在“do while。。”处出现“对象变量为设置”,flexgrid控件中只有第一行有标题,而数据没有,这是为什么呢?请高手指教一下。
部分代码如下:
Private Sub Command2_Click()
Dim txtSQL As String
Dim MsgText As String
Dim mrc As ADODB.Recordset
txtSQL = "select soldtocontactid,orderid,carrierid,ordernet from transorderheader"
Set mrc = ExecuteSQL(txtSQL, MsgText)With MSFlexGrid1
     .Rows = 2
     .CellAlignment = 4
     .TextMatrix(1, 0) = "客户编号"
     .TextMatrix(1, 1) = "订单号"
     
     .TextMatrix(1, 2) = "输入人员"
     .TextMatrix(1, 3) = "订单金额"
     Do While Not mrc.EOF
     .Rows = .Rows + 1
     .CellAlignment = 4
     .TextMatrix(.Rows - 1, 0) = mrc.Fields(0)
     .TextMatrix(.Rows - 1, 1) = mrc.Fields(1)
     
     .TextMatrix(.Rows - 1, 2) = mrc.Fields(2)
     .TextMatrix(.Rows - 1, 3) = mrc.Fields(3)     mrc.MoveNext
     Loop
End With
mrc.Close
end sub
类模块的内容如下:
Public Function Connectstring() As String
Connecstring = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=rdtry;Data Source=mlirdtry"
End FunctionPublic Function ExecuteSQL(ByVal SQL As String, MsgString As String) As ADODB.Recordset
Dim cnn As ADODB.Connection
Dim rst As ADODB.Recordset
Dim sTokens() As String
On Error GoTo executesql_error
sTokens = Split(SQL)
Set cnn = New ADODB.Connection
cnn.Open Connectstring
If InStr("INSERT,DELETE,UPDATE", UCase$(sTokens(0))) Then
cnn.Execute SQL
MsgString = sTokens(0) & "query successful"
Else
Set rst = New ADODB.Recordset
rst.Open Trim$(SQL), cnn, adOpenKeyset, adLockOptimistic
Set ExecuteSQL = rst
MsgString = "查询到" & rst.RecordCount & "条记录"
End If
executesql_exit:
Set rst = Nothing
Set cnn = Nothing
Exit Function
executesql_error:
MsgString = "查询错误:" & Err.Description
Resume executesql_exit
End Function

解决方案 »

  1.   

    在Set mrc = ExecuteSQL(txtSQL, MsgText)加上
    Set mrc=new ADODB.Recordset 试试
      

  2.   

    With reportgrid
           .Rows = 2
           .CellAlignment = 4
           .TextMatrix(0, 0) = "111"
           .TextMatrix(0, 1) = "222"
           .TextMatrix(0, 2) = "333"      
         Do While Not src.EOF
            .Rows = .Rows + 1
            .CellAlignment = 4
            .TextMatrix(.Rows - 2, 0) = src.Fields(0)
            .TextMatrix(.Rows - 2, 1) = src.Fields(1)
            .TextMatrix(.Rows - 2, 2) = src.Fields(2)
      

  3.   

    Dim mrc As new ADODB.Recordset
    txtSQL = "select soldtocontactid,orderid,carrierid,ordernet from transorderheader"
    if mrc.state=adstateopen then mrc.close
    mrc.open txtsql,conn,adopenkeyset,adlockreadonly
    ...
      

  4.   

    src.MoveNext
           Loop
      End With
      

  5.   

    select soldtocontactid,orderid,carrierid,ordernet from transorderheader
    你用SQL查询器查询一下如果数据中有NULL字段也是不行了