SQL Change Column name

D

Darhl Thomason

I am using VB6 as a front end to an Access97 database. My computer has
Access2003 and won't let me edit the columns, tables, etc. I can add/change
data, but not the underlying structure of the database.

I have found & modified a VB front end that will allow me to add/drop
tables, add/drop columns, etc. But it doesn't have an option to change a
column name. I've done a bunch of googling on it, and know that using ALTER
TABLE and CHANGE is supposed to do it, but it's not, so I'm wondering if
CHANGE is not compatible with Access97.

The SQL statement I'm using is:

ALTER TABLE tblName CHANGE COLUMN NameField testChange

I have also tried

ALTER TABLE tblName CHANGE NameField testChange

Both of these give me a "Syntax error in ALTER TABLE statement"

Any ideas?

Thanks!

d
 
G

Gary Walter

Darhl Thomason said:
I am using VB6 as a front end to an Access97 database. My computer has
Access2003 and won't let me edit the columns, tables, etc. I can
add/change data, but not the underlying structure of the database.

I have found & modified a VB front end that will allow me to add/drop
tables, add/drop columns, etc. But it doesn't have an option to change a
column name. I've done a bunch of googling on it, and know that using
ALTER TABLE and CHANGE is supposed to do it, but it's not, so I'm
wondering if CHANGE is not compatible with Access97.

The SQL statement I'm using is:

ALTER TABLE tblName CHANGE COLUMN NameField testChange

I have also tried

ALTER TABLE tblName CHANGE NameField testChange

Both of these give me a "Syntax error in ALTER TABLE statement"
Hi Darhl,

I am not familiar with a "CHANGE COLUMN" clause.

There is an "ALTER COLUMN" clause (which I believe
was not supported in Acc97, and in Acc2xxx only if there
was no relationship on the column).

But, the "ALTER COLUMN" did not include the ability
to rename the column (I believe).

To do what you want, all in SQL, you will need a series
of queries. For example if your columns are TEXT(20)
and there was no constraint on "old" column NameField:

ALTER TABLE tblName ADD COLUMN testChange TEXT(20);

UPDATE TABLE tblName SET testChange = NameField;

ALTER TABLE tblName DROP COLUMN NameField;

Try on backup db just in case I forgot something...

good luck,

gary
 
G

Gary Walter

of course, in Access w/DAO,
all you need is one line of code:

CurrentDb.TableDefs("tblName").Fields("NameField").Name = "testChange"
 
D

Darhl Thomason

Gary Walter said:
of course, in Access w/DAO,
all you need is one line of code:

CurrentDb.TableDefs("tblName").Fields("NameField").Name = "testChange"

"Gary Walter" wrote:

Hi Gary,

Thanks for the advice. Your first reply is kind of what I was leaning
towards. I'm doing this in VB6, so I'm hoping the DAO method will work here
as well. Right now, my thought process is to read the old column details,
use that to create the new column, copy the data from the old column to the
new column, then delete the old column.

I don't think there are any constraints on the column, but I honestly don't
know. How would I check? I do know the column is only a "data" column, the
relationships are all built on the "ID" column.

It's a side project, so I'll hopefully be able to get back to it later this
week.

Thanks again!

Darhl
 
G

Gary Walter

Darhl Thomason said:
Hi Gary,

Thanks for the advice. Your first reply is kind of what I was leaning
towards. I'm doing this in VB6, so I'm hoping the DAO method will work
here as well. Right now, my thought process is to read the old column
details, use that to create the new column, copy the data from the old
column to the new column, then delete the old column.

I don't think there are any constraints on the column, but I honestly
don't know. How would I check? I do know the column is only a "data"
column, the relationships are all built on the "ID" column.

Hi Darhl,

Sounds like you should be okay.

Cannot you just verify on a backup?

This may be "overkill" but here be an old Access code module
that can give all details in Debug window using DAO
(perhaps you can adapt to your purposes):

Option Compare Database
Option Explicit

Function fGetIndexes(pstrTableName As String) As Boolean
'adapted from NEAT CD
' Prints indexes in Immediate Window
On Error GoTo Err_fGetIndexes
Dim db As DAO.Database
Dim tdf As DAO.TableDef
Dim indx As DAO.Index
Dim i As Integer
Set db = CurrentDb
Set tdf = db.TableDefs(pstrTableName)
Debug.Print "=================================="
Debug.Print "INDEX Info for: " & pstrTableName
Debug.Print "=================================="
Debug.Print "Index Count: " & tdf.Indexes.Count
For i = 0 To tdf.Indexes.Count - 1
Set indx = tdf.Indexes(i)
Debug.Print "----------------"
Debug.Print vbCrLf & "Index Name: " & indx.Name
Debug.Print " Primary: " & indx.Primary
Debug.Print " Foreign: " & indx.Foreign
Debug.Print " Clustered: " & indx.Clustered
Debug.Print " Fields: " & indx.Fields
Debug.Print " Required: " & indx.Required
Debug.Print " Unique: " & indx.Unique
Debug.Print " Ignore Nulls: " & indx.IgnoreNulls

