1

I am using the following vba code to execute an sql statement in Access 2007:

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

DoCmd.RunSQL (SQL)

Wich prints:

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

The query should update an already existing table with already existing records:

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

What I would like is another column added with In every record the value that is written in the variable strTableName like so:

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

Seems to me that it is a valid SQL statement, but for some reason vba keeps telling me that there is a syntax error while highlighting

DoCmd.RunSQL (SQL) 

What am I doing wrong?

2 Answers 2

2

AFAIK, ALTER TABLE change table schema, but not data.

You must "UPDATE " & strNEW & " SET GolfLengte = " & strTableName after ALTER TABLE.

Sign up to request clarification or add additional context in comments.

Comments

0

try

ALTER TABLE tbl_GL_850
ADD GolfLengte INT NOT NULL DEFAULT(850)

apologies....this is SQL, I missed the VBA Access part of the question (it's early)

try:

ALTER TABLE tbl_GL_850 ADD COLUMN GolfLengte INT DEFAULT 850

2 Comments

This gave me an error : Error 3320 while executing missing ),] or element in expression
This doesnt work either, no error, but my "golfLengte" column stays empty

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.