I tested and also get the syntax error on the ALTER TABLE.
It is modifying TableDefs instead of using an SQL action. Here is example of code from my project that adds a new table to backend and alters some properties. Thought your error would be a simple fix and would not need my example but maybe you will find it helpful. It opens a connection to the backend to create and alter table.
Code:
Private Sub btnBuild_Click()
'On Error GoTo Err_proc
Dim tdf As TableDef
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim db As DAO.Database
Set rs = New ADODB.Recordset
Set cn = New ADODB.Connection
If IsNull(Me.tbxTestNum) Then
MsgBox "Must enter test number.", vbCritical, "Error"
Else
'Create the Table
cn.Open "Provider=Microsoft.ACE.OLEDB.12.0; Data Source='\\dotatufs02\CRM\Lab\Database\Data\LabData.accdb'"
cn.Execute "CREATE TABLE " & Me.tbxTestNum & " (LabNum text(12) PRIMARY KEY Not Null, method text(30) Not Null);"
With CurrentDb
Set tdf = .CreateTableDef(Me.tbxTestNum)
tdf.SourceTableName = Me.tbxTestNum
tdf.Connect = "; DATABASE=\\dotatufs02\CRM\Lab\Database\Data\LabData.accdb"
.TableDefs.Append tdf
End With
Me.tbxTestNum.SetFocus
rs.Open "SELECT * FROM BuildTable;", CurrentProject.Connection, adOpenStatic, adLockPessimistic
'must use DAO to set AllowZeroLength property
Set db = DBEngine.OpenDatabase("\\dotatufs02\CRM\Lab\Database\Data\LabData.accdb")
While Not rs.EOF
If rs!DataField <> "LabNum" And rs!DataField <> "method" Then
cn.Execute "ALTER TABLE " & Me.tbxTestNum & " ADD COLUMN " & _
rs!DataField & " " & IIf(rs!DataType = "Boolean", "Bit", rs!DataType) & _
IIf(rs!DataType = "Text", "(" & rs!FieldSize & ")", "") & ";"
End If
If rs!DataType = "Text" Then
db.TableDefs(Me.tbxTestNum).Fields(rs!DataField).AllowZeroLength = False
End If
rs.MoveNext
Wend
rs.Close
cn.Close
End If
Exit_proc:
Exit Sub
Err_Proc:
MsgBox "Error encountered in AddDataTable procedure btnBuild_Click - " & Err & " : " & Err.Description
Resume Exit_proc
End Sub