将DAO DBEngine DataTable的列从DataType dbInteger更改为VB6中的dbLong

时间:2009-07-20 20:44:14

标签: database vb6 dao data-access-object

我继承了一个传统的VB6应用程序来维护,而且我的vb6还有点生锈......

我有一个DAO表,其中包含DAO.DataTypeEnum.dbInteger类型的字段,需要将其更改为输入DAO.DataTypeEnum.dbLong。是否有快捷方式vb6设置此新数据类型并保留现有值,或者是否需要创建临时列来存储数据,然后删除并重新创建具有新数据类型的列,然后手动迁移数据?

2 个答案:

答案 0 :(得分:2)

如果您的数据库引擎支持ALTER TABLE ALTER COLUMN,则

Shahkalpesh's answer没问题。如果您使用的是Access数据库引擎(Jet .mdb,ACE .accdb等),则可以在ANSI-92 Query Mode(Jet 4.0和Access 2002以后)中使用ALTER COLUMN。

过去我完全通过代码完成了这一切。下面的代码将字符串字段转换为浮点双精度而不使用ALTER COLUMN。它创建一个具有不同名称和正确数据类型的新字段,复制数据,删除原始字段,并将新字段重命名为原始名称。你可以轻松地调整它来做整数到长。

  Dim fld As DAO.Field

  ' Cant just change the type of an existing field. '
  ' Instead have to create the new field with a temporary name, '
  ' fill it with the required data, delete the old MyField field '
  ' and then rename the new field. The renaming has to be done '
  ' with DAO - cant do it through SQL '

  ' Add TEMP_MyField field: required double field. Will be renamed later '
  sSQL = "ALTER TABLE MyTable " & _
         "ADD COLUMN TEMP_MyField DOUBLE NOT NULL "
  dbDatabase.Execute sSQL, dbFailOnError

  ' Copy the MyField values to the TEMP_MyField field '
  sSQL = "UPDATE MyTable SET TEMP_MyField = CDbl(MyField)"
  dbDatabase.Execute sSQL, dbFailOnError

  ' Delete the original MyField field (the text field) '
  sSQL = "ALTER TABLE MyTable DROP COLUMN MyField"
  dbDatabase.Execute sSQL, dbFailOnError

  ' Need to refresh the TableDefs to make sure new field shows up '
  dbDatabase.TableDefs.Refresh

  ' Get a reference to the temporary MyField field we just created '
  Set fld = dbDatabase.TableDefs("MyTable").Fields("TEMP_MyField")

  ' Rename it to the final name we want it to have '
  fld.Name = "MyField"

答案 1 :(得分:1)

如果这是一次性作业,您可以打开Access数据库并更改数据类型 添加评论到这篇文章,否则。

编辑:您可以在数据库对象上发出ALTER语句

CurrentDb.Execute "ALTER TABLE myTable ALTER Column myIntegerColumn Long"
相关问题