Export text file with multiple lines of hard coded text, then results of query, then text

I need to built a text file that combines lines of static text then results of a query, then one last row of static text at the bottom. Below is an example of the rough outline of what the template would be. I am guessing a module would be the best way to do this but I am unsure of how to do this.

Line 1: Example Text
Line 2: Example Text
Line 3: Example Text


Footer: Example Text
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

omgangIT ManagerCommented:
Here's a sample I had in a TEST db.  It provides example of how to do what you want.
OM Gang

Public Sub TestWriteToFile()
On Error GoTo Err_TestWriteToFile

    Dim rs As ADODB.Recordset
    Set rs = New ADODB.Recordset
    rs.Open "SELECT * FROM tblCategories", CurrentProject.Connection, _
            adOpenStatic, adLockReadOnly
    Open "C:\Temp\TestWriteToFile.txt" For Output As #1
    Do Until rs.EOF
        'Debug.Print rs!Category
        Print #1, rs!CategoryID & vbTab & rs!ManufacturerID & vbTab & rs!Category
    Close #1
    Set rs = Nothing
    Exit Sub
    MsgBox Err.Number & ", " & Err.Description, , "Error"
    Resume Exit_TestWriteToFile
End Sub

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
omgangIT ManagerCommented:
To add static text before after

Print #1, "Line 1: Example Text"
Print #1, "Line 2: Example Text"
Print #1, "Line 3: Example Text"

    Do Until rs.EOF
        'Debug.Print rs!Category
        Print #1, rs!CategoryID & vbTab & rs!ManufacturerID & vbTab & rs!Category

Print #1, "Footer: Example Text"
wilpitzAuthor Commented:
Worked!!! Changed the select to the query I am using and changed the field and the code worked great!!!
omgangIT ManagerCommented:
Excellent.  I've used the same concept to construct html web pages, etc.
OM Gang
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.