Solved

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

Posted on 2011-09-28
4
230 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Percona Monitoring and Management and Grafana

Proactive monitoring is vital to a highly-available environment. We have a quick start guide on Experts Exchange for Grafana users.

Question has a verified solution.

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

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 …
The System Center Operations Manager 2012, known as SCOM, is a part of the Microsoft system center product that provides the user with infrastructure monitoring and application performance monitoring. SCOM monitors:   Windows or UNIX/LinuxNetwo…
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
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…
Suggested Courses

632 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