Etienne Borocco Etienne Borocco - 11 days ago 6
SQL Question

for-loop add columns using SQL in MS Access

I am trying to add n columns to a table, like in this example of code where

n = 10
:

Sub toto()
Dim db As Database, i As Integer
Set db = CurrentDb()

For i = 1 To i = 10
db.Execute " ALTER TABLE time_series " _
& "ADD COLUMN F_" & i & " Number;"
' End If
Next i
End sub


I tried to convert
i
in string with
CStr(i)
but to no avail. Any hint?

EDIT: No column is added.

Answer

I have tested your code, I do not see any issues except for the fact, your For statement is a bit off and that you needed to set the db object. Try this code.

Sub toto()
    Dim db As Database, i As Integer
    Set db = CurrentDb
    For i = 1 To 10
        db.Execute " ALTER TABLE time_series " _
                    & "ADD COLUMN F_" & i & " Number;"
    Next i
    Set db = Nothing
End Sub
Comments