Dim conn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim sql As String
conn.Open connstring
sql = "select * from [user]"
rs.Open sql, conn, 1, 1            With grdlist
                 .Cols = 4
                 .TextMatrix(0, 0) = ""
                 .TextMatrix(0, 1) = "用户名"
                 .TextMatrix(0, 2) = "密码"
                 .TextMatrix(0, 3) = "用户类别"
                 
                 .FixedRows = 1
                 
                 For i = 1 To .Cols - 1
                     .ColAlignment(i) = 0
                 Next i
                 
                 .FillStyle = flexFillRepeat
                 .Col = 0
                 .Row = 0
                 .ColSel = 1
                 .ColSel = .Cols - 1
                 .CellAlignment = 4
                 
                 .ColWidth(0) = 0
                 .ColWidth(1) = 1200
                 .ColWidth(2) = 1200
                 .ColWidth(3) = 1000
                 Do While Not rs.EOF
                 .Rows = .Rows + 1
                        For i = 1 To rs.RecordCount
                        .TextMatrix(i, 0) = ""
                        .TextMatrix(i, 1) = rs("admin")
                        .TextMatrix(i, 2) = rs("pass")
                        .TextMatrix(i, 3) = rs("myclass")
                        Next i
                    rs.MoveNext
                 Loop
                 
            End With
    rs.Close
    Set rs = Nothing
    conn.Close
    Set conn = Nothing--------------------------------------------------
错误提示:.TextMatrix(i, 0) = ""   下标越界

解决方案 »

  1.   

    把  .ColSel = 1
        .ColSel = .Cols - 1
    取消掉看看
      

  2.   

    樓主可看見自己用了雙循環了,
    把你的do while取消掉用這樣顯示就可以了
    rs1.MoveFirst
      Do While Not rs1.EOF
         With Form1.MSFlexGrid1
             .Row = .Rows - 1
             .Col = 0
             .Text = IIf(IsNull(rs1("bianhao")), "", rs1("bianhao"))   '請注意是小寫的i﹐不是L
             .Col = 1
             .Text = IIf(IsNull(rs1("danwei")), "", rs1("danwei"))
             .Col = 2
             .Text = IIf(IsNull(rs1("shuzhi")), "", rs1("shuzhi")) & "%"
             .Col = 3
             .Text = IIf(IsNull(rs1("userid")), "", rs1("userid"))
             .Col = 4
             .Text = IIf(IsNull(rs1("jiandan")), "", rs1("jiandan"))
             .Col = 5
             .Text = IIf(IsNull(rs1("shengxiao")), "", rs1("shengxiao"))
           End With
           Form1.MSFlexGrid1.Rows = Form1.MSFlexGrid1.Rows + 1
           rs1.MoveNext
      Loop
      

  3.   

    我去掉了这一段也还是不行:                 .FillStyle = flexFillRepeat
                     .Col = 0
                     .Row = 0
                     .rowsel=1
                     .ColSel = .Cols - 1
                     .CellAlignment = 4
      

  4.   

    For i = 1 To rs.RecordCount
    .TextMatrix(i, 0) = ""
    .TextMatrix(i, 1) = rs("admin")
    .TextMatrix(i, 2) = rs("pass")
    .TextMatrix(i, 3) = rs("myclass")
    Next i不明白你这样写的作用,改成下面的试试i = .Rows-1
    .TextMatrix(i, 0) = ""
    .TextMatrix(i, 1) = rs("admin")
    .TextMatrix(i, 2) = rs("pass")
    .TextMatrix(i, 3) = rs("myclass")
      

  5.   

    i = 0
    Do While Not rs.EOF
        i = i + 1
        .TextMatrix(i, 0) = ""
        .TextMatrix(i, 1) = rs("admin")
        .TextMatrix(i, 2) = rs("pass")
        .TextMatrix(i, 3) = rs("myclass")
        rs.MoveNext
    Loop
      

  6.   

    With grdlist
                     .Cols = 4
                     .TextMatrix(0, 0) = ""
                     .TextMatrix(0, 1) = "用户名"
                     .TextMatrix(0, 2) = "密码"
                     .TextMatrix(0, 3) = "用户类别"
                     
                     .FixedRows = 1
                     
                     For i = 1 To .Cols - 1
                         .ColAlignment(i) = 0
                     Next i
                     
                     .FillStyle = flexFillRepeat
                     .Col = 0
                     .Row = 0
                     .ColSel = 1
                     .ColSel = .Cols - 1
                     .CellAlignment = 4
                     
                     .ColWidth(0) = 0
                     .ColWidth(1) = 1200
                     .ColWidth(2) = 1200
                     .ColWidth(3) = 1000
                     
                     Do While Not rs.EOF
                         i = .Rows - 1
                        .TextMatrix(i, 0) = ""
                        .TextMatrix(i, 1) = rs("admin")
                        .TextMatrix(i, 2) = rs("pass")
                        .TextMatrix(i, 3) = rs("myclass")
                         rs.MoveNext
                         .Rows = .Rows + 1
                     Loop
                     
                End With写这了这样可以没错误,可以打开,就是怎么会在最后面多出一空白行了?新人新问题,前辈们多指教.........
      

  7.   

    .Rows = 1
                     Do While Not rs.EOF
                         .Rows = .Rows + 1
                         i = .Rows - 1
                        .TextMatrix(i, 0) = ""
                        .TextMatrix(i, 1) = rs("admin")
                        .TextMatrix(i, 2) = rs("pass")
                        .TextMatrix(i, 3) = rs("myclass")
                         rs.MoveNext
                     Loop
      

  8.   

    写这了这样可以没错误,可以打开,就是怎么会在最后面多出一空白行了?
    ////
    本來就會多一行呀,如果你不要多一行,當數據完全讀完以後你吧
    .rows=.rows-1
    就可以了呀