注意点如下:
1、{sql server}中间要加空格,这个地方卡了很久;
2、textmatrix.0代表标题行3
3、MS1.cols要比rs.fields.count多一列,因为左边有一列是序号列,不过经测试,用MS1.TextMatrix(0, n - 1) = Rs.Fields(n - 1).Name和MS1.TextMatrix(j, i - 1) = Rs.Fields(i - 1)也能运行,而且好像更清楚一些,这样的话因为利用了标题列,则COLS可以=RS.fields.count
4、rs.recordcount事先是算不出来的
5、DAO选2.51,否则会出现“系统不支持所选择的排序”
Dim Db As Database
Dim Rs As Recordset
Dim Ws As Workspace
Private Sub Form_Load()
Set Ws = CreateWorkspace("", "admin", "")
Set Db = Ws.OpenDatabase("", False, False, "driver={sql server};server=wjpc;uid=sa;pwd=;database=db_2008")
Set Rs = Db.OpenRecordset("aa")
If Rs.RecordCount > 0 Then
Rs.MoveFirst
Dim i%, j%, n%
j = 1
MS1.Cols =Rs.Fields.Count + 1
MS1.Rows = 78 '78
For n = 1 To Rs.Fields.Count
MS1.TextMatrix(0, n) = Rs.Fields(n - 1).Name
Next
Do While Rs.EOF = False
For i = 1 To 10
MS1.TextMatrix(j, i) = Rs.Fields(i - 1)
Next
Rs.MoveNext
j = j + 1
Loop
End If
End Sub