Combine 4 mshflexgrids with random access

Hi Experts

I have working on an estimating program for about 4 years now and have learned vb as i go.
Without the knowledge of planning ahead properly i get stuck trying to add new features.
I save my estimates to a Random access file with about 110 fields of varaible length strings, integers, and currency's.
My problem
I am trying to add 4 mshflexgrids of variable numbers of rows. 2 have 4-columns and 2 have 2 columns
How can i combine  the random access and the 4 mshflexgrids to 1 file and reopen it. ?
I do not need the mshflexigrid settings, just the data
isnoend2001Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

HTorresCommented:
You can pack each msf data into a string, and format it like this

with me.msf1
   .row = 0
   .col=0
   .colsel = .cols-1
   .rowsel = .rows-1
   data1 = .clip
end with

... and so on with msf2, msf3, msf4

... after that:

t = format(len(data1),"000000") & "||DATASTART:" & data1 & ":DATAEND||" & _
     format(len(data2),"000000") & "||DATASTART:" & data2 & ":DATAEND||" & _
     format(len(data3),"000000") & "||DATASTART:" & data3 & ":DATAEND||" & _
     format(len(data4),"000000") & "||DATASTART:" & data4 & ":DATAEND||"

open "somefile" for binary as #1
put #1,1,t
close 1

'so your data is saved

'to reopen it:

open "somefile" for binary as #1
t = string(lof(1)," ")
get #1,1,t
close 1
p=0
do
   currentlyretrieving=   currentlyretrieving+1
   p = instr(p+1,t,"||DATASTART",vbTextCompare)
   if p > 0 then
      'found one
      datalen = VAL(mid(t,p-7,6))
      if currentlyretrieving=1 then data1 = mid(t,p+10,datalen)
      if currentlyretrieving=2 then data2 = mid(t,p+10,datalen)
      if currentlyretrieving=3 then data3 = mid(t,p+10,datalen)
      if currentlyretrieving=4 then data4 = mid(t,p+10,datalen)
   else
      'not found, so exit
      exit do
   end if
loop

thats it.

it MAY need some tweeking because im doing it on the fly, without vb near.

but the idea is clear?

ahh and to put it back to msf you can:

1) read data1 and search how many vbcrlfs, you add one and thats your number of rows
2) read data1 and search how many vtabs you have before a single VBCRLF, thats your number or cols.

hope this .5 cent helps.

: )
0
HTorresCommented:
why you need it in random? you query the file before saving it?
0
HTorresCommented:

with msf1
   .rows=<rowsindata>
   .cols = <fixed number of cols>
   .colsel = .cols-1
   .rowsel = .rows-1
   .clip = data1
   .colsel = 0
   .rowsel = 0
end with

:)
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

isnoend2001Author Commented:
Hi HTorres
Thanks for your reply
The reason for the random is to provide compatibily with older versions of my program that were made with Random.
I will check out the code you have provided and be back
0
HTorresCommented:

Hi again isnoend

okay, probably if you need to save the data in a fixed lenght record file, you can prepare a structure for a single row and then save row by row from the four msf in a single file
then save how many rows you have from msf1, msf2, msf3, msf4 in a separated file.

: )




0
isnoend2001Author Commented:
Hi HTorres
Thank you for your reply
I had a similiar problem last year where i combined an rtf file with random access
http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_21356359.html
and was hoping i could do something like the above here, is this possible ?
0
HTorresCommented:
yes isnoend, data1, data2, data3, data4 could be anything.

jpg, gif, rtf, xls, doc, pdf, ocx, ... what ever file or data you want.... you name it.

you just need to make sure the "||DataStart" mark wont be mistakenly recognized by that routine.

because if you use (by example) a char(0) & char(1) as datastart, you probably will get that many times in some kind of data... wich of course will cause errors or data corruption.

i emphasize to use a binary file and  000000||DataStart:<data>:dataend|| because that way <data> could take 1 byte to the limit of a string var in vb (dunno that yet, but i used ~9mb string var sometime i think)

if you use a fixed lenght record you will probably face this:
a) waste of hard disk space
b) probability to ran out of space in some record --->> data corruption.
but:
random is pretty faster than a binary file.

you can easily make a routine to "give you the data on record 10 "

record = 10
GimmeData(record)

get the pos for the first,
go to the second (first + lenght)
...
go to the tenth
return data.

this does not imply to read the whole bunch of data, just the first chars (000000||datastart:)... and once readed, you know where is the next datastart mark.

hope this helps.
 
