VB6 Loading from a test file

Posted on 2011-04-28
Last Modified: 2012-06-27
I have a text file which I write to, it takes the following format

Random Text - line 1
User defined type - line 2
User defined type - line 2
User defined type - line 3

I want to be able to just read the file in with user defined types only, so what I did was read in the file, exclude the first line then write the rest to a temp file which II then read in as follows

       Open sTempFilename For Random Access Read As #1 Len = Len(uExport)
        i = 0
        While Not EOF(1)

I checked that when I wrote to the temp file the length of each line is 511, however the length of the user defined type is 512, the consequence of this is that when I am reading in the types from the temporary file after line 1 they start to go one character out.

Hope that makes sense, any help would be appreciated

Question by:rocky050371
    LVL 76

    Expert Comment

    'Lines' implies text files with one or more line termination characters (line return and/or line feed). Binary and Random access do not expect this.

    It sounds as if your UDT includes one more such characters than the file. Obviously you could try shortening your UDT by one character, and see what happens.
    LVL 76

    Expert Comment

    What was the code to create the temporary file?

    Author Comment

       Open sTempFilename For Output As #2
            While Not EOF(1)
                If i = 0 Then
                    i = 1
                    Input #1, sTemp
                    Input #1, sTemp
                    Print #2, sTemp
                End If
    LVL 16

    Expert Comment

    Can you provide any sample data and a copy of your UDT so that we can see where the two don't match?

    Otherwise, do you REALLY need to have the file open for RANDOM access?  It seems a pretty huge waste to create a temp file just to read the data.  I would think you should be able to access the file by openning it for BINARY Access.  That way, you can to a "Line Input" command to read the 1st text line, then use GET with no record number and your UDT to get the records (Get #1,,UDT).
    LVL 27

    Accepted Solution

    Smth like this?
    Dim strData as string
    Open YourFile For Binary As #1
        strData = String(LOF(1),0)
        Get #1,,strData
    Close #1
    Dim arrData() As String
    Dim i As Long
    Open YourTempFile For OutPut As #1
       For I=1 To Ubound(arrData)
            Print #1,arrData(i)
    Close #1

    Open in new window


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Suggested Solutions

    Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
    Article by: Martin
    Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
    Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
    This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

    794 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

    14 Experts available now in Live!

    Get 1:1 Help Now