如何更改DAO.Recordset字段类型

时间:2015-02-23 10:34:06

标签: excel vba dao recordset

有什么办法可以在VBA中更改DAO.Recordset字段的类型吗?

由于某种原因,解释器将记录集字段评估为类型8(dateTime),但我需要此字段为Text,因为它应该是

我试过以下:

rs.Fields(0).Type = 10

但是这给了我在此集合中找不到的项目。错误。

有什么办法可以在DAO.Recordset中更改特定字段的类型?

EDIT1: 这是我到目前为止所做的事情

Private Sub doMagic()
    Dim db as DAO.Database
    Dim rs as DAO.Recordset
    Dim sql as String: sql = "SELECT * FROM [Sheet1$] WHERE [Column1] IS NOT NULL"

    Set db = OpenDatabase(ThisWorkbook.FullName, False, True, "Excel 8.0; HDR=Yes; IMEX=1;"
    set rs = db.OpenRecordset(sql)

    Dim updateSQL As String: updateSQL = "ALTER TABLE [Sheet1$] ALTER COLUMN [Column5] TEXT(100)"

    ' I tried following, non of them working:
    ' db.RunSql(updateSQL) - error: Metod or data member not found
    ' db.Execute(updateSQL) - fails with error: Operation is not supported for this type of object.
    ' DoCmd.RunSql(updateSQL)- error: Object required
End Sub

1 个答案:

答案 0 :(得分:0)

您无法使用DAO更改表字段类型。创建DAO记录集时,字段的定义是只读的。要更改字段类型,您需要使用DDL(数据定义语言)查询,这是link解释SQL的内容,这是您通常会执行的操作。像,

DoCmd.RunSQL "ALTER TABLE yourTableName ALTER COLUMN yourColumnName TEXT(50)"