?
Solved

First time text file read/write user...vb6

Posted on 2004-11-04
7
Medium Priority
?
105,651 Views
Last Modified: 2012-05-05
Keeping text file closed, I want to start with basic steps to buid a routine to read from a text file and later write to it.  I want solid operation and want some info on text file format.  Is data shown below a good option:
Field1, Field2, Field3
1,12,A
2,34,C
3,36,E

Thanks
0
Comment
Question by:Mike Eghtebas
7 Comments
 
LVL 5

Assisted Solution

by:brianb99999
brianb99999 earned 600 total points
ID: 12498730
Standard flat file Read and write routines:

Read:
    Dim intFileHandle As Integer
    Dim strRETP As String
    intFileHandle = FreeFile
    Open "path to file" For Input As #intFileHandle
    Do While Not EOF(intFileHandle)
        Line Input #intFileHandle, strRETP
        MsgBox strRETP
    Loop
    Close #intFileHandle

Write (creates a new file overwriting any existing file):
    Dim intFileHandle As Integer
    Dim strRETP As String
    strRETP = "Hi There"
    intFileHandle = FreeFile
    Open "path to file" For Output As #intFileHandle
    Print #intFileHandle, "---------------------------"
    Print #intFileHandle, strRETP
    Print #intFileHandle, "---------------------------"
    Close #intFileHandle

Write (appends data to an existing file):
    Dim intFileHandle As Integer
    Dim strRETP As String
    strRETP = "Hi There"
    intFileHandle = FreeFile
    Open "path to file" For Append As #intFileHandle
    Print #intFileHandle, "---------------------------"
    Print #intFileHandle, strRETP
    Print #intFileHandle, "---------------------------"
    Close #intFileHandle

Brian.
0
 
LVL 1

Assisted Solution

by:wehoit
wehoit earned 400 total points
ID: 12499188
A few comments on Brian's answer:

1) You should use "Write" instead of "Print" to output comma separated values. e.g.:
Dim iOne as integer, iTwo as integer, iThree as String
iOne=1
iTwo=12
iThree="A"
...
Write #intFileHandle, iOne,iTwo,iThree
...
Gives this output:
1,12,A

etc.


2) You should then use "Input" not "Line Input" to read the comma separated values:
Dim xOne as integer, xTwo as integer, xThree as String
...
Input #intFileHandle, xOne,xTwo,xThree
...

This reads each separated value into the specified variables...


0
 
LVL 16

Accepted Solution

by:
jimbobmcgee earned 1000 total points
ID: 12506034
You don't have to use Write instead of Print -- if you use Write, it will enclose all text strings in "quote marks" and all dates in #hashes#.  This can be useful, as it eliminates the error that will occur in a CSV if text you want to write contains commas and allows you to put date values straight into a Date variable, but may not be what you want.  Print is a WYSIWYG output system.

Similarly, you don't have to use Input instead of Line Input.  Input will read until it reaches a comma or a new line.  Line Input will read until it reaches a new line only.  You can use Line Input to read the whole line and split this line with thisArray = Split(strLineJustRead, ","), to get comma-separated values in one chunk.

Personally, if I'm writing a CSV-style flat file, I prefer to use a different delimeter, such as pipe (|) -- this prevents the CSV being confused by any commas that may be deliberately in the text, e.g.

    Print #filenum, "this text, here, has a comma|54455|223"

Another thing to note is, when writing multiple data items to the file, if you don't want to insert a new line, follow your Print line with a semicolon (;), e.g.:

    Print #filenum, "I want the text in this string to be on the same line -> ";
    Print #filenum, myString;
    Print #filenum, " But I want a new line now."             'NO ; MOVES TO NEW LINE

The Open/Close method is an older method -- it is often favourable to use the object orientated approach, nowadays.  To do this, add a reference to the Microsoft Scripting Runtime (you don't have to but it makes life so much easier) and try the following (converting brianb99999's code, from above):

    Dim oFSO As New Scripting.FileSystemObject
    Dim oTS As Scripting.File
    Dim strRetp As String

    '''' READ ROUTINE ''''''''''''''''''''''''''''''''''''''''''''''''''''
    Set oTS = oFSO.OpenTextFile("x:\mypath\myfile.ext", ForReading)

          Do Until oTS.AtEndOfStream

               strRetp = oTS.ReadLine
               MsgBox strRetp

          Loop

          oTS.Close

    Set oTS = Nothing
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''


    '''' WRITE ROUTINE ''''''''''''''''''''''''''''''''''''''''''''''''''''
    strRept = "Hi there"
    Set oTS = oFSO.OpenTextFile("x:\mypath\myfile.ext", ForWriting, True)

          oTS.WriteLine "---------------------------"
          oTS.WriteLine strRetp
          oTS.WriteLine "---------------------------"
          oTS.Close

    Set oTS = Nothing
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''


    '''' APPEND ROUTINE '''''''''''''''''''''''''''''''''''''''''''''''''
    strRept = "Hi there"
    Set oTS = oFSO.OpenTextFile("x:\mypath\myfile.ext", ForAppending, True)

          oTS.WriteLine "---------------------------"
          oTS.WriteLine strRetp
          oTS.WriteLine "---------------------------"
          oTS.Close

    Set oTS = Nothing
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

This object-orientated approach also allows you to write Unicode (good for foreign characters).  You can use some other methods to read and write, with this approach, also:

    .Read(numberofcharacters)
    .ReadAll
    .Write "string to write"    '<-- NO NEW LINE
   
Look up the File System Object, for more info, here:

     http://msdn.microsoft.com/library/en-us/script56/html/FSOoriFileSystemObject.asp

HTH

J.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 34

Author Comment

by:Mike Eghtebas
ID: 12506340
brianb99999, I started with your code.  Now, I am working through.

wehoit, Thank you for the tips.

jimbobmcgee, Wow, very impressive.  As a beginer to txt file, this is kind of feedback I was hoping for.

Q1: If I use MyArray = Split(strLineJustRead, "|")
Do I need to have Dim MyArray(10) As Variant prior to this code for Split to work and produce:

MyArray(1)="A"
MyArray(1)=1234
MyArray(1)=#1/1/2005#
etc. ?

Q2: In case of a memo field, what is the max. string lenght I can handle with text file.  I wonder if wrap feature avaialbe here or I am limitted to say 255 caracters?

Regards,

Mike
0
 
LVL 34

Author Comment

by:Mike Eghtebas
ID: 12507320
I figured the answer for Q1 from last post.

Thanks
0
 
LVL 34

Author Comment

by:Mike Eghtebas
ID: 12510594
0
 

Expert Comment

by:dwieringa
ID: 14635745
In jimbobmcgee's post, I believe oTS should be defined as a Scripting.TextStream instead of Scripting.File
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
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 Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…
Suggested Courses

840 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