Full Version: Export Report To Multiple Text Files
UtterAccess Discussion Forums > Microsoft® Access > Access Reports
ChrisSymonds
Hi,

I've been asked to help move data from one HR db to another, Part of this is to take data from a memo field in db1 and put each one into a seperate text file.
So db1 has roughly this structure:
tblEmployeeData
Record 1 UserID1 UserResume1
Record 2 UserID2 UserResume2

and get:
UserID1.txt containing UserResume1
UserID2.txt containing UserResume2

Is there a way to do that?

TIA

Chris
theDBguy
Hi Chris,

What version of Access are you using? Please remember to select the version number when posting questions in case it becomes relevant to the discussion.

Yes, there are several ways to do that. For example, you can use the Open statement to write the text into file, or you can use the TransferText method to export the data into a text file.

Just my 2 cents... 2cents.gif
ChrisSymonds
Sorry, it is 2010. I've never used either of those methods; I'll research them. thanks for the hint


Chris
theDBguy
Hi Chris,

yw.gif

Let us know if you get stuck. Good luck!
Larry Larsen
Hi Chris
Here is a quickly modified routine that often use when I need to break up a recordset of data into seperate data files(txt)..

CODE
Private Sub fx()
Dim rst        As DAO.Recordset
Dim db         As DAO.Database
Dim strSQL     As String

    Set db = CurrentDb
    strSQL = "SELECT DISTINCT RecID,UserID FROM tblEmployeeData"
    Set rst = db.OpenRecordset(strSQL, dbOpenSnapshot)

    With rst
        Do Until .EOF
            strSQL = "(SELECT UserID,UserResume FROM tblEmployeeData WHERE RecID = " & !RecID & ")"
            ExportToCSV_A strSQL, "C:\temp\" & !UserID & ".txt", , False
            .MoveNext
        Loop
    End With

    Set rst = Nothing
End Sub

--------------------------------------------------
Private Sub Command1_Click()
    Call fx
End Sub
-------------------------------------------------

Public Function ExportToCSV_A(strSource As String, _
                              strFilename As String, _
                              Optional strColumnDelimiter As String = ",", _
                              Optional blHeaders As Boolean = False) As Byte
'Exports a table or query or SQL statement to a text file.  If a SQL is passed
'as the source, enclose it in Parenthesis.

Dim intChannel As Integer
Dim strSQL     As String
Dim strHeaders As String
Dim x          As Integer

    'Close any open files
    For intChannel = 1 To 511
        Close #intChannel
    Next intChannel

    'Open a channel to communicate with your TEMP file and
    intChannel = FreeFile
    Open strFilename For Output Access Write As #intChannel

    'Write the contents of the table to the file
    'Open the source
    strSQL = "SELECT * FROM " & strSource & " As vTbl"

    With CurrentProject.Connection.Execute(strSQL, , 1)    'adCmdText = 1

    'Build Headers if appropriate
        If blHeaders = True Then
            For x = 0 To .Fields.Count - 1
                strHeaders = strHeaders & strColumnDelimiter & .Fields(x).Name
            Next
            strHeaders = Mid(strHeaders, Len(strColumnDelimiter) + 1) & vbCrLf
        End If

    'Write to the CSV file
        Print #intChannel, strHeaders & .GetString(2, , strColumnDelimiter, vbCrLf, "<NULL>")    'adClipString = 2

    End With

    'Close all the files
    Close #intChannel

End Function


HTH's
thumbup.gif
ChrisSymonds
Larry,

Thanks so much, you've saved me hours of work

Regards,

Chris
Larry Larsen
Hi Chris

I didn't intentionally want to steer you away from thDBGuys suggestions but I was only using that routine earlier to day and it was still hanging around in my head..

Glad it helped...
thumbup.gif
theDBguy
Hi Larry,

QUOTE (Larry Larsen @ May 24 2012, 02:13 PM) *
I didn't intentionally want to steer you away from thDBGuys suggestions but I was only using that routine earlier to day and it was still hanging around in my head..

I don't think you "steered" Chris away from my suggestion because that is one of the ways I recommended, i.e. use the "Open" statement.

Thanks for the assist! thumbup.gif
ChrisSymonds
I've been working at this for 3 days now and am completely stuck. When I run the code it hangs at "Set rst = db.OpenRecordset(strSQL, dbOpenSnapshot)" wiht the error message "Runtime error 3163 field is too small to accept the amount of data you attempted to add. Try inserting or pasting less data."

I assume this is because one of the fields is a Memo field. Ive tried everything I can think of, or find on this site or on the internet to fix this but can't. Any suggestions?

I also tried to us the TransferText method referred to earlier in this post but it too seeme to choke on memo fileds.

TIA,

Chris
Larry Larsen
Hi Chris
Can you give any details at the size of data within this memo field, memo fields have a general limit of about 64k..
thumbup.gif
ChrisSymonds
Hi,

There are about 8,800 records in the table each has 3 fields a date, an identifying number, a mamo field that has a resume.
the largest resume has 603,669 character in it (just ran a queyr with LEN(Resume), not sure what that translates to in K of data. That resume is the biggest by about a factor of 5, if you think that one is the problem I can delete it?

Thanks,

Chris
Larry Larsen
Hi Chris
It's worth a try... back that table up and run the routine and see if it falls over...

OpenRecordSet is a DAO method that applies among others to the Database, TableDef and QueryDef objects. One of the arguments of this method is Type, to specify what kind of recordset you want to open. Possible values for Type are:

dbOpenDynamic
dbOpenDynaset
dbOpenForwardOnly
dbOpenSnapshot
dbOpenTable

If you want to be able to add, edit and delete records, use dbOpenDynaset. This will work both with tables/queries in the database itself as with linked tables that reside in another Access database.
If you only want to retrieve records without editing them, and you want to be able to move forwards and backwards, you can use dbOpenSnapshot.
If you will only run through the records once, from first to last, and you don't need to edit them, you can use dbOpenForwardOnly.
dbOpenSnapshot and dbOpenForwardOnly place fewer locks on the table, which can be handy in a multi-user environment.

thumbup.gif
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.