Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating a control (flexgrid) programmatically in VB/VBA

VBA How To

Creating a control (flexgrid) programmatically in VB/VBA

by  taupirho  Posted    (Edited  )



I came across a weird bug in excel97/VBA the other day. I was using a flexgrid to display some data I was retrieving from some text files. I did the usual thing, dragged the flexgrid onto a new form, set its properties and then started filling it with my retrieved data. So far so good. The problem came when I saved my workbook. It doubled in size. I opened it, saved it again and it doubled in size again. Pretty soon it went from about 750K to 70 Mbytes. Disaster. Anyway the way around this bug was to create the control programmatically. Here's how you do it.

In the declaration section - if you want your control to respond to events

Private WithEvents msflexgrid3 As MSFlexGrid

Now in your module or procedure

'Create the control
'
Set msflexgrid3 = userform1.Controls.Add("MSFLEXGRIDLIB.MSFLEXGRID", harry, True)

'set some of its properties
'
With msflexgrid3
.Height = 120
.Width = 563.4
.Left = 15.6
.Rows = 1
.Cols = 10
.FixedRows = 0
.AllowBigSelection = False
.AllowUserResizing = flexResizeColumns
.Appearance = flex3D
.Font.Name = "arial"
.Font.Size = 8
.Font.Bold = False
.HighLight = 1
.ScrollBars = 3
.SelectionMode = 1
.Top = 35
.colwidth(0) = 240
.colwidth(1) = 840
.colwidth(2) = 1300
.colwidth(3) = 840
.colwidth(4) = 930
.colwidth(5) = 1200
.colwidth(6) = 1060
.colwidth(7) = 2000
.colwidth(8) = 2000
.colwidth(9) = 795
.CellAlignment = 2
.Redraw = True

'Add some column titles
'
.AddItem "Column 1" & vbTab & _
"Column 2" & vbTab & _
"Column 3" & vbTab & _
"Column4" & vbTab & _
"Column 5" & vbTab & _
"Column 6" & vbTab & _
"Column 7" & vbTab & _
"Column 8" & vbTab & _
"Column 9" & vbTab & _
"Column 10", 0





'Assume we have a recordset full of data, this will add them one by one
' to the flexgrid

For i = 0 To ADOlookup2.RecordCount - 1

With msflexgrid3
.AddItem "X" & vbTab &
ADOlookup2.Fields.Item(0).Value & vbTab & _
ADOlookup2.Fields.Item(1).Value & vbTab & _
ADOlookup2.Fields.Item(2).Value & vbTab & _
ADOlookup2.Fields.Item(3).Value & vbTab & _
ADOlookup2.Fields.Item(4).Value & vbTab & _
ADOlookup2.Fields.Item(5).Value & vbTab & _
ADOlookup2.Fields.Item(6).Value & vbTab & _
ADOlookup2.Fields.Item(7).Value & vbTab & _
ADOlookup2.Fields.Item(8).Value, 1
End With

ADOlookup.MoveNext

Next I

With msflexgrid3
.Rows = ADOlookup.RecordCount + 1
.Row = 0
.col = 0
.RowSel = .FixedRows - 1
.ColSel = .Cols - 1
.CellAlignment = 2
.Redraw = True
End with

Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top