Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

MS Access 2003 or later To MySQL Migration Project Hello All, this is my second article in the category of MS-OFFICE Automation. In internet I am not able to find any comprehensive resource on the Migration of MS Access back-end to MySQL so I fin…
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 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 create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …

721 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