Change column name

jl39775

Registered User.
Local time
Today, 00:32
Joined
Dec 12, 2001
Messages
43
Is there a sql statement that will allow me to change the name of a column?

Thanks,

James
 
This user-defined-function (UDF) will do it:
Code:
Function RenameField2(ptblname As String, _
pfldname As String, pnewfldname As String)
'**************************************************
'Name:          RenameField2 (Function)
'Purpose:       Rename a field programatically
'Author:        Bob Askew (raskew)
'Parameters:    ptblName = Name of table to modify
'               pfldname = Name of field to rename
'               pnewfldname = New field name
'(From Debug Window:)
'Inputs: ? renamefield2("tblColors", "hue", "color")
'Output:  Field 'hue' has been renamed 'color'
'**************************************************

Dim db As DATABASE
Dim td As TableDef
Dim fld As Field

Set db = CurrentDb
Set td = db.TableDefs(ptblname)
  
For Each fld In td.Fields
  If fld.Name = pfldname Then
     fld.Name = pnewfldname
     'quit the loop if successful
     Exit For
  End If
Next fld

db.TableDefs.Refresh

'avoid memory leaks
Set td = Nothing
Set db = Nothing

End Function
Give it a try on a sample table.
 

Users who are viewing this thread

Back
Top Bottom