I'm trying to change a field's data type through code. This field contains date values but its current data type is "text". Anyone can supply the code to perform this task. Thanks,
Sub ChangeFieldDataType()
Dim dbs As Database, tdf As TableDef
Dim fld As Field
' Return reference to current database.
Set dbs = CurrentDb
' Return reference to Employees table.
Set tdf = dbs.TableDefs!Employees
' Set Field object.
Set fld = tdf.Fields("Field1"
' Set field properties.
fld.Type = dbText
Time out! There should be no reason to change the design of the table within code. If you do there is something seriously wrong. Consider the VBA built in functions for changing variable datatypes.
CStr()
CDate()
CInt()
CDbl()
Example:
Dim strVal As String
strVal = CStr(intValue)
Steve King Growth follows a healthy professional curiosity
There's no great reason why you shouldn't change the design of a database in code.
In many respects - it may be your only option - you're writing a database conversion tool that changes the database schema on a database that you have shipped out to clients - it's not feasible or professional to get users to change fields for you.
Rather, you should create the new field with a temporary name.
copy the required data into this (since you are using jet - you can use the functions cstr() cint() and the like)
(hint: UPDATE blah SET thefieldyoujustadded = CStr(thefielthatisgivingyougrief))
Then drop your original field.
Then rename your new field so that it has the correct name
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.