Adding Field with Spaces (1 Viewer)

stew32cu4

New member
Local time
Yesterday, 19:25
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
 

Beetle

Duly Registered Boozer
Local time
Yesterday, 17:25
Joined
Apr 30, 2011
Messages
1,808
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;"
 

stew32cu4

New member
Local time
Yesterday, 19:25
Joined
Aug 9, 2012
Messages
2
Eureka!! It's the simple things that get ya.

Thanks Beetle.
 

Pat Hartman

Super Moderator
Staff member
Local time
Yesterday, 19:25
Joined
Feb 19, 2002
Messages
43,266
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

Top Bottom