-
Loading Into an MSFlexGrid
Alright, well I have a MSFlexGrid on the form, and I have a function set up that saves everything in the FlexGrid to a file in a certain format. How would I be able to load the saved file back into the FlexGrid? This is this format it saves in:
Code:
First Section's Data, Second Section's Data ,
Here is the code used to save:
Code:
Public Function SaveFlex(FileName As String)
'First, open the file
Open FileName For Output As #1
iRows = MSFlexGrid1.Rows
iCols = MSFlexGrid1.Cols
For i = 0 To iRows - 1
For j = 0 To iCols - 1
If IsNumeric(MSFlexGrid1.TextMatrix(i, j)) Then
Print #1, CCur(MSFlexGrid1.TextMatrix(i, j)); ",";
Else
Print #1, MSFlexGrid1.TextMatrix(i, j); ",";
End If
Next j
Print #1, 'Finish the current row.
Next i
Close #1 'Close the file
End Function
Thanks in advaned!
JLTL
-
Re: Loading Into an MSFlexGrid
You would use the same format to read it. Post the code that writes the data, and we'll try to help you read it back in. If you can't post the code in a thread, post your zipped project and all support files it needs.
-
Re: Loading Into an MSFlexGrid
Code:
Public Function SaveFlex(FileName As String)
'First, open the file
Open FileName For Output As #1
iRows = MSFlexGrid1.Rows
iCols = MSFlexGrid1.Cols
For i = 0 To iRows - 1
For j = 0 To iCols - 1
If IsNumeric(MSFlexGrid1.TextMatrix(i, j)) Then
Print #1, CCur(MSFlexGrid1.TextMatrix(i, j)); ",";
Else
Print #1, MSFlexGrid1.TextMatrix(i, j); ",";
End If
Next j
Print #1, 'Finish the current row.
Next i
Close #1 'Close the file
End Function
There ya go ^^
-
Re: Loading Into an MSFlexGrid
-
Re: Loading Into an MSFlexGrid
Something like this should work:
VB Code:
Public Function ReadFlex(FileName As String)
Dim str As String, rowI() As String, colI() As String
Open FileName For Input As #1
str = Input(LOF(1), #1) ' Read file into string
Close #1 'Close the file
rowI() = Split(str, vbCrLf) ' Split into rows
For i = 0 To UBound(rowI) ' Determine number of rows
colI = Split(rowI(i), ",") ' Split each row into columns
For j = 0 To UBound(colI) ' Determine number of cols
MSFlexGrid1.TextMatrix(i, j) = colI(j) ' Assign col to grid
Next j
Next i
End Function
Post back if you have any problems.
I'll be around for a while.
-
Re: Loading Into an MSFlexGrid
Hmm. I get this:
Code:
Runtime Error '381':
Subscript out of range
and it points to this line:
Code:
MSFlexGrid1.TextMatrix(i, j) = colI(j) ' Assign col to grid
Thanks in advance :)
-
Re: Loading Into an MSFlexGrid
Oh, you have an extra comma in there. Use:
VB Code:
For j = 0 To UBound(colI) - 1 ' Determine number of cols
and please, no more PM's. The box gets filled too quickly.
-
Re: Loading Into an MSFlexGrid
Thanks again ^^ And sorry about the PM's I'm a desperate little programmer xD
Edit: Worked PERFECTLY! Thanks a ton!
-
Re: Loading Into an MSFlexGrid
Gar.. I'm getting that stupid Subscript out of range error again... What should I do -_-.
-
Re: Loading Into an MSFlexGrid
Where is it occurring? If you are getting all of the rows, you could subtracting 1 to see if there is an extra line in there. Did it ever work?
-
Re: Loading Into an MSFlexGrid
Does your flexgrid have enough rows? If not use .additem to add rows. You could amend dglienna's code if you want to:
Quote:
Originally Posted by dglienna
VB Code:
Public Function ReadFlex(FileName As String)
Dim str As String, rowI() As String, colI() As String
Open FileName For Input As #1
str = Input(LOF(1), #1) ' Read file into string
[COLOR=DarkRed][I] str = replace(str,",",vbTab) ' replaces comma with a tab character, so .additem works properly[/I][/COLOR]
Close #1 'Close the file
rowI() = Split(str, vbCrLf) ' Split into rows
[COLOR=DarkRed][I]for z= MSFlexgrid1.rows to 1 step -1
MSFlexgrid1.removeitem 1
next z[/I][/COLOR]
For i = 0 To UBound(rowI) ' Determine number of rows
[COLOR=DarkRed][I]MSFlexgrid1.additem rowI(i)[/I][/COLOR]
Next i
End Function
.additem will add a row, and uses the tab character to split the input row into columns. My approach deletes all the rows in the grid first, then populates it one row at a time
-
Re: Loading Into an MSFlexGrid
Hmm... Now I get the error:
"Can not remove last non-fixed row"
>< FlexGrids are complicated :P
-
Re: Loading Into an MSFlexGrid
Oh yeah! I remember that one. You either deal with the first line separately or (a simpler workaround, though I'm sure the real programmers would disapprove) set the rowheight of row 1 to 0 (hide it) and alter
VB Code:
for z= MSFlexgrid1.rows to 1 step -1
MSFlexgrid1.removeitem 1
next z
to
VB Code:
for z= MSFlexgrid1.rows to 2 step -1
MSFlexgrid1.removeitem 1
next z
-
Re: Loading Into an MSFlexGrid
Please post the code you are using. There are 100 reasons that you code could give you that error. We need to see what you are doing to be able to help you. If your code is too big post it as an attachment. Thanks. :)
-
Re: Loading Into an MSFlexGrid
Oh yeah! I remember that one. You either deal with the first line separately or (a simpler workaround, though I'm sure the real programmers would disapprove) set the rowheight of row 1 to 0 (hide it) and alter
VB Code:
for z= MSFlexgrid1.rows to 1 step -1
MSFlexgrid1.removeitem 1
next z
to
VB Code:
for z= MSFlexgrid1.rows to [COLOR=DarkRed]2[/COLOR] step -1
MSFlexgrid1.removeitem [COLOR=DarkRed]2[/COLOR]
next z
-
Re: Loading Into an MSFlexGrid
How would I set the Row Height of the first row to 0 though? Thank in advanced.
-
Re: Loading Into an MSFlexGrid
I did the exact opposite of the code that wrote the file. You had an extra comma that we took care of, now you might have an extra row.
Stick with one way. We can't tell you how to fix it if you have two differnt routines. Post what you have.
-
Re: Loading Into an MSFlexGrid
Here is both the Save Flex and the Load Flex:
VB Code:
Public Function LoadFlex(FileName As String)
Dim str As String, rowI() As String, colI() As String
Open FileName For Input As #1
str = Input(LOF(1), #1) ' Read file into string
str = Replace(str, ",", vbTab) ' replaces comma with a tab character, so .additem works properly
Close #1 'Close the file
rowI() = Split(str, vbCrLf) ' Split into rows
For z = MSFlexGrid1.Rows To 2 Step -1
MSFlexGrid1.RemoveItem 1
Next z
For i = 0 To UBound(rowI) ' Determine number of rows
MSFlexGrid1.AddItem rowI(i)
Next i
End Function
Public Function SaveFlex(FileName As String)
'First, open the file
Open FileName For Output As #1
iRows = MSFlexGrid1.Rows
iCols = MSFlexGrid1.Cols
For i = 0 To iRows - 1
For j = 0 To iCols - 1
If IsNumeric(MSFlexGrid1.TextMatrix(i, j)) Then
Print #1, CCur(MSFlexGrid1.TextMatrix(i, j)); ",";
Else
Print #1, MSFlexGrid1.TextMatrix(i, j); ",";
End If
Next j
Print #1, 'Finish the current row.
Next i
Close #1 'Close the file
End Function
-
Re: Loading Into an MSFlexGrid
I don't like that method. You don't have to clear out the flexgrid when writing to it. If you are filling in all cells, then it will erase what was in there.
What line is the error occurring on?
-
Re: Loading Into an MSFlexGrid
Then I'll use your method, which worked right off the bat, but then stopped working. It pointed to the same line as before.
VB Code:
MSFlexGrid1.TextMatrix(i, j) = colI(j)
That line
-
Re: Loading Into an MSFlexGrid
How many rows are in the grid? How many columns?
Hover the mouse over I, and J to see what the values are. There may be an extra line of code, so you would just -1 from .rows in the FOR statement, just as we did with the J For statement for the .cols
-
Re: Loading Into an MSFlexGrid
In VB it says there are 2 rows and 2 collumns. But when I open it and it loads the information from the Access Database, it changes to however many values there is in the database.
-
Re: Loading Into an MSFlexGrid
You can use .cols to find the max number of cols and .rows to find the max number of rows.
-
Re: Loading Into an MSFlexGrid
Quote:
Originally Posted by eyeRmonkey
You can use .cols to find the max number of cols and .rows to find the max number of rows.
In newbie terms? o.o
Edit: nvm, figured it out =P
Edit Again: Ok, VERY wierd. With some lists, it works, but with others it doesn't?
-
Re: Loading Into an MSFlexGrid
Quote:
Originally Posted by jltl
Edit Again: Ok, VERY wierd. With some lists, it works, but with others it doesn't?
Once again, if you want help, we need a better idea of whats going wrong. Does an error occur? If so what does it say and what line does it highlight? If not then tell us how it "doesn't work." Thanks. :)
-
Re: Loading Into an MSFlexGrid
Quote:
Originally Posted by eyeRmonkey
Once again, if you want help, we need a better idea of whats going wrong. Does an error occur? If so what does it say and what line does it highlight? If not then tell us how it "doesn't work." Thanks. :)
I already posted the error and the line a few posts up.
Edit: I'm not going to use the MSFlexGrid. For what I'm doing, there is way to much going on. I'll just use a simpler listbox :P Thanks for all your help though!
-
Re: Loading Into an MSFlexGrid
Quote:
Originally Posted by jltl
I already posted the error and the line a few posts up.
Ok, well you said you figured it out, then said it wasn't working again so I assumed it was a new error.