Mike:
As an example of changing a field's property here's some code I came up with
a while ago as an answer when somebody wanted to change the display control
of a Boolean (Yes/No) field to a check box:
Const PROPEXISTS = 3367
Dim db As DAO.Database
Dim tdf As DAO.TableDef
Dim fld As DAO.Field
Dim prp As DAO.Property
Set db = CurrentDb
Set tdf = db.TableDefs("MyTable")
Set fld = tdf.Fields("MyFieldName")
On Error Resume Next
Set prp = fld.CreateProperty("DisplayControl", dbInteger, acCheckBox)
fld.Properties.Append prp
If Err = PROPEXISTS Then
fld.Properties("DisplayControl") = acTextBox
Else
' unknown error
MsgBox Err.Description
End If
To make a column a table's primary key you can use the DDL CREATE INDEX
statement e.g.
CREATE INDEX PrimaryKey
ON MyTable (MyID) WITH Primary;
Similarly to alter a column you use the DDL ALTER TABLE statement, e.g.
ALTER TABLE MyTable
ALTER COLUMN MyNumber DOUBLE;
Ken Sheridan
Stafford, England
CLSWL said:
I used your DAO solution to rename fields. It works well.
How do I change other field properties, specifically making a field the
primary index, changing it's data type or changing its caption?
--
-CLSWL
:
My understanding o f the position is the same as Doug's. Here are DAO and
ADOX solutions:
Public Sub RenameColumn_DAO(strTable As String, strOldName As String,
strNewName As String)
Dim dbs As DAO.Database
Dim tdf As DAO.TableDef
Dim fld As DAO.Field
Set dbs = CurrentDb
Set tdf = dbs.TableDefs(strTable)
Set fld = tdf.Fields(strOldName)
fld.Name = strNewName
End Sub
Public Sub RenameColumn_ADOX(strTable As String, strOldName As String,
strNewName As String)
Dim cat As ADOX.Catalog
Dim tbl As ADOX.Table
Set cat = New Catalog
cat.ActiveConnection = CurrentProject.Connection
Set tbl = cat(strTable)
tbl.Columns(strOldName) = strNewName
End Sub
Ken Sheridan
Stafford, England
:
Hello all,
I want to rename a field in a table programmatically. I know I can do
this using DAO/ADO, but after doing some searching I found that I could
possibly do it with a SQL statement. I tried the following query:
ALTER TABLE tblname RENAME COLUMN [oldfieldname] TO [newfieldname];
However, I get the error message 'Syntax Error in ALTER TABLE
statement', highlighting the 'RENAME' part of the statement. I was
wondering what is the correct syntax, if this is possible? I have
MSAccess 2003.
Thanks,
Mike