Results 1 to 8 of 8
  1. #1
    5 Star Lounger
    Join Date
    Jan 2001
    Location
    Vancouver, Br. Columbia, Canada
    Posts
    632
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Add lines to Text File (Acc97 SR2)

    The application needs to generate a plain-ASCII text file for a 3rd party app. I use the DoCmd.TransferText method to generate the text file. The method is invoked from a command-button on the form, using a recordset/query that is managed by the form. Works fine. However, the 3rd party app also requires two lines of header text at the beginning of the file -- they are not part of the query that forms the body of the text file.

    How do I add the two lines of text into the file using VBA?

    TIA
    --------------------------------------------------
    Jack MacDonald
    Vancouver, Canada

  2. #2
    Gold Lounger
    Join Date
    Jun 2001
    Location
    Crystal Beach, FL, Florida, USA
    Posts
    3,436
    Thanks
    1
    Thanked 34 Times in 34 Posts

    Re: Add lines to Text File (Acc97 SR2)

    Look for "Write Statement" in Help. I'd offer more help, but I've only done it about once myself, and I'd have to go look it up all over again!
    Mark Liquorman
    See my website for Tips & Downloads and for my Liquorman Utilities.

  3. #3
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Add lines to Text File (Acc97 SR2)

    Write # just adds data to a sequential file. To actually manipulate the location of the record, you need to use the Open Statement with the Random keyword and use the Put statement to insert the record. I've used this method in VB but never needed it in Access.

    In this case, you're probably going to need to write the two lines into a second file using the Put statement and then append the first file to the second. The alternative would be to read the entire first file into an array and write it to the second file one record at a time using Put. <img src=/S/shrug.gif border=0 alt=shrug width=39 height=15>
    Charlotte

  4. #4
    5 Star Lounger
    Join Date
    Jan 2001
    Location
    Vancouver, Br. Columbia, Canada
    Posts
    632
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Add lines to Text File (Acc97 SR2)

    Thanks for the info. That's given me enough to start looking in the right place.
    --------------------------------------------------
    Jack MacDonald
    Vancouver, Canada

  5. #5
    Gold Lounger
    Join Date
    Jun 2001
    Location
    Crystal Beach, FL, Florida, USA
    Posts
    3,436
    Thanks
    1
    Thanked 34 Times in 34 Posts

    Re: Add lines to Text File (Acc97 SR2)

    Maybe I misunderstood what Put does. I didn't think it inserted records between records, rather that it wrote a record at the record number indicated, overwriting what was there?

    I would use Write to create a Header file, use Transfer text to create a Data file, then use Copy command to create final output file:
    Copy Header.txt + Data.txt Output.txt
    Mark Liquorman
    See my website for Tips & Downloads and for my Liquorman Utilities.

  6. #6
    5 Star Lounger
    Join Date
    Jan 2001
    Location
    Vancouver, Br. Columbia, Canada
    Posts
    632
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Add lines to Text File (Acc97 SR2)

    Almost there -- but the output is being wrapped in additional quotation marks. How do I suppress the quotation marks? The code comments show what I expected/wanted to see and what was actually generated.

    Code snippet:

    ' put the required headers into a file
    If Dir(sFilePathPart & ".PML") <> "" Then Kill (sFilePathPart & ".PML")
    Open sFilePathPart & ".PML" For Output As #1
    sTitle = InputBox("Enter a title for the distribution list")
    Write #1, "TITLE " & sTitle ' expected: TITLE fred BUT got "TITLE fred"
    Write #1, "SENDER " & Chr(34) & "FERIC" & Chr(34) & " <lists>" ' expected: SENDER "FERIC" <lists>
    Write #1, "NOSIG Y" ' expected: NOSIG Y BUT got "NOSIG Y"

    ' read the data from the generated file into the destination file
    Open sfilename For Input Access Read As #2
    Do While Not EOF(2)
    Input #2, sTitle
    Write #1, sTitle ' expected exactly same string as read from file.
    Loop

    Close #1
    Close #2

    Thanks in advance.
    --------------------------------------------------
    Jack MacDonald
    Vancouver, Canada

  7. #7
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Add lines to Text File (Acc97 SR2)

    Yes, that's exactly what Put does unless you give it the necessary instructions to add a record at the *end* of the file. However, if you load the entire text file into an array, you can Put them back in any order you please.
    Charlotte

  8. #8
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Add lines to Text File (Acc97 SR2)

    You're writing strings that are quote delimited, so naturally they include quotes. Your file header isn't actually text, is it? You're trying to write a path or something like it. How is the header actually supposed to be used? I'm not sure you can write an undelimited string like that to a text file because I've never tried it. If the header is the same every time, you might try finding a way to build the header and save it, then reuse it each time with a new output file.
    Charlotte

Posting Permissions

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