Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to change names of columns in Access table with VBA 2

Status
Not open for further replies.

howardr

Technical User
Sep 26, 2002
14
US
I need to change the names of the columns in an Access table that I am pulling in from another Access database so that I can run the pre-written queries I have. Is there a way to import a table from another Access database and then rename the fields / columns in VBA to what I need them to be?

Thanks in advance for your help!
 
How are ya howardr . . . . .

After you import the table and with [blue]no relationships established[/blue] for that table, try the following and modify to your taste ([blue]You![/blue] substitute proper names in [purple]purple[/purple]):
Code:
[blue]Public Sub NewNames()
   Dim db As DAO.Database, tdf As TableDef, fld As Field
   
   Set db = CurrentDb()
   Set tdf = db.TableDefs("[purple][b]YourTableName[/b][/purple]")
   
   For Each fld In tdf.Fields
      If fld.Name = "[purple][b]CurrentName1[/b][/purple]" Then
         fld.Name = "[purple][b]NewName1[/b][/purple]"
      ElseIf fld.Name = "[purple][b]CurrentName2[/b][/purple]" Then
         fld.Name = "[purple][b]NewName2[/b][/purple]"
      End If
   Next
   
   Set tdf = Nothing
   Set db = Nothing

End Sub[/blue]

Calvin.gif
See Ya! . . . . . .
 
Thanks TheAceMan1 !! Worked like a charm. Your help is greatly appreciated!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top