Results 1 to 9 of 9
  1. #1
    Middlemarch is offline Competent Performer
    Windows 10 Access 2019
    Join Date
    Mar 2015
    Posts
    365

    Copying Field Names one table to another

    I'm using DoCmd.RunSavedImportExport to import a text file to a table in my db
    The table has fieldnames Field1, Field2 etc.


    I think this command will change them


    CurrentDb.TableDefs("TableName").Fields("FieldName ").Name = "NewName"
    but it would be repeated many times.


    I do have a empty "tblTemp" (structure only) with the fieldnames. Is there a way to copy them into the new table ?

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    This will get the names, shouldn't be too tough to add the bit to copy to the other table, using the x variable.

    Code:
      Dim strSQL  As String
      Dim db      As DAO.Database
      Dim rs      As DAO.Recordset
      Dim x As Integer
    
      Set db = CurrentDb()
      
      strSQL = "SELECT TOP 1 * FROM tblTempRes"
    
      Set rs = db.OpenRecordset(strSQL, dbOpenDynaset, dbSeeChanges)
    
      If Not rs.EOF Then
        For x = 0 To rs.Fields.Count - 1
          Debug.Print rs(x).Name
        Next x
      End If
    
      rs.Close
      Set rs = Nothing
      Set db = Nothing
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    Middlemarch is offline Competent Performer
    Windows 10 Access 2019
    Join Date
    Mar 2015
    Posts
    365
    Thank you Paul, that worked great. Never seen "TOP" used before. Used "CurrentDb.TableDefs("tPending").Fields("Field " & x + 1).Name = rs(x).Name"
    Is there something similar for DataType as they've all become Short Text which is trouble to come.

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    I thought they already existed. How are they being created? Or do you want to change the data type as part of this process?

    I'm heading out to dinner soon with my wife so it will likely be tomorrow before I see any replies. Nobody wants my input after a bottle of wine.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    By the way, I just used TOP to make sure I only pulled one record, didn't want to drag a whole table over the wire.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  6. #6
    Middlemarch is offline Competent Performer
    Windows 10 Access 2019
    Join Date
    Mar 2015
    Posts
    365
    I'll have a play, may fluke something. The table is (was) a text file exported by DoCmd.RunSavedImportExport and imported back the same way.
    That's to share it with someone else.
    There were formatting options including the field names, but Access complained about the names, including "-------------------------" which isn't one.

    Enjoy the wine

  7. #7
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Wine was good.

    I found this if you need to change data types:

    https://stackoverflow.com/questions/...ba-access-2007
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  8. #8
    Middlemarch is offline Competent Performer
    Windows 10 Access 2019
    Join Date
    Mar 2015
    Posts
    365
    Thank you, nicely sorted.

  9. #9
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Happy to help!
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

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

Similar Threads

  1. Replies: 7
    Last Post: 07-02-2022, 05:55 AM
  2. Field names in new table
    By Josen in forum Queries
    Replies: 8
    Last Post: 10-17-2019, 03:19 PM
  3. Replies: 7
    Last Post: 03-21-2018, 04:58 AM
  4. Replies: 1
    Last Post: 07-28-2014, 03:40 PM
  5. Replies: 6
    Last Post: 03-22-2011, 08:22 PM

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