Results 1 to 13 of 13

Thread: [RESOLVED] Best way to export text to a CSV file?

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2005
    Location
    Poulsbo, WA
    Posts
    60

    Resolved [RESOLVED] Best way to export text to a CSV file?

    Okay, I have my "updated" VB6 app working. Next step is to export certian fields into a CSV so they can be imported into an Access database.

    Background -
    The VB app hashes a text file something like this... The file is fixed width, and 3 or 4 characters on the right side get read.

    EXAMPLE
    Select Case cc
    Case "DA" To "DD"
    NoSupport$ = "yes"
    skipcnt2% = skipcnt2% + 1
    Case "XA" To "XJ"
    NoSupport$ = "yes"
    skipcnt2% = skipcnt2% + 1
    Nsnap.SF43 = "Card Type not supported " & cctext
    Nsnap.Narr = RTrim(Nsnap.Narr) & "[" & Mid(textdata, 18, 63) & "]" &CRLF$ & " "
    Case "YX" To "YZ"
    NoSupport$ = "yes"
    skipcnt2% = skipcnt2% + 1
    Nsnap.SF43 = "Card Type not supported " & cctext
    Nsnap.Narr = RTrim(Nsnap.Narr) & "[" & Mid(textdata, 18, 63) & "]" & CRLF$

    This then gets hacked down into seperate fields displayed on a form based on number of characters and position in the line(I believe)

    EXAMPLE
    SF1 As String * 4 ' 2 Work Center
    SF2 As String * 4 ' 3 Job Seq No
    SF3 As String * 11 ' 4 APL / AEL
    SF4 As String * 19 ' 5 Equip Noun Name
    SF5 As String * 1 ' 6 WND - When Discovered Date
    SF6 As String * 1 ' 7 STA - Status

    Any suggestions as the best way to extract specific fieldsfrom specific records(as they are displayed) to add to a CSV formatted file?

    John

  2. #2
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Best way to export text to a CSV file?

    I would split the data into arrays, then you can loop thru
    I would use WRITE to add the fields to a text file. It will add quotes, and pound signs where required, and also add commas between the variables.
    VB Code:
    1. dim ff as integer, x as integer
    2. ff=freefile
    3. open app.path & "\outfile.txt" for output as #ff
    4. for x=0 to ubound(myarray)
    5.   write #ff, myarray1(x), myarray2(x)
    6. next x
    7. close #ff

  3. #3

    Thread Starter
    Member
    Join Date
    Jun 2005
    Location
    Poulsbo, WA
    Posts
    60

    Re: Best way to export text to a CSV file?

    Array not needed. I should have said that only a few of the displayed fields would need to be put into the file.

    BUT, this does put me on the right track !

    Just because I want to put a rubber ducky into a post -

    John

  4. #4

    Thread Starter
    Member
    Join Date
    Jun 2005
    Location
    Poulsbo, WA
    Posts
    60

    Re: Best way to export text to a CSV file?

    Okay, I'm even less experienced that I orginally thought. What I tried didn't work...

    What is the proper way to open say, Wordpad, and put the selected fields in there. Comma seperated of course, appending a new line each time instead of overwriting.

  5. #5
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Best way to export text to a CSV file?

    if u use append instead of output it will add to the file..
    its faster to do it like this that to try to manipulate wordpad etc...
    once its done then maybe open wordpad..

    open app.path & "\outfile.txt" for append as #ff
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  6. #6

    Thread Starter
    Member
    Join Date
    Jun 2005
    Location
    Poulsbo, WA
    Posts
    60

    Re: Best way to export text to a CSV file?

    So I should open the file, and specify the text to add like this -

    Dim ff As String, x As Integer
    ff = FreeFile
    Open App.Path & "\outfile.txt" For Append As #ff
    'For x = 0 To UBound(ff)
    FreeFile = Form1.fieldboxes(0) & "," & Form1.fieldboxes(1)

    Write #ff,
    'Next x
    Close #ff

  7. #7
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Best way to export text to a CSV file?

    Try this:

    VB Code:
    1. Dim ff As Integer
    2. ff = FreeFile
    3. Open App.Path & "\outfile.txt" For Append As #ff
    4. Write #ff,  Form1.fieldboxes(0), Form1.fieldboxes(1)
    5. Close #ff

    ff is the filenumber, which you need for the Open, Close, and Write statements. FreeFile determines the next one that you can use.

  8. #8

    Thread Starter
    Member
    Join Date
    Jun 2005
    Location
    Poulsbo, WA
    Posts
    60

    Re: Best way to export text to a CSV file?

    Ahh, I see.

    And the method to make the filepath user defined is? I'm new to VB, can you tell?

    John

  9. #9
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Best way to export text to a CSV file?

    App.Path puts in wherever the program is located. You can hard code the path, by leaving app.path off and adding the path with the filename.

    "c:\temp\outfile.txt"

    or did you want the user to pick a file location?
    Take a look at this example. It's one way to do it.

    http://vbforums.com/attachment.php?attachmentid=40809

  10. #10

    Thread Starter
    Member
    Join Date
    Jun 2005
    Location
    Poulsbo, WA
    Posts
    60

    Re: Best way to export text to a CSV file?

    Thanks for the info!

    But you know something, now that I think about it,the user will be adding to this CSV file irregularly while using this application. I should probably hard code the destination file.

    John

  11. #11
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Best way to export text to a CSV file?

    just put the file in the same location as the app and use App.Path & "\outfile.txt"
    then, no matter where the user puts the app.. the outfile will be created there & added to there
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  12. #12
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Best way to export text to a CSV file?

    You might also want to let the user KILL the file from time to time. Append will create it if it's not there, though.

  13. #13

    Thread Starter
    Member
    Join Date
    Jun 2005
    Location
    Poulsbo, WA
    Posts
    60

    Lightbulb Re: Best way to export text to a CSV file?

    Good ideas.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width