我使用以下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)
我做错了什么?
答案 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