• Add lines to Text File (Acc97 SR2)

    Author
    Topic
    #361535

    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

    Viewing 0 reply threads
    Author
    Replies
    • #546801

      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!

      • #546838

        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. shrug

        • #546846

          Thanks for the info. That’s given me enough to start looking in the right place.

          • #546853

            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) & ” ” ‘ expected: SENDER “FERIC”
            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.

            • #546871

              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.

        • #546852

          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

          • #546869

            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.

    Viewing 0 reply threads
    Reply To: Add lines to Text File (Acc97 SR2)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: