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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

New grid columns have incorrect columns() reference

Status
Not open for further replies.

BScullion

Programmer
Aug 25, 2003
7
GB
Over time I have added columns to the grid on one of my forms. And I have set columnorder to reflect the order that I want them to display as.

The problem is if I use a loop to reference the columns with the columns() collection, the columns get mixed up.

The code that I am using is:

ls_FieldList = ""
FOR i = 1 TO thisform.Grid.ColumnCount
ls_ControlSource = thisform.Grid.Columns(i).controlsource
ls_FieldList = ls_FieldList + ls_ControlSource + ","
Next

What I want is the field list to be in the same order as is displayed in the grid. (The user cant change the column orders)

Help!
 
the line ls_ControlSource = thisform.Grid.Columns(i).controlsource will generate an error. maybe you should try something like this.

ls_fieldlist = ""
for i = 1 to thisform.grid1.columncount
anycol = "column" + transform(i)
ls_controlsource = thisform.grid1.&anycol..controlsource
ls_fieldlist = ls_fieldlist + ls_controlsource
endfor


take note of the double period (..} in the 4th line of the code.
lemme know if this helped.

kilroy [trooper]
 
also, you should take a look at the FAQ section for more tips on the grid control.

kilroy [trooper]
 
Thanks. This would work, but it would require me to rename my columns from friendly names to column1, column2, etc. Which isnt really a train smash until you add a few columns and have to rename all the columns according to the order, then go through the code and rename them there too.

Isnt there an easier way of doing this?
 
One way could be to build an array to hold the column data, then build your list from the array:
Code:
DIMENSION aCols(MyForm.Grid1.ColumnCount, 2 )
FOR iii = 1 TO MyForm.Grid1.ColumnCount 
  *... Next line is optional
  aCols[MyForm.Grid1.Columns(iii).ColumnOrder, 1] = ;
    MyForm.Grid1.Columns(iii).Name
  *... store the list in an array
  aCols[MyForm.Grid1.Columns(iii).ColumnOrder, 2] = ;
    MyForm.Grid1.Columns(iii).ControlSource
Next 

ls_FieldList = ''
FOR iii = 1 TO MyForm.Grid1.ColumnCount 
  ls_FieldList = ls_FieldList + aCols[iii, 2]+ ","
NEXT


-Dave S.-
[cheers]
Even more Fox stuff at:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top