Private Sub Command1_Click()
Dim cnn1 As ADODB.Connection
Dim rs1 As ADODB.Recordset
Dim cmd1 As ADODB.CommandSet cnn1 = New ADODB.Connection
Set rs1 = New ADODB.Recordsetcnn1.Open "Provider=sqloledb;Data Source=TANGZL;Initial Catalog=pubs;User Id=sa;Password=sa;"Set cmd1 = New ADODB.Command
Set cmd1.ActiveConnection = cnn1
cmd1.CommandText = "select id1,id2 from product where id1>2 and id2<8"
Set rs1 = cmd1.Executegrid1.Clear
grid1.Rows = rs1.RecordCount + 1
grid1.Cols = rs1.Fields.CountFor j = 0 To rs1.Fields.Count - 1
grid1.TextMatrix(0, j) = rs1.Fields(j).Name
grid1.ColWidth(j) = 2 * TextWidth(rs1.Fields(j).Name)
Next jrs1.MoveFirst
For i = 0 To rs1.RecordCount - 1
For j = 0 To rs1.Fields.Count - 1
If Not IsNull(rs1.Fields(j).Value) Then
grid1.TextMatrix(i + 1, j) = rs1.Fields(j).Value
End If
Next j
rs1.MoveNext
Next i
End Sub错误提示:
下标越界
错误指向 grid1.TextMatrix(0, j) = rs1.Fields(j).Name
而数据库中明明是有很多数据的啊把grid1.Rows = rs1.RecordCount + 1
grid1.Cols = rs1.Fields.Count
换成
grid1.Rows = 5
grid1.Cols = 5

没有数据显示
而数据库中明明是有很多数据的啊

解决方案 »

  1.   

    cnn1.CursorLocation = adUseClient '*****
    cnn1.Open "Provider=sqloledb;Data Source=TANGZL;Initial Catalog=pubs;User Id=sa;Password=sa;"Set cmd1 = New ADODB.Command
    Set cmd1.ActiveConnection = cnn1
    cmd1.CommandText = "select id1,id2 from product where id1>2 and id2<8"
    Set rs1 = cmd1.Executers1.MoveLast '*****
    grid1.Clear
    grid1.Rows = rs1.RecordCount + 1
    grid1.Cols = 2
      

  2.   

    非常感谢of123() 
    你可以到如下帖子发出回复,无论答案对或错,我都会给你加分
    excel????????????? 
    字段的说明2???此帖将结合我其他发的几个未结帐的帖一起给分!!!!
    excle错误