• Create Access table

    Author
    Topic
    #463793

    Excel/ Access 2007.
    Hello.
    I’ve created a procedure that imports a csv file. I’m then using DAO to create a new table (TableDef)
    and copy the (csv) data into this new table. Is there an easier way to do this, without me having to
    loop through all the rows? Similarly, would there be an easier way if I were appending the records to
    an existing table? Thanks, Andy.

    Viewing 0 reply threads
    Author
    Replies
    • #1185121

      You could run an SQL statement:

      Dim strSQL As String
      Dim strNewTable As String
      Dim strCSVTable As String
      strNewTable = “tblNew”
      strCSVTable = “tblImport”
      strSQL = “SELECT * INTO [” & strNewTable & “] FROM [” & strCSVTable * “]”
      CurrentDb.Execute strSQL, dbFailOnError

      To append to an existing table, use

      strSQL = “INSERT INTO [” & strNewTable & “] SELECT * FROM [” & strCSVTable * “]”

      You can specify columns instead of *.

      • #1185123

        You could run an SQL statement:

        Dim strSQL As String
        Dim strNewTable As String
        Dim strCSVTable As String
        strNewTable = “tblNew”
        strCSVTable = “tblImport”
        strSQL = “SELECT * INTO [” & strNewTable & “] FROM [” & strCSVTable * “]”
        CurrentDb.Execute strSQL, dbFailOnError

        To append to an existing table, use

        strSQL = “INSERT INTO [” & strNewTable & “] SELECT * FROM [” & strCSVTable * “]”

        You can specify columns instead of *.

        Thank you. Is tblImport an Excel range name? Andy.

        • #1185125

          I had assumed from your description that you had already imported the CSV file into an Access table named tblImport, and wanted to transfer its records to a new or existing table.

          • #1185127

            I had assumed from your description that you had already imported the CSV file into an Access table named tblImport, and wanted to transfer its records to a new or existing table.

            No, sorry. The csv data is currently in an Excel worksheet. I now need, from Excel, to either create a new
            Access table or append to an existing one. Andy.

            • #1185129

              You could use DoCmd.TransferSpreadsheet in Access to import from an Excel workbook, or DoCmd.TransferText to import directly from a .csv file.
              If you want, you can use Automation to do this from Excel.

              Alternatively, you could use ADO – it can run SQL on an Excel worksheet or .csv file.

            • #1185137

              You could use DoCmd.TransferSpreadsheet in Access to import from an Excel workbook, or DoCmd.TransferText to import directly from a .csv file.
              If you want, you can use Automation to do this from Excel.

              Alternatively, you could use ADO – it can run SQL on an Excel worksheet or .csv file.

              Thank you very much. Andy.

    Viewing 0 reply threads
    Reply To: Reply #1185137 in Create Access table

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information:




    Cancel