How To Write A Recordset To File With VBA

In this video you’ll find out how to write a
recordset to a file with VBA using the Microsoft
Scripting Object.

You’ll find out how to take your
table data, and export it to a csv
formatted file so you can share it
with other applications.

Use this method to get your data
out of the normal data and use it
or upload it somewhere else.

As you may or may not know, the CSV
format is a recognizable format by nearly
every application, Apple, Linux or Windows.

I use the csv format in ASP and PHP web applications
so the viewer can export the contents to
a spreadsheet format on their computer.


'*********************************
'LoeblCom Services
'713-409-7041
'http://loeblcomservices.com
'https://vbahowto.com
'*********************************

Option Compare Database

Private Sub btnWrite_Click()
    Dim strSQL As String
    Dim rst As Recordset
    Dim strLines As String
    Dim fsoFile As Object
    Dim strPath As String
    
    
    Set fsoFile = CreateObject("Scripting.FileSystemObject")
    Dim objFile As Object

    strSQL = "SELECT * FROM tblProject"
    
    Set rst = CurrentDb.OpenRecordset(strSQL)
    
    Do While Not rst.EOF
        strLines = strLines & rst.Fields("proj_Wage") & ","
        strLines = strLines & rst.Fields("proj_TotalMaterial") & ","
        strLines = strLines & rst.Fields("proj_UsedCost") & ", "
        strLines = strLines & rst.Fields("proj_Cost") & ", "
        strLines = strLines & rst.Fields("proj_SupplierCost") & ", "
        strLines = strLines & rst.Fields("proj_Unit") & ", "
        strLines = strLines & rst.Fields("proj_SupplierZip") & ", "
        strLines = strLines & rst.Fields("proj_ProjectType") & ", "
        strLines = strLines & rst.Fields("proj_Hours") & vbCrLf
        
        
        rst.MoveNext
    Loop
    
    strPath = CurrentProject.Path & "\project_data.csv"
    
    Set objFile = fsoFile.CreateTextFile(strPath)
    
    objFile.WriteLine strLines
    objFile.Close
    
    Set fsoFile = Nothing
    Set objFile = Nothing
    
    
    Set rst = Nothing
    
    MsgBox "Done!"

End Sub



Click here for the file:
write-recordset-to-file-with-vba.mdb

applications.




By the way, if you got or are getting value from the VBA information, please give me a tip, thanks!


These posts may help answer your question too...

How To Parse A Flat File In Excel VBA

In another post I demonstrated how to access a file on your computer using the MS Office Library. Here it is if you don’t know what I’m talking about. In this post, I am going to show you how to access the file and load it into your spreadsheet. I will do the same thing […]

How can I interact with other Office applications (Excel) using VBA in Access?

Need to write your Access data or query to an Excel file? Here is the how to do it: Most people are familiar with Excel and know how to use it well (enough), and when you start talking about Access, they get scared off, and don’t know what to do anymore. Well, here you are […]

How To Generate A XML File With Access VBA

XML is used to structure your data. You can send a CSV file to another person, but using XML, you can structure your data in a more elegant format, and get very specific about your fields, and what data is contained within your tags (your field names). This is different than a basic CSV file […]

Mouse Click Counter On Access Form

This post will demonstrate how you can count the number of clicks on your button in a certain time frame. It will function like a game . Here is the database form all in one: Option Compare Database Public m_dteStartTime As Date Public m_dteStopTime As Date Private Sub btnClicker_Click() Dim intValue As Integer Dim rst […]

Previous Post

MS Access Hiding Text Boxes Based On User Selection VBA

Next Post

VBA Building Dynamic SQL