简体   繁体   中英

Change data field type using access query

I need to change a single field data type from Long Integer type to Decimal, this is easily done by just using properties or design view.

However I want to know if this is possible using a query or perhaps a short VBA script? I can't add a new field, it has to be the existing one. We aso have to do this back and forth, depending on which program is reading the database.

The issue is we are in a transistion period between two programs. We have Microstation V8i with an add-on that reads data from the.mdb (32bit connection) database where the fields and tables are predefined and this specific field is a Long Integer. The other program is Microstation ConnectEdition which uses an updated version of the add-on which has a connection to an almost similar database (64 bit), however the developer of the update to the add-on has changed this field to a Decimal (among a few other things).

Since we currently use both programs and need to able to use both databases interchangeably, I'm working on a creating a combined Macro/Query that changes the necessary parts. This data type change is the last thing I need to do in order for this to be more automatic.

Edit-Added VBA:

Public Function changeDataType1()

Dim db As DAO.Database
Dim table As DAO.TableDef

Set db = CurrentDb
strSql = "ALTER TABLE feature ALTER COLUMN mslink DECIMAL(18,0);"
CurrentProject.Connection.Execute strSql
strSQL1 = "ALTER TABLE ugfeature ALTER COLUMN feature DECIMAL(18,0);"
CurrentProject.Connection.Execute strSQL1

Set db = Nothing

End Function

You can switch the field between those data types by executing ALTER TABLE statements from an ADO connection. (See Allen Browne's note about DECIMAL support .)

strSQL = "ALTER TABLE MyTable ALTER COLUMN switch_type LONG;"
CurrentProject.Connection.Execute strSQL

strSQL = "ALTER TABLE MyTable ALTER COLUMN switch_type DECIMAL(18,9);"
CurrentProject.Connection.Execute strSQL

I tested those examples in the Access Immediate window, so it should be easy to incorporate them into a VBA procedure, if that's your preference.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM