Article ID: 128016
Article Last Modified on 1/19/2007
Option Explicit
'*****************************************************************
' The AlterFieldType Sub procedure requires three string
' parameters. The first string specifies the name of the table
' containing the field to be changed. The second string specifies
' the name of the field to be changed. The third string specifies
' the new data type for the field.
'*****************************************************************
Sub AlterFieldType (TblName As String, FieldName As String, _
NewDataType As String)
Dim db As Database
Dim qdf As QueryDef
Set db = CurrentDb()
' Create a dummy QueryDef object.
Set qdf = db.CreateQueryDef("", "Select * from PROD1")
' Add a temporary field to the table.
qdf.SQL = "ALTER TABLE [" & TblName & "] ADD COLUMN_
AlterTempField " & NewDataType
qdf.Execute
' Copy the data from old field into the new field.
qdf.SQL = "UPDATE DISTINCTROW [" & TblName & "] SET_
AlterTempField = [" & FieldName & "]"
qdf.Execute
' Delete the old field.
qdf.SQL = "ALTER TABLE [" & TblName & "] DROP COLUMN ["_
& FieldName & "]"
qdf.Execute
' Rename the temporary field to the old field's name.
db.tabledefs("[" & TblName & "]").Fields("AlterTempField")._
Name = FieldName
' Clean up.
End Sub
AlterFieldType "PROD1", "UnitsInStock", "LONG"
Additional query words: programming modify design
Keywords: kbhowto kbprogramming KB128016