View Single Post
Old 02-17-2003, 09:06 AM
Flyguy's Avatar
Flyguy Flyguy is offline
Lost Soul

Retired Moderator
* Guru *
Join Date: May 2001
Location: Vorlon
Posts: 19,175
Default Show Table data in MSFlexgrid

For this code sample place a MSFlexGrid control (MSFlexGrid1) and a command button (Command1) on your form and make a reference to "Microsoft ActiveX Data Object 2.x Library"
Option Explicit Dim m_sDBPath As String Private Sub Command1_Click() Dim adoConn As ADODB.Connection Dim adoRST As ADODB.Recordset On Error Goto errHandler ' open the connection Set adoConn = New ADODB.Connection adoConn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _ "Data Source=" & m_sDBPath ' get a recordset Set adoRST = New ADODB.Recordset adoRST.Open "Titles", adoConn, adOpenForwardOnly, , adCmdTable With adoRST If Not .EOF Then FG_ShowRecordset MSFlexGrid1, adoRST End If .Close End With adoConn.Close Set adoConn = Nothing Set adoRST = Nothing Exit Sub errHandler: MsgBox "An error occured." & vbLf & Err.Number & ": " & Err.Description, vbCritical Set adoConn = Nothing Set adoRST = Nothing End Sub Private Sub FG_ShowRecordset(myFG As MSFlexGrid, myRST As ADODB.Recordset) Dim iField As Integer, iNofFields As Integer Dim lRow As Long Screen.MousePointer = vbHourglass With myRST .MoveFirst iNofFields = .Fields.Count End With With myFG .Redraw = False .AllowUserResizing = flexResizeColumns .ScrollTrack = True .FixedCols = 0 .FixedRows = 0 .Cols = iNofFields .Rows = 1 ' setup the header For iField = 0 To iNofFields - 1 .TextMatrix(0, iField) = myRST.Fields(iField).Name Next iField End With With myRST Do ' increase the number of rows lRow = myFG.Rows myFG.Rows = myFG.Rows + 1 ' add the values to the current row For iField = 0 To iNofFields - 1 If Not IsNull(.Fields(iField).Value) Then myFG.TextMatrix(lRow, iField) = .Fields(iField).Value End If Next iField ' proceed to the next record .MoveNext Loop Until .EOF End With With myFG .FixedRows = 1 .Redraw = True End With Screen.MousePointer = vbNormal End Sub Private Sub Form_Load() m_sDBPath = "C:\Program Files\Microsoft Visual Studio\VB98\Biblio.MDB" Me.Caption = m_sDBPath End Sub
Reply With Quote