Results 1 to 4 of 4
  1. #1
    newbie30 is offline Novice
    Windows 10 Access 2010 64bit
    Join Date
    Nov 2017
    Posts
    15

    Renaming a column in temp table after import

    Hi,

    Am basically importing data from EXCEL into Access and below is the code.

    DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12, "DLPTemp3", FileName, True



    Dim Rs1 As ADODB.Recordset

    Set Rs1 = mdbx.OpenRecordset("SELECT * FROM DLPTemp3;", SeeChangesReadWrite)

    DoCmd.Close acTable, "DLPTemp3"
    mdbx.Execute ("DROP TABLE DLPTemp3")

    However the Excel contains a field with a & (eg: test & Test) and I need to rename that to 'Test and Test' before importing into an SQL table.

    Could someone pl tell how do I go about renaming in a tempdb.

    Thanks

  2. #2
    aytee111 is offline Competent At Times
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    You will import a spreadsheet to a temp table then use an append query to clean up the field names and the data to add it to a permanent table.

  3. #3
    newbie30 is offline Novice
    Windows 10 Access 2010 64bit
    Join Date
    Nov 2017
    Posts
    15
    Yes I will import it into a temp table and then after some validations import the field names and data from the temp table into a permanent one. Somehow I need to rename the field before the import into the permanent one. I tried this

    str1 = "ALTER TABLE DLPTemp3 Add [Postage and packaging] money"
    mdbx.Execute str1
    str2 = "UPDATE DLPTemp3 SET DLPTemp3.[Postage & packaging] = DLPTemp3.[Postage and packaging] FROM DLPTemp3 JOIN DLPTemp3 N ON DLPTemp3.[Sales Record Number] = N.[Sales record number] "
    mdbx.Execute str2
    str3 = "Alter table DLPTemp3 drop column [Postage & packaging]"
    mdbx.Execute str3

    However the data isn't getting copied.it renames the field though.

    Is there a better solution?

    Thanks

  4. #4
    NoellaG's Avatar
    NoellaG is offline VIP
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Jun 2010
    Location
    Belgium
    Posts
    1,041
    Hi,

    if you load the library (Tools -> References in VBA window) Microsoft ADO ext. for DDL and security, you can use the following function to alter the name of a column in a Access table with the name tbl1 (change the name according to your situation):
    ===================================
    Public Function fRenameColumn(strOldColName As String, strNewColName As String) As Integer
    On Error GoTo Err_fRenameColumn
    Dim cat As New ADOX.Catalog
    Dim tblT As ADOX.Table
    Dim colC As ADOX.Column

    Set cat.ActiveConnection = CurrentProject.Connection
    Set tblT = cat.Tables("tbl1")
    For Each colC In tblT.Columns
    If colC.Name = strOldColName Then colC.Name = strNewColName
    fRenameColumn = 1
    Next colC

    Exit_fRenameColumn:
    Set colC = Nothing
    Set tblT = Nothing
    Set cat = Nothing
    Exit Function

    Err_fRenameColumn:
    fRenameColumn = -1
    Resume Exit_fRenameColumn

    End Function

    ======================
    Kind regards

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

Similar Threads

  1. Import automation and copy to a temp table
    By Alonza Malcom in forum Access
    Replies: 24
    Last Post: 09-30-2015, 08:00 AM
  2. Renaming Column Headers in Pivot Table
    By excellenthelp in forum Access
    Replies: 1
    Last Post: 10-24-2014, 01:50 PM
  3. Replies: 1
    Last Post: 09-03-2014, 10:48 AM
  4. Replies: 3
    Last Post: 12-14-2012, 11:26 AM
  5. Replies: 5
    Last Post: 12-01-2011, 05:38 PM

Tags for this Thread

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