Dear Jahangir,
I'll try illustrate what you require with MSFLex Grid control.
'Simply pass this function the flexgrid object and the sql statement
'and it will do the rest.
'
'This could be customised as per your needs
'Parameter :
' objGrid : The flexgrid object
' sSQL : The sql statement used to retrieve the data
' Returns :
' true : If the grid is successfully filled
' false : If the grid is not filled
Private Function FillGrid(objGrid As MSFlexGrid, sSQL As String)
On Error GoTo errHandler
Dim sConStr As String
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim lngCurrRow As Long ' Current row in the grid
Dim intCols As Integer ' Total cols
Dim intCurrCol As Integer ' Current col in the grid
sConStr = "Your Database DSN"
cn.ConnectionString = sConStr
cn.Open
rs.Open sSQL, cn
If rs.EOF Then
rs.Close
Set rs = Nothing
cn.Close
Set cn = Nothing
FillGrid = False
Exit Function
End If
intCols = rs.Fields.Count
'Print Header
With objGrid
.Cols = intCols
.FixedCols = 0
.Rows = 2
For intCurrCol = 0 To intCols - 1
.TextMatrix(0, intCurrCol) = rs.Fields(intCurrCol).Name
Next
End With
lngCurrRow = 1
Do While rs.EOF = False
With objGrid
For intCurrCol = 0 To intCols - 1
.TextMatrix(lngCurrRow, intCurrCol) = rs.Fields(intCurrCol) & ""
Next
.Rows = .Rows + 1
End With
rs.MoveNext
lngCurrRow = lngCurrRow + 1
Loop
FillGrid = True ' Grid has been successfully filled
Exit Function
errHandler:
MsgBox Err.Description
End Function
Please customise the error handling as per the app. needs.
Hope you will find something out of this.
Regards,
Rajesh