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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need Help with sub routine

Status
Not open for further replies.

bryn30

Technical User
Mar 5, 2001
57
US
I found this code that will do what I am needing posted by DougP. Thanks Doug,
How do I place this code in a sub routine and How do I put the last piece of info in a Debug window?

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 Table1")

' 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
------------------------------------------

Put the following a the debug window

AlterFieldType "Table1", "Employee", "LONG"
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top