0
isnoend2001Author Commented:
Hi HTorres
You are kinda losing me with last your comment.
I inserted your code using the .clip property
It saves fine, but when opening i get an error:
Private Sub Command3_Click()
Dim p As Integer
Dim t As String
Dim datalen
Dim Data1 As String, Data2 As String, Data3 As String, Data4 As String
Dim currentlyretrieving As Integer
Open App.Path & "\somefile" For Binary As #1
t = String(LOF(1), " ")
Get #1, 1, t
Close 1
p = 0
Do
   currentlyretrieving = currentlyretrieving + 1
   p = InStr(p + 1, t, "||DATASTART", vbTextCompare)
   If p > 0 Then
      'found one
      datalen = Val(Mid(t, p - 7, 6)) <------------------------------Invalid procedure call here
      If currentlyretrieving = 1 Then Data1 = Mid(t, p + 10, datalen)
      If currentlyretrieving = 2 Then Data2 = Mid(t, p + 10, datalen)
      If currentlyretrieving = 3 Then Data3 = Mid(t, p + 10, datalen)
      If currentlyretrieving = 4 Then Data4 = Mid(t, p + 10, datalen)
   Else
      'not found, so exit
      Exit Do
   End If
Loop
End Sub


How to put Data1,Data2 etc into grids?
I will be gone today, be back this evening

0
HTorresCommented:
what error you get?

let me rewrite the code for put data1 into grids.
0
isnoend2001Author Commented:


datalen = Val(Mid(t, p - 7, 6)) <------------------------------Invalid procedure call here
0
HTorresCommented:
sorry i didit on the fly... it had some errors

'load button

Dim p As Integer
Dim t As String
Dim datalen As Long
Dim data1 As String, data2 As String, data3 As String, data4 As String
Dim CurrentlyRetrieving As Integer
Dim CurrentlyProcessing As Integer
Dim RowsFound As Long
Dim tmpstring As String

'open
Open App.Path & "\somefile" For Binary As #1
t = String(LOF(1), " ")
Get #1, 1, t
Close 1

'retrieves
p = 0
Do
   CurrentlyRetrieving = CurrentlyRetrieving + 1
   p = InStr(p + 1, t, "||DATASTART", vbTextCompare)
   If p > 0 Then
      'found one
     
      'get datalen
      datalen = Val(Mid(t, p - 6, 6))
     
      'separate t into data1 ... data4
      If CurrentlyRetrieving = 1 Then data1 = Mid(t, p + 12, datalen)
      If CurrentlyRetrieving = 2 Then data2 = Mid(t, p + 12, datalen)
      If CurrentlyRetrieving = 3 Then data3 = Mid(t, p + 12, datalen)
      If CurrentlyRetrieving = 4 Then data4 = Mid(t, p + 12, datalen)
   Else
      'not found, so exit
      Exit Do
   End If
Loop

'find out how many rows data1 ... data4 have
CurrentlyProcessing = 0
Do
    CurrentlyProcessing = CurrentlyProcessing + 1
    If CurrentlyProcessing > 4 Then Exit Do
    p = 0
    RowsFound = 0
   
    'data1 could be an array Data(currentlyprocessing) ...
    If CurrentlyProcessing = 1 Then tmpstring = data1
    If CurrentlyProcessing = 2 Then tmpstring = data2
    If CurrentlyProcessing = 3 Then tmpstring = data3
    If CurrentlyProcessing = 4 Then tmpstring = data4
   
    Do
        p = InStr(p + 1, tmpstring, Chr(13), vbTextCompare)
       
        If p > 0 Then
            RowsFound = RowsFound + 1
        Else
            Exit Do
        End If
    Loop
   
    If CurrentlyProcessing = 1 Then Me.msf1.Rows = RowsFound + 1
    If CurrentlyProcessing = 2 Then Me.msf1.Rows = RowsFound + 1
    If CurrentlyProcessing = 3 Then Me.msf1.Rows = RowsFound + 1
    If CurrentlyProcessing = 4 Then Me.msf1.Rows = RowsFound + 1
   
Loop

With Me.msf1
    .Row = 0
    .Col = 0
    'rows = 'already set
    '.cols = 'dunno if you need to change this
    .RowSel = .Rows - 1
    .ColSel = .cols - 1
    .Clip = data1
    .RowSel = 0
    .ColSel = 0
   
End With

' and so on with msf2, 3, 4 ...
' thats it.

-----------

about your last question...

"Hi HTorres
Thank you for your reply
I had a similiar problem last year where i combined an rtf file with random access
http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_21356359.html
and was hoping i could do something like the above here, is this possible ?"

yes you can, you can put anything there
rtf files... anything.  (jpg, xls, zip, exe... )



0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
isnoend2001Author Commented:
Thanks alot HTorres
Your the man!
Works perfectly
0
HTorresCommented:
glad to help you isnoend2001,

and thank YOU for the A!!!

: )

h.
0
HTorresCommented:
just a comment,

when saving, please erase the old file before, because if the new data is smaller than the old one, it could come in data corruption.

it happens.

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.