specify the msflex.col and .row of the msflex and use .text method to display the value into the grid.
ex :
do while not rs.eof
msflex1.col=1
msflex1.row=1
msflex1.text=rs!ecode
msflex1.col=2
msflex1.row=1
msflex1.text=rs!ename
msflex1.col=3
msflex1.row=1
msflex1.text=rs!desig
rs.movenext
loop
and so on
am Saving Database in TWo Tables
ie. Inward_local Receipt_no # P Key
Inward_items Receipt_no & Sr_no -> # P Key
Invoice Details are Storing in Inward_local Database
While Item_details are Storing in Inward_items as Aganist one Receipt_no Many Items Can Inwarded. One to Many Relationship.
i Wants to Fetch the Records From Inward_items Into Flexgrids to the Correspodning Receipt_no Where Receipt_no & Sr_no
Suppose Against Receipt_no There 3 Items are Inwarded
Where X is Not all the Record in A Table
but X = Suppose Receipt_no 1 Has 5 Items ie
Receipt_no 1 SerialNo 1
Receipt_no 1 SerialNo 2
Receipt_no 1 SerialNo 3
Receipt_no 1 SerialNo 4
Receipt_no 1 SerialNo 5
Recordcount will give all records but i want only Records Against Corresponding Receipt_no 1
When Receipt_no 2 Then Condition should fail
Here's a Sub I recently wrote, you can just put this into your project and use it as in the "'Example usage" comments.
Code:
Public Sub FillFromRecordset_FlexGrid(p_ctlFlexGrid As Control, _
p_objRecordset As Object, _
p_booFieldNamesAsHeaders As Boolean)
'Fill an MSFlexGrid control with values from a recordset
'By Si_the_geek, VBForums
'Parameters:
' p_ctlFlexGrid - The FlexGrid control to fill
' p_objRecordset - The Recordset to get the data from (can be ADO/DAO)
' p_booFieldNamesAsHeaders - True to show field names as column headers
'Example usage:
' Call FillFromRecordset_FlexGrid(MSFlexGrid1, objRS, True)
Dim lngCol As Long
Dim lngRow As Long
Dim booOldRedraw As Boolean
With p_ctlFlexGrid
'Set up the required number of columns
.Cols = p_objRecordset.Fields.Count
.FixedCols = 0
'If specified, show field names as headers
If p_booFieldNamesAsHeaders Then
.FixedRows = 1
For lngCol = 0 To p_objRecordset.Fields.Count - 1
.TextMatrix(0, lngCol) = p_objRecordset.Fields(lngCol).Name
Next lngCol
End If
'remove all rows except any headers, and the first data row (cannot be removed)
.Rows = .FixedRows + 1
'Turn off screen updates (much faster to fill the data)
booOldRedraw = .Redraw
.Redraw = False
'Check if there is any data
If p_objRecordset.EOF Then
'if there is no data, only allow the required blank row, and hide it (height=0)
.AddItem ""
.RemoveItem .FixedRows
.RowHeight(.FixedRows) = 0
Else
'We have data, add it one row at a time
'(nb: there are various ways to do this, this way is quick, and easy to read)
lngRow = .Rows
Do While Not p_objRecordset.EOF
'Add the row (empty)
.AddItem ""
'Set the values once cell at a time (avoids problems with Nulls and data containing grid delimiters)
For lngCol = 0 To p_objRecordset.Fields.Count - 1
'if you want to format the text for some columns differenly, you can _
'use If/Else or Select Case here - but its better to do that in your SQL _
'statement, as it is more efficient, and allows this sub to be re-used easily
.TextMatrix(lngRow, lngCol) = p_objRecordset.Fields(lngCol).Value & ""
Next lngCol
'Increment our row counter
lngRow = lngRow + 1
'Move to the next row of data
p_objRecordset.MoveNext
Loop
'Remove the blank row we left at the top
.RemoveItem .FixedRows
End If
'Re-enable screen updates (if was previously enabled)
.Redraw = booOldRedraw
'Force a redraw of the grid
.Refresh
End With
End Sub
Last edited by si_the_geek; Nov 21st, 2007 at 06:43 PM.
Reason: changed to Code tags for easier copy&paste, and extended comments
Here's a Sub I recently wrote, you can just put this into your project and use it as in the "'Example usage" comments.
Code:
Public Sub FillFromRecordset_FlexGrid(p_ctlFlexGrid As Control, _
p_objRecordset As Object, _
p_booFieldNamesAsHeaders As Boolean)
'Fill an MSFlexGrid control with values from a recordset
'By Si_the_geek, VBForums
'Parameters:
' p_ctlFlexGrid - The FlexGrid control to fill
' p_objRecordset - The Recordset to get the data from (can be ADO/DAO)
' p_booFieldNamesAsHeaders - True to show field names as column headers
'Example usage:
' Call FillFromRecordset_FlexGrid(MSFlexGrid1, objRS, True)
Dim lngCol As Long
Dim lngRow As Long
Dim booOldRedraw As Boolean
With p_ctlFlexGrid
'Set up the required number of columns
.Cols = p_objRecordset.Fields.Count
.FixedCols = 0
'If specified, show field names as headers
If p_booFieldNamesAsHeaders Then
.FixedRows = 1
For lngCol = 0 To p_objRecordset.Fields.Count - 1
.TextMatrix(0, lngCol) = p_objRecordset.Fields(lngCol).Name
Next lngCol
End If
'remove all rows except any headers, and the first data row (cannot be removed)
.Rows = .FixedRows + 1
'Turn off screen updates (much faster to fill the data)
booOldRedraw = .Redraw
.Redraw = False
'Check if there is any data
If p_objRecordset.EOF Then
'if there is no data, only allow the required blank row, and hide it (height=0)
.AddItem ""
.RemoveItem .FixedRows
.RowHeight(.FixedRows) = 0
Else
'We have data, add it one row at a time
'(nb: there are various ways to do this, this way is quick, and easy to read)
lngRow = .Rows
Do While Not p_objRecordset.EOF
'Add the row (empty)
.AddItem ""
'Set the values once cell at a time (avoids problems with Nulls and data containing grid delimiters)
For lngCol = 0 To p_objRecordset.Fields.Count - 1
'if you want to format the text for some columns differenly, you can _
'use If/Else or Select Case here - but its better to do that in your SQL _
'statement, as it is more efficient, and allows this sub to be re-used easily
.TextMatrix(lngRow, lngCol) = p_objRecordset.Fields(lngCol).Value & ""
Next lngCol
'Increment our row counter
lngRow = lngRow + 1
'Move to the next row of data
p_objRecordset.MoveNext
Loop
'Remove the blank row we left at the top
.RemoveItem .FixedRows
End If
'Re-enable screen updates (if was previously enabled)
.Redraw = booOldRedraw
'Force a redraw of the grid
.Refresh
End With
End Sub
Hi how to set column width to auto-fit with this ?
i usually set it manually with msflexgrid1.FormatString