Dynamically Populate MSFlexGrid Control

Dynamically Populate MSFlexGrid Control

If you use an MSFlexGrid control to display data returned in an ADO recordset, you can use this code to dynamically populate the grid?including the header row?with the information in the recordset. You need an open ADO recordset named rst and a form containing an MSFlexGrid control named msfGrid:

 Dim cln As FieldWith msfGrid	.Rows = 2	.Cols = rst.Fields.Count  	'get the number of grid cols	.FixedRows = 1	.FixedCols = 0	.Row = 0	.Col = 0	For Each cln In rst.Fields		.Text = cln.Name  		'populate header row with names of fields		If .Col < .Cols - 1 Then .Col = .Col + 1	Next	Do While Not rst.EOF   	'loop thru recordset to populate grid		.Row = rst.AbsolutePosition		'move to the next row		.Col = 0   		'reset ourselves back to column(0)		For Each cln In rst.Fields			If Not IsNull(cln.Value) Then				.Text = Trim(CStr(cln.Value))			Else				.Text = ""			End If			If .Col < .Cols - 1 Then .Col = .Col + 1		Next		rst.MoveNext		.Rows = .Rows + 1  		'add a new row to the grid	Loop	.Rows = .Rows - 1   	'remove the last row because it's blank	.Row = 0End With
See also  Essential Measures for Safeguarding Your Digital Data

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist