Results 1 to 10 of 10
  1. #1
    mp3909 is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Feb 2018
    Posts
    154

    Exporting to a csv

    I have this line of code to export data to csv:

    Code:
    DoCmd.TransferText acExportDelim, "XYZ", qdf.Name, sFileName



    and I get an error on this line saying "The Mictosoft Acess database engine could not find the object "myFileName.csv". Make sure the object exists and that spell its name and path correctly."

    I am beginning to wonder, for this to work, do you need to already have a saved version of the file? I thought the above method will create a new file with that filename saved in that location.

  2. #2
    JoeM is offline VIP
    Windows 7 32bit Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    No, you do not need the file to be already created. But have you included a valid file path in your file name field?
    It needs to know not only what to name it, but where to put the file.

    Also, I am not certain that you have the correct arguments in the correct order (what is "qdf.name?")
    See: https://docs.microsoft.com/en-us/off...d.transfertext

    A simple way is to do an example as a Macro (using the TransferText) export. Hard code appropriate values, and try exporting the data.
    If that works, use the "Convert Macros to Visual Basic" functionality and convert the Macro.
    Then you can copy that line of code, and make edits to make it dynamic instead of hard-coded.
    This will ensure that you have the correct arguments in the correct order.

  3. #3
    mp3909 is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Feb 2018
    Posts
    154
    qdf.Name is the name of my query.

  4. #4
    mp3909 is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Feb 2018
    Posts
    154
    I still get an error saying the Microsoft Access database engine could not find the object "name of the file.csv". Make sure the object exists and that you spell its name and the path name correctly.

    I feel that when you try to export via using VBA, you definitely need the file to be created.

  5. #5
    JoeM is offline VIP
    Windows 7 32bit Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    I feel that when you try to export via using VBA, you definitely need the file to be created.
    That is DEFINITELY not true. I have used this many time before without any issues.
    Please post the rest of your code, where we can see the values of your variables and how they are being populated.

  6. #6
    JoeM is offline VIP
    Windows 7 32bit Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    I just created a simple example, and it worked for me without any errors (and successfully exported the file).

    Here is what my code looks like:
    Code:
    Private Sub cmdExport_Click()
    
        Dim fName As String
        Dim qName As String
        Dim xsName As String
        
        fName = "C:\Test\JoeM.csv"
        qName = "Query4"
        xsName = "CSV_Export_Specs"
        
        DoCmd.TransferText acExportDelim, xsName, qName, fName
        
        MsgBox "File exported to: " & fName
        
    End Sub
    I would really want to see all the code, where you are setting those variables values (setting query and file name).
    I assume too that you actually have an Export Specification named "XYZ" that is exporting to a comma delimited text file?
    Also make sure that you have write privileges to whatever path you are exporting to.

  7. #7
    mp3909 is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Feb 2018
    Posts
    154
    Hi JoeM,

    So sorry, my bad.
    I had an extra argument in my DoCmd.TransferText method - I was specifying a specification name "XYZ"
    Code:
    DoCmd.TransferText acExportDelim, "XYZ", qdf.Name, sFileName, True
    As soon as I removed this, it works:

    Code:
    DoCmd.TransferText acExportDelim, , qdf.Name, sFileName, True
    and yes, you are right, you do not need to create a file beforehand, it will simply create a new one for you.

    Thank you for your help

  8. #8
    mp3909 is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Feb 2018
    Posts
    154
    Is there a way to export data into the 2nd tab sheet of an existing csv file that already has data in the first tab sheet?

  9. #9
    JoeM is offline VIP
    Windows 7 32bit Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    I had an extra argument in my DoCmd.TransferText method - I was specifying a specification name "XYZ"
    That would/should work fine - if "XYZ" is a valid EXPORT specification. I am guessing maybe it is really as IMPORT specification, which would mean it is looking for an existing file, not exporting to a new file.
    Note that you CANNOT use Import Specifications for Export Specification arguments (and vice-versa).

    Is there a way to export data into the 2nd tab sheet of an existing csv file that already has data in the first tab sheet?
    A CSV, like all other text files, is a "flat file", meaning it has no sheets/tabs. So it is impossible to export to a second sheet tab since they do not exist in text files.
    When you view a CSV in Excel, you are actually converting it to an Excel file. Excel files allow multiple sheets/tabs. CSV files do not.
    If you want to see what a CSV (or any text file) REALLY looks like, open it in a Text Editor, like NotePad or WordPad, instead of Excel.

  10. #10
    mp3909 is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Feb 2018
    Posts
    154
    yes, it was an Import Specification, so that's why it did not work.
    Thanks

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Exporting PDF
    By Ddempsii in forum Access
    Replies: 6
    Last Post: 08-29-2016, 10:29 AM
  2. Exporting to app, is it possible?
    By shadowsedge in forum Access
    Replies: 1
    Last Post: 04-07-2016, 02:11 PM
  3. Exporting Help
    By manic in forum Access
    Replies: 5
    Last Post: 06-22-2012, 04:46 PM
  4. Exporting to PDF
    By Alaska1 in forum Access
    Replies: 4
    Last Post: 12-16-2010, 09:52 PM
  5. Exporting Help
    By mrnikeswsh in forum Import/Export Data
    Replies: 11
    Last Post: 01-16-2010, 11:59 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums