2

I am fairly new to using VBA in MS Access and I am having trouble embedding SQL statements into VBA code. I have a database with almost 200 tables, and I would like to change the data type of one column (named lake) in each table to a "text" data type. I wrote the following code, but keep getting a syntax error for the ALTER TABLE statement.

Public Sub changeDataType()

Dim db As DAO.Database
Dim table As DAO.TableDef

Set db = CurrentDb

  For Each table In db.TableDefs
     DoCmd.RunSQL "ALTER TABLE" & table.Name & "ALTER COLUMN [lake] TEXT(100);"
  Next

Set db = Nothing

End Sub

Can anyone tell me why I'm getting the syntax error?

Thanks,
Paul

1 Answer 1

3

this statement will not be correct:

DoCmd.RunSQL "ALTER TABLE" & table.Name & "ALTER COLUMN [lake] TEXT(100);"

if, for instance "table.Name" = "myTable", the resulting statement will look like this:

DoCmd.RunSQL "ALTER TABLEmyTableALTER COLUMN [lake] TEXT(100);"

try adding a space to separate the name, like this:

DoCmd.RunSQL "ALTER TABLE [" & table.Name & "] ALTER COLUMN [lake] TEXT(100);"
Sign up to request clarification or add additional context in comments.

4 Comments

Good catch. You might want to go one step further and suggest ALTER TABLE [" & table.Name & "] ..." for added safety.
aha ... good as well ... I'll edit the answer to include ... thanks.
Thanks all. Gord, with your additional suggestion I got it to work...at least I think...Access is still running it. Thanks to both
should be able to ... what's the error message you are getting?

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.