如何在添加新列时设置默认值

时间:2013-07-30 07:29:55

标签: sql vba ms-access-2007

我使用以下vba代码在Access 2007中执行sql语句:

SQL = "ALTER TABLE " & strNEW & _
      " ADD COLUMN ([GolfLengte] INT) DEFAULT " & _
      strTableName

DoCmd.RunSQL (SQL)

Wich打印:

ALTER TABLE tbl_GL_850 ADD COLUMN ([GolfLengte] INT) DEFAULT 850

查询应该使用已有记录更新已存在的表:

    Table1
+--------------+----------+----------+
| Column1      |  Column2 |  Column3 |                  
+--------------+----------+----------+
|       x      |    x     |    x     | 
+--------------+----------+----------+
|       x      |    x     |    x     | 
+--------------+----------+----------+
|       x      |    x     |    x     |
+--------------+----------+----------+
|       x      |    x     |    x     |
+--------------+----------+----------+

我想要的是另一个添加了In every record列的变量strTableName中的值,如下所示:

    Table1              strTableName = 850
+--------------+----------+----------+----------+
| Column1      |  Column2 |  Column3 |GolfLengte|         
+--------------+----------+----------+----------+
|       x      |    x     |    x     |   850    |
+--------------+----------+----------+----------+
|       x      |    x     |    x     |   850    |
+--------------+----------+----------+----------+
|       x      |    x     |    x     |   850    |
+--------------+----------+----------+----------+
|       x      |    x     |    x     |   850    |
+--------------+----------+----------+----------+

在我看来它是一个有效的SQL语句,但由于某种原因,vba一直告诉我突出显示时出现语法错误

DoCmd.RunSQL (SQL) 

我做错了什么?

2 个答案:

答案 0 :(得分:2)

AFAIK,ALTER TABLE更改表架构,但不更改数据。

"UPDATE " & strNEW & " SET GolfLengte = " & strTableName之后您必须ALTER TABLE

答案 1 :(得分:0)

ALTER TABLE tbl_GL_850
ADD GolfLengte INT NOT NULL DEFAULT(850)
道歉....这是SQL,我错过了问题的VBA Access部分(现在很早)

尝试:

ALTER TABLE tbl_GL_850 ADD COLUMN GolfLengte INT DEFAULT 850