DAO Recordset Export File

Littlemalky

Board Regular
Joined
Jan 14, 2011
Messages
223
So I found this code to use to export my query as a text file to a file location. It's awesome and efficient and fast, but it's structured as an "Append". I'm not familiar enough with this kind of code to know how to convert it over to just replace the file each time. I want to overwrite it with new contents, not append.

Does anyone know how to modify this?

Code:
Private Sub btnEXPORT_TEXT_FILE_Click()
    Dim rs As DAO.Recordset, strRec As String, j As Integer
    Set rs = CurrentDb.OpenRecordset("UNION_ADDITIONS_ACCRUALS_LOAD_FILES")
    Open "[URL="file://\\cobalt\public\SCC\CAPEX\Test\UNION_ADDITIONS_ACCRUALS_LOAD_FILES.txt"]...\Test\UNION_ADDITIONS_ACCRUALS_LOAD_FILES.txt[/URL]" For Append As #1
    Do Until rs.EOF
        For j = 1 To rs.Fields.Count - 1
            If strRec = "" Then
            strRec = rs(j) & vbTab
            Else
            strRec = strRec & rs(j) & vbTab
            End If
        Next
        Print #1, strRec
        rs.MoveNext
        strRec = ""
Loop
Close #1
    MsgBox "Export Complete!"
End Sub
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Try changing Append to Output.

By the way, I think there might be a way/ways to do this without looping.
 
Upvote 0
For that you would either need to alter the existing query, use a SELECT statement to only return the required fields from the existing query or use code to only extract the fields you want.
 
Upvote 0
So this is working great now, except for that fact that it's cutting off my first field. I'm only getting 3 out of 4 fields. Any advise?
 
Upvote 0
Can you post your current code?
 
Upvote 0
Yes, sorry.

Code:
<code>

Private Sub btnEXPORT_TEXT_FILE_Click()
    Dim rs As DAO.Recordset, strRec As String, j As Integer
    Set rs = CurrentDb.OpenRecordset("UNION_ADDITIONS_ACCRUALS_LOAD_FILES")
    Open "[URL="file://\\cobalt\public\SCC\CAPEX\Test\UNION_ADDITIONS_ACCRUALS_LOAD_FILES.txt"]...\Test\UNION_ADDITIONS_ACCRUALS_LOAD_FILES.txt[/URL]" For Output As #1
    Do Until rs.EOF
        For j = 1 To rs.Fields.Count - 1
            If strRec = "" Then
            strRec = rs(j) & vbTab
            Else
            strRec = strRec & rs(j) & vbTab
            End If
        Next
        Print #1, strRec
        rs.MoveNext
        strRec = ""
Loop
Close #1
    MsgBox "Export Complete!"
End Sub
<code></code>
</code>
 
Upvote 0

Forum statistics

Threads
1,215,866
Messages
6,127,403
Members
449,382
Latest member
DonnaRisso

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top