Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2011-09-28
4
Medium Priority
?
237 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 1200 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 800 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 800 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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

This very simple solution applies to a narrow cross-section of the "needs to close" variety. In this case, the full message in Event Viewer was in applog, Event ID 1000: Faulting application iexplore.exe, version 8.0.6001.18702, faulting module …
Microsoft's Excel has many features that most people will never need nor take advantage of.  Conditional formatting is one feature that you may find a necessity once you start using it.
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…
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…

877 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