Adding Field with Spaces

stew32cu4

New member
Local time
Today, 15:14
Joined
Aug 9, 2012
Messages
2
I have used this forum many times to help find answers, unfortunately I have searched high and low to no avail for this issue.

I need at add a field to a table with spaces in the name. I have the [] around the field name but I keep getting the "Error: 3292 Syntax error in field definition". The code works fine using CamelCase, but that would cause more work to correct later.

Red highlighted text is where I encounter the error. Thanks in advance.

Code:
Private Sub cmdMatrixUpdate_Click()
strNewField = InputBox("Enter the New Map Name") 'Set the input to variable
strAddField = "ALTER TABLE Web_Matrix_Table ADD COLUMN " & [strNewField] & " YESNO;"
strInsertSQL = "INSERT INTO ReportLookup (Report) VALUES ('" & [strNewField] & "');"
If strNewField = "" Then
    Exit Sub
Else
    [COLOR=red]DoCmd.RunSQL strAddField[/COLOR] 'Adding the new field to the table
    DoCmd.RunSQL strInsertSQL 'Inserting input vairable to ReportLookup Table
End If
Set db = CurrentDb
Set tdf = db.TableDefs("Web_Matrix_Table")
Set field = tdf.Fields(strNewField)
Set property = field.CreateProperty("DisplayControl", dbInteger, 106)
field.Properties.Append property
End Sub
 
You need to put the brackets inside the string delimiters for the Alter table query;

strAddField = "ALTER TABLE Web_Matrix_Table ADD COLUMN [" & strNewField & "] YESNO;"
 
Eureka!! It's the simple things that get ya.

Thanks Beetle.
 
Welcome aboard :)
You never NEED to create column names that will cause you problems with forms/reports/code. Best practice is to use only letters, numbers, and the underscore. Spaces and special characters must be eliminated when working with code and so Access translates them all to underscores when creating procedure names and forces you to use square brackets in others.
 

Users who are viewing this thread

Back
Top Bottom