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

RESULTS FROM qry_Export

Footer: Example Text
wilpitzAsked:
Who is Participating?
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
    
    rs.MoveFirst
    Do Until rs.EOF
        'Debug.Print rs!Category
        Print #1, rs!CategoryID & vbTab & rs!ManufacturerID & vbTab & rs!Category
        rs.MoveNext
    Loop
    
Exit_TestWriteToFile:
    Close #1
    Set rs = Nothing
    Exit Sub
    
Err_TestWriteToFile:
    MsgBox Err.Number & ", " & Err.Description, , "Error"
    Resume Exit_TestWriteToFile
    
End Sub

Open in new window

0

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"

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

Print #1, "Footer: Example Text"
0
wilpitzAuthor Commented:
Worked!!! Changed the select to the query I am using and changed the field and the code worked great!!!
0
omgangIT ManagerCommented:
Excellent.  I've used the same concept to construct html web pages, etc.
OM Gang
0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.