Access Data to CSV file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • xxoulmate
    New Member
    • May 2007
    • 77

    Access Data to CSV file

    how can i extract data from access table into csv file format.,
    i want it to save through query.,

    i have example of sql query saving in xls format., but i want the query result to save in csv format.
    eg. xls format
    - "Select Fields into TempData in 'sample.xls' 'Excel 5.0;' from From Table"
  • ADezii
    Recognized Expert Expert
    • Apr 2006
    • 8834

    #2
    Originally posted by xxoulmate
    how can i extract data from access table into csv file format.,
    i want it to save through query.,

    i have example of sql query saving in xls format., but i want the query result to save in csv format.
    eg. xls format
    - "Select Fields into TempData in 'sample.xls' 'Excel 5.0;' from From Table"
    I can show you a Custom Routine that I created which will write specific Query Field Values to a CSV (Comma Seperated Value) File but I'll wait and see if anyone has a better idea. I'm probably mistaken, but I do not think that there is an easy way to accomplish this through the Access Interface, but there is programmaticall y.

    Comment

    • xxoulmate
      New Member
      • May 2007
      • 77

      #3
      how bout., instead of saving it in csv file save it in text file through query

      Comment

      • harshakusam
        New Member
        • Apr 2009
        • 36

        #4
        This method will help you...

        Code:
        The TransferText method has the following syntax and options:
        
        DoCmd.TransferText(TransferType, SpecificationName, TableName, FileName, HasFieldNames, HTMLTableName, CodePage)

        This will work...

        Code:
        Dim strSQL As String
        strSQL = "Your select statement"
        DoCmd.TransferText ExportDelim, "strSQL", "path.csv"

        Comment

        • xxoulmate
          New Member
          • May 2007
          • 77

          #5
          how can i add this to vb 6 program
          ex.
          i will use the access database
          location = "c:\db1.mdb "
          query = "select * from table1"


          how can i use docmd in vb

          Comment

          • NeoPa
            Recognized Expert Moderator MVP
            • Oct 2006
            • 32668

            #6
            There is an option to save an object (Table or QueryDef will work - NOT SQL string) as a CSV file within Access (VBA). If you need to know how to access this via VB, then I suggest the VB forum is where you need help (although I suspect it may be as simple as having an Access application variable in your code and using AccVar.DoCmd ...). Would you like me to move this over for you?

            Comment

            • ADezii
              Recognized Expert Expert
              • Apr 2006
              • 8834

              #7
              Originally posted by xxoulmate
              how can i extract data from access table into csv file format.,
              i want it to save through query.,

              i have example of sql query saving in xls format., but i want the query result to save in csv format.
              eg. xls format
              - "Select Fields into TempData in 'sample.xls' 'Excel 5.0;' from From Table"
              It took me a little while, but I did come up with what may be a workable solution, the 'Base Code' of which I'll post for you. Basically, if the Filename contains 'Exec ' it is converted and Renamed to 'Exec_', the Import occurs without any problems, and the User is notified of this process. If the Filename does not contain 'Exec ', it is Imported normally. It is up to you as to whether or not it is a viable Option:
              Code:
              Public Function fImportCSVFile(strFileName As String)
              Dim strNewName As String
              
              If InStr(strFileName, "Exec ") > 0 Then     'does "Exec " appear in the File Name
                strNewName = Replace(strFileName, "Exec ", "Exec_")
                  Name strFileName As strNewName      'Rename the File replqacing Exec  with Exec_
                    DoCmd.TransferText acImportDelim, , "CSV Table", strNewName, False
                    MsgBox strFileName & " has been Renamed to " & strNewName & " in order to avoid " & _
                                         "Import Errors", vbExclamation, "Change in File name"
              Else
                DoCmd.TransferText acImportDelim, , "CSV Table", strFileName, False
              End If
              End Function
              To successfully Import a File with 'Exec ' contained within its Name:
              Code:
              Debug.Print fImportCSVFile("C:\Dezii\Exec Test.csv")

              Comment

              • NeoPa
                Recognized Expert Moderator MVP
                • Oct 2006
                • 32668

                #8
                Wasn't the "Exec in filename" issue from another thread?

                Comment

                • ADezii
                  Recognized Expert Expert
                  • Apr 2006
                  • 8834

                  #9
                  Originally posted by NeoPa
                  Wasn't the "Exec in filename" issue from another thread?
                  I do believe you're corect, NeoPa. Just add it to my ever growing 'OOPs List'. Actually, I think I may have Double-Posted, sorry.

                  Comment

                  • NeoPa
                    Recognized Expert Moderator MVP
                    • Oct 2006
                    • 32668

                    #10
                    I think the giggle was worth it ADezii ;)

                    Besides, you more than make up for any senior moment lapses.

                    Comment

                    • HAL4u
                      New Member
                      • Jan 2010
                      • 1

                      #11
                      Table to CSV

                      Code:
                      DoCmd.TransferText acExportDelim, "KlasExportspecificatie", "Resultaten", "C:\Users\HAL\Desktop\" & klKlassen.Value & " " & sKeuze & ".csv"
                      
                      "KlasExportspecificatie"      is the exportspecification I created with the export wizard, containing the settings for CSV files
                      "Resultaten"                  is the table from which the data is exporting
                      "C:\Users\HAL\Desktop\" & klKlassen.Value & ".csv"
                                                    is the file path to where the file is exported
                      klKlassen.Value               is the value from a list-box which contains a part of the filename
                      The trick is to make a exportspecifica tion in the Export Wizard.
                      Right-click the exported table. Choose EXPORT, TXT-file.
                      All checkboxes not checked.
                      Use on the next page the ADVANCED-button to modify and save the exportspecifica tion.

                      That does the trick, HAL

                      Comment

                      Working...