Solved

how to use vb6 console code to read a structured record and its members

Posted on 2011-09-28
4
224 Views
Last Modified: 2012-05-12
I have need to read a fixed length file (text file) and then parsed out to various members/fields within that structure which made up the total record length.  This is pretty easy in other language, but I need to use VB6 to do that.  - example

Type hdr     ' rec length = 3 + 8 + 6 = 17
    rec_type As String * 3       ' 1st field
    create_date As String * 8  ' 2nd field
    create_time As String * 6  ' 3rd field    
End Type
Global hdr_buf As hdr
dim s1 as string
dim ch_in as integer

ch_in = freefile()
open "test_file.txt" for input as #ch_in      ' as input file

Line Input #ch_in, s1    ' read the first line
' /* assign what read string s1 into the hdr_buf

        hdr_buff = s1        ' this is my intention, but I get mistype error.   need help how to do it right

' so I can use the data element within this records

       debug.print hdr_buff.rec_type
       debug.print hdr_buf.create_date

can some of your experts help?

thanks

   
0
Comment
Question by:mshox1
  • 2
4 Comments
 
LVL 11

Accepted Solution

by:
kbirecki earned 300 total points
ID: 36716454
Instead of hdr_buf = s1, do this:
hdr_buf.rec_type = left(s1,3)
hdr_buf.create_date = mid(s1,4,8)
hdr_buf.create_time = mid(s1,12,6)

Open in new window

0
 
LVL 5

Assisted Solution

by:DerZauberer
DerZauberer earned 200 total points
ID: 36716523
Maybe you are looking for the vb functions for fixed record length read and write operations:

Type hdr
    rec_type As String * 3
    create_date As String * 8
    create_time As String * 6
End Type

Sub Sample()
    
    Dim ch_in As Integer
    Dim b As hdr
    Dim c As hdr
    
    ch_in = FreeFile()
    Open "D:\\TEMP\\Test.bin" For Random As #ch_in
    b.rec_type = "REC"
    b.create_date = "2011-01-01"
    b.create_time = "10:00"
    Put #1, , b
    b.create_date = "2011-01-02"
    b.create_time = "11:00"
    Put #1, , b
    Close #ch_in
    Open "D:\TEMP\Test.bin" For Random As #ch_in
    Get #ch_in, , c
    Debug.Print c.create_date
    Debug.Print c.create_time
    Get #ch_in, , c
    Debug.Print c.create_date
    Debug.Print c.create_time
    Close #ch_in
End Sub

Open in new window

0
 
LVL 5

Assisted Solution

by:DerZauberer
DerZauberer earned 200 total points
ID: 36716551
Or better add record length specifier:

    Open "D:\\TEMP\\Test.bin" For Random As #ch_in Len = Len(b)
...
    Open "D:\TEMP\Test.bin" For Random As #ch_in Len = Len(b)

Open in new window


Ah and my date format was too long ^^
0
 

Author Closing Comment

by:mshox1
ID: 36938768
thank you for all.
0

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

We were having a lot of "Heartbeat Alerts" in our SCOM environment, now "Heartbeat" in a SCOM environment for those of you who might not be familiar with SCOM is a packet of data sent from the agent to the management server on a regular basis, basic…
The new Microsoft OS looks great, is easier than ever to upgrade to, it is even free.  So what's the catch?  If you don't change the privacy settings, Microsoft will, in accordance with the (EULA) you clicked okay to without reading, collect all the…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…

770 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