|
-
Aug 2nd, 2005, 10:15 PM
#1
Thread Starter
Member
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
Last edited by jltl; Aug 2nd, 2005 at 10:26 PM.
~!~Computer Nerd~!~
-
Aug 2nd, 2005, 10:25 PM
#2
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.
-
Aug 2nd, 2005, 10:26 PM
#3
Thread Starter
Member
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 ^^
-
Aug 2nd, 2005, 10:51 PM
#4
Thread Starter
Member
Re: Loading Into an MSFlexGrid
-
Aug 2nd, 2005, 11:19 PM
#5
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.
-
Aug 2nd, 2005, 11:23 PM
#6
Thread Starter
Member
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
-
Aug 2nd, 2005, 11:49 PM
#7
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.
-
Aug 2nd, 2005, 11:52 PM
#8
Thread Starter
Member
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!
-
Aug 4th, 2005, 03:10 PM
#9
Thread Starter
Member
Re: Loading Into an MSFlexGrid
Gar.. I'm getting that stupid Subscript out of range error again... What should I do -_-.
-
Aug 4th, 2005, 03:26 PM
#10
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?
-
Aug 4th, 2005, 03:28 PM
#11
Hyperactive Member
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:
 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
-
Aug 4th, 2005, 03:39 PM
#12
Thread Starter
Member
Re: Loading Into an MSFlexGrid
Hmm... Now I get the error:
"Can not remove last non-fixed row"
>< FlexGrids are complicated :P
-
Aug 4th, 2005, 03:43 PM
#13
Hyperactive Member
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
-
Aug 4th, 2005, 03:43 PM
#14
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.
-
Aug 4th, 2005, 03:43 PM
#15
Hyperactive Member
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
-
Aug 4th, 2005, 03:48 PM
#16
Thread Starter
Member
Re: Loading Into an MSFlexGrid
How would I set the Row Height of the first row to 0 though? Thank in advanced.
-
Aug 4th, 2005, 03:50 PM
#17
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.
-
Aug 4th, 2005, 03:52 PM
#18
Thread Starter
Member
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
Last edited by jltl; Aug 4th, 2005 at 03:55 PM.
~!~Computer Nerd~!~
-
Aug 4th, 2005, 03:56 PM
#19
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?
-
Aug 4th, 2005, 03:58 PM
#20
Thread Starter
Member
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
-
Aug 4th, 2005, 04:13 PM
#21
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
-
Aug 4th, 2005, 04:16 PM
#22
Thread Starter
Member
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.
-
Aug 4th, 2005, 04:18 PM
#23
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.
-
Aug 4th, 2005, 04:19 PM
#24
Thread Starter
Member
Re: Loading Into an MSFlexGrid
 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?
Last edited by jltl; Aug 4th, 2005 at 04:24 PM.
~!~Computer Nerd~!~
-
Aug 4th, 2005, 04:25 PM
#25
Re: Loading Into an MSFlexGrid
 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.
-
Aug 4th, 2005, 04:28 PM
#26
Thread Starter
Member
Re: Loading Into an MSFlexGrid
 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!
-
Aug 4th, 2005, 04:30 PM
#27
Re: Loading Into an MSFlexGrid
 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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|