?
Solved

Combine 4 mshflexgrids with random access

Posted on 2006-04-24
14
Medium Priority
?
187 Views
Last Modified: 2010-05-01
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
0
Comment
Question by:isnoend2001
  • 9
  • 5
14 Comments
 
LVL 4

Expert Comment

by:HTorres
ID: 16531311
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
 
LVL 4

Expert Comment

by:HTorres
ID: 16531314
why you need it in random? you query the file before saving it?
0
 
LVL 4

Expert Comment

by:HTorres
ID: 16531337

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

:)
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:isnoend2001
ID: 16531415
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
 
LVL 4

Expert Comment

by:HTorres
ID: 16531576

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
 

Author Comment

by:isnoend2001
ID: 16531613
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
 
LVL 4

Expert Comment

by:HTorres
ID: 16531953
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
 

Author Comment

by:isnoend2001
ID: 16534944
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
 
LVL 4

Expert Comment

by:HTorres
ID: 16534980
what error you get?

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

Author Comment

by:isnoend2001
ID: 16535196


datalen = Val(Mid(t, p - 7, 6)) <------------------------------Invalid procedure call here
0
 
LVL 4

Accepted Solution

by:
HTorres earned 2000 total points
ID: 16540418
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
 

Author Comment

by:isnoend2001
ID: 16541122
Thanks alot HTorres
Your the man!
Works perfectly
0
 
LVL 4

Expert Comment

by:HTorres
ID: 16544370
glad to help you isnoend2001,

and thank YOU for the A!!!

: )

h.
0
 
LVL 4

Expert Comment

by:HTorres
ID: 16544384
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

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question