Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim rsVar As Variant
Dim i As Integer
cn.Open "Testing" '<-- Your DSN
rs.Open "select * from Cies", cn, adOpenStatic, adLockOptimistic
rs.MoveLast
rs.MoveFirst
' Assuming that rs is your ADO recordset
MSHFlexGrid1.Rows = rs.RecordCount + 1
rsVar = rs.GetString(adClipString, rs.RecordCount)
MSHFlexGrid1.Cols = rs.Fields.Count
' Set column names in the grid
For i = 0 To rs.Fields.Count - 1
MSHFlexGrid1.TextMatrix(0, i) = rs.Fields(i).Name
Next
MSHFlexGrid1.Row = 1
MSHFlexGrid1.Col = 0
' Set range of cells in the grid
MSHFlexGrid1.RowSel = MSHFlexGrid1.Rows - 1
MSHFlexGrid1.ColSel = MSHFlexGrid1.Cols - 1
MSHFlexGrid1.Clip = rsVar
' Reset the grid's selected range of cells
MSHFlexGrid1.RowSel = MSHFlexGrid1.Row
MSHFlexGrid1.ColSel = MSHFlexGrid1.Col
rs.Close
Set rs = Nothing
cn.Close
Set cn = Nothing
Name Data Type (SQL Server) Data Type(Access)
------------------------------------------------------------------
PartID Integer Integer
PartName VarChar (10) Text (10)
Option Explicit
Private Sub Command1_Click()
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
cn.Open "Testing" '<-- Your DSN
rs.Open "select * from Test", cn, adOpenStatic, adLockOptimistic
rs.MoveLast
' Check the number records in your ADO recordset
' The RecordCount property of the ADO recordset reflects the
' actual number of records
Debug.Print "ADO recordset record-count: " & vbTab & _
rs.RecordCount
Form1.Caption = "Loading grid... please wait..."
Set MSHFlexGrid1.Recordset = rs
' Check the number of rows that the Hierarchical FlexGrid
' contains The RecordCount property of the Hierarchical
' FlexGrid also reflects the actual number of records
Debug.Print "MSHFlexGrid1 Recordset record-count: " & vbTab & _
MSHFlexGrid1.Recordset.RecordCount
Form1.Caption = "MSHFlexGrid Display Sample"
' Free resources
rs.Close
Set rs = Nothing
cn.Close
Set cn = Nothing
End Sub
Private Sub Command2_Click()
Form1.Caption = "Loading data... please wait..."
' Set the Grid's source to be ADODC
Set MSHFlexGrid1.DataSource = Adodc1
' Check the number of records in ADODC recordset
Debug.Print "ADO data control record-count: " & vbTab & _
Adodc1.Recordset.RecordCount
Form1.Caption = "MSHFlexGrid Display Sample"
End Sub
Private Sub Command3_Click()
MSHFlexGrid1.Clear
Form1.Caption = "Grid is clear now..."
End Sub
重要:本文是以 Microsoft 機器翻譯軟體翻譯而成,而非使用人工翻譯而成。Microsoft 同時提供使用者人工翻譯及機器翻譯兩個版本的文章,讓使用者可以依其使用語言使用知識庫中的所有文章。但是,機器翻譯的文章可能不盡完美。這些文章中也可能出現拼字、語意或文法上的錯誤,就像外國人在使用本國語言時可能發生的錯誤。Microsoft 不為內容的翻譯錯誤或客戶對該內容的使用所產生的任何錯誤或損害負責。Microsoft也同時將不斷地就機器翻譯軟體進行更新。