Results 1 to 3 of 3
  1. #1
    jadog is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Oct 2012
    Posts
    5

    Macro to change column name


    Is there a way to create a macro in Access that can change the column name in my table? For example:

    I have a table with the name SP20C and I want to change the current column name from F1 to Item_no. Is this possible?

    I wish there was a way to record it like in Excel...

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    You want to actually change the name of the field in table (why?) or just show an alias on forms and reports?

    There are ways to programmatically change field name in table but why is this necessary? If you change fieldname this could cause issues if the fieldname is already used elsewhere in code or in expressions in textboxes.

    If you just need to do a one-time design edit, open the table and modify.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    jadog is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Oct 2012
    Posts
    5
    I was looking for a way to "automate" a multi-step process. There may have been a different (better) way to do this, and I appreciate you trying to help me with that. Anyway, I finally figured it out, and for those who may be looking for a way to get that done, below is the code I used. Just copy the entire script below into the VB Editor in Access:

    Code:
    Sub RenameFieldControl(strTableName As String, _
                            strFieldFrom As String, _
                            strFieldTo As String)
                            
        Dim dbs As DAO.Database
        Dim tDef As DAO.TableDef
        Dim fDef As DAO.Field
        
        Set dbs = CurrentDb()
        Set tDef = dbs.TableDefs(strTableName)
        Set fDef = tDef.Fields(strFieldFrom)
        
        fDef.Name = strFieldTo
        
        Set fDef = Nothing
        Set tDef = Nothing
        Set dbs = Nothing
                            
    End Sub
    
    Sub RenameField()
        RenameField "TableName", "CurrentColumnName", "NewColumnName"
    End Sub

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

Similar Threads

  1. Replies: 2
    Last Post: 05-26-2012, 01:08 PM
  2. Change color on text using column value?
    By PoorCadaver in forum Programming
    Replies: 5
    Last Post: 03-02-2012, 09:52 AM
  3. Replies: 1
    Last Post: 08-18-2011, 08:35 AM
  4. Replies: 0
    Last Post: 01-12-2011, 12:43 PM
  5. Primary Key change macro?
    By gracin in forum Access
    Replies: 0
    Last Post: 03-06-2009, 10: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