Solved

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

Posted on 2011-09-28
4
220 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
thank you for all.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

As with any other System Center product, the installation for the Authoring Tool can be quite a pain sometimes. This article serves to help you avoid making these mistakes and hopefully save you a ton of time on troubleshooting :)  Step 1: Make sur…
Many companies are making the switch from Microsoft to Google Apps (https://www.google.com/work/apps/business/). Use this article to learn more about what Google Apps has to offer and to help if you’re planning on migrating to Google Apps. It is …
Viewers will learn how to maximize accessibility options in an Excel workbook for users with accessibility issues.
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…

743 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now