Next i

Debug.Print "========================================"
Debug.Print "End of INDEX Info for: " & pstrTableName
Debug.Print "========================================"
db.Close
fGetIndexes = True

Exit_fGetIndexes:
Set tdf = Nothing
Set db = Nothing
Exit Function
Err_fGetIndexes:
fGetIndexes = False
If Err = 3265 Then
MsgBox "The table " & pstrTableName & " does not exist."
Resume Exit_fGetIndexes
Else
MsgBox "Error No: " & Err.Number & "; Description: " &
Err.Description
Resume Exit_fGetIndexes
End If
End Function

Public Function FieldType(intType As Integer) As String
'from post by Dan Artuso
Select Case intType
Case dbBoolean
FieldType = "dbBoolean"
Case dbByte
FieldType = "dbByte"
Case dbInteger
FieldType = "dbInteger"
Case dbLong
FieldType = "dbLong"
Case dbCurrency
FieldType = "dbCurrency"
Case dbSingle
FieldType = "dbSingle"
Case dbDouble
FieldType = "dbDouble"
Case dbDate
FieldType = "dbDate"
Case dbText
FieldType = "dbText"
Case dbLongBinary
FieldType = "dbLongBinary"
Case dbMemo
FieldType = "dbMemo"
Case dbGUID
FieldType = "dbGUID"
End Select

End Function

Public Function FieldOutput(fldTemp As DAO.Field) As Variant
' adapted from Access Help

Dim prpLoop As DAO.Property
Dim strProp As String

' Enumerate Properties collection of passed Field
' object.
For Each prpLoop In fldTemp.Properties
' Some properties are invalid in certain
' contexts (the Value property in the Fields
' collection of a TableDef for example). Any
' attempt to use an invalid property will
' trigger an error.
On Error Resume Next
strProp = strProp & prpLoop.Name & " = " & _
prpLoop.Value & vbCrLf
On Error GoTo 0
Next prpLoop

FieldOutput = strProp

End Function

Public Function fGetRelations() As Boolean
On Error GoTo Err_fGetRelations
Dim db As DAO.Database
Dim rel As DAO.Relation
Dim fld As DAO.Field

'open current db
Set db = CurrentDb

Debug.Print "============================"
Debug.Print "RELATIONS"
Debug.Print "============================"

For Each rel In db.Relations

With rel

Debug.Print "RelName = ", .Name
Debug.Print "Table = ", .Table
Debug.Print "ForeignTable = ", .ForeignTable
Debug.Print "RelationAttributes = ", .Attributes

For Each fld In .Fields

Debug.Print "FieldName = ", fld.Name
Debug.Print "ForeignTableFieldName = ", fld.ForeignName

Next
End With
Debug.Print "--------------------------"
Next
fGetRelations = True

db.Close

Exit_fGetRelations:
Set fld = Nothing
Set db = Nothing
Exit Function

Err_fGetRelations:
MsgBox Err.DescripTableion
Resume Exit_fGetRelations

End Function

Public Function fListTableInfo(pTable As String) As Boolean
On Error GoTo Err_fListTableInfo
Dim db As DAO.Database
Dim tdf As DAO.TableDef
Dim fld As DAO.Field
Dim idx As DAO.Index
Dim strName As String
Dim strType As String
Dim varProp As Variant
Dim i As Integer

Set db = CurrentDb
Set tdf = db.TableDefs(pTable)

Debug.Print "Table: " & pTable

'fGetIndexes pTable

For i = 0 To tdf.Fields.Count - 1
Debug.Print "----------------------------"
Set fld = tdf.Fields(i)
varProp = FieldOutput(fld)
strName = fld.Name
strType = FieldType(tdf.Fields(i).Type)
If strType = "dbText" Then
strType = strType _
& " (" & tdf.Fields(i).Size & ")"
End If
'is it a primary key?
For Each idx In tdf.Indexes
If idx.Primary Then
' Found a Primary Key
For Each fld In idx.Fields
If fld.Name = strName Then
strType = strType & " (pk)"
Exit For
End If
Next fld
Exit For
End If
Next idx

Debug.Print "Field: " & strName & vbCrLf _
& "Field Type: " & strType _
& vbCrLf & varProp

Next i

Debug.Print "----------------------------"

db.Close

fListTableInfo = True

Exit_fListTableInfo:
Set fld = Nothing
Set tdf = Nothing
Set db = Nothing
Exit Function

Err_fListTableInfo:
MsgBox Err.Description
Resume Exit_fListTableInfo
End Function

good luck,

gary
 
D

Darhl Thomason

Gary Walter said:
good luck,

gary
I actually have some functions similar to those you posted. I'll definitely
poke through your code and see if I can make it work.

Thanks again!

Darhl
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top