Link to home
Start Free TrialLog in
Avatar of isnoend2001
isnoend2001Flag for United States of America

asked on

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
Avatar of HTorres
HTorres

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.

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

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

:)
Avatar of isnoend2001

ASKER

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

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.

: )




Hi HTorres
Thank you for your reply
I had a similiar problem last year where i combined an rtf file with random access
https://www.experts-exchange.com/questions/21356359/Add-rtf-file-to-random-access.html
and was hoping i could do something like the above here, is this possible ?
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.
 
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

what error you get?

let me rewrite the code for put data1 into grids.


datalen = Val(Mid(t, p - 7, 6)) <------------------------------Invalid procedure call here
ASKER CERTIFIED SOLUTION
Avatar of HTorres
HTorres

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks alot HTorres
Your the man!
Works perfectly
glad to help you isnoend2001,

and thank YOU for the A!!!

: )

h.
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.