|
-
Feb 18th, 2016, 03:35 AM
#1
Thread Starter
Lively Member
[RESOLVED] Syntax Error in field definition.
Hi,
I am trying to add a column to a table.
It was working fine and now suddenly i get an error message " Syntax Error in field definition"
Code:
Dim Sql As String
Dim tbl As String
Dim col As String
Dim rtl As String
tbl = Me.Combo67
col = Me.Combo65
Sql = "ALTER TABLE " & [tbl] & " ADD COLUMN " & [col] & " Text(128) "
rtl = MsgBox("ADD " & col & " TO " & tbl & " ?", vbYesNo + vbExclamation, "PLEASE CONFIRM")
MsgBox Sql
Select Case rtl
Case 6
DoCmd.RunSQL Sql
Case 7
End
End Select
End Sub
Im taking the table Name and the column Name from a Combo box.
how can I rectify this error??
Thanks!!
Last edited by saranmc; Feb 18th, 2016 at 03:42 AM.
-
Feb 18th, 2016, 04:34 AM
#2
Re: Syntax Error in field definition.
i guess if the new column name could contain a space, such an error could occur
are you sure the [] are required /allowed, i would think they should be omitted
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Feb 18th, 2016, 05:40 AM
#3
Thread Starter
Lively Member
Re: Syntax Error in field definition.
i checked westconn1... no difference with or without []...
but you are partly right, the error occurs due to the spaces.
i checked with a column Name without spaces and it works!
but with spaces or Special characters it does not.
so what can be done?? the new Name could contain spaces...
-
Feb 18th, 2016, 06:41 AM
#4
Re: Syntax Error in field definition.
From MS:
Names of fields, controls, and objects in Microsoft Access:
Can be up to 64 characters long.
Can include any combination of letters, numbers, spaces, and special characters except a period (.), an exclamation point (!), an accent grave (`), and brackets ([ ]).
Can't begin with leading spaces.
Can't include control characters (ASCII values 0 through 31).
Can't include a double quotation mark (") in table, view, or stored procedure names in a Microsoft Access project.
Although you can include spaces in field, control, and object names, most examples in the Microsoft Access documentation show field and control names without spaces because spaces in names can produce naming conflicts in Microsoft Visual Basic for Applications in some circumstances.
-
Feb 18th, 2016, 06:51 AM
#5
Re: Syntax Error in field definition.
it is better to eliminate spaces or replace spaces with underscore, but you can try enclosing field names that contain spaces within single quotes
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Feb 18th, 2016, 07:48 AM
#6
Re: Syntax Error in field definition.
try
"ALTER TABLE " & "[" & tbl & "]" & " ADD COLUMN "
do not put off till tomorrow what you can put off forever
-
Feb 18th, 2016, 08:14 AM
#7
Thread Starter
Lively Member
Re: Syntax Error in field definition.
yes i tried to enclose them in single quotes but in VBA Editor it does not allow me to,....
the line turns red...
Code:
Sql = "ALTER TABLE" & 'tbl' & "ADD COLUMN" & 'col' & text(10)"
the error is with the single quotes
-
Feb 18th, 2016, 08:19 AM
#8
Thread Starter
Lively Member
Re: Syntax Error in field definition.
It still does not work Ikke......
-
Feb 18th, 2016, 08:33 AM
#9
Re: Syntax Error in field definition.
This adds a column named "new column2" for me into table1:
Code:
Sub addCol()
Dim db As Database
Dim strSQL As String
Dim colName As String
Set db = CurrentDb
colName = "'new column2'"
strSQL = "ALTER TABLE table1 ADD " & colName & " VARCHAR(5) NULL ;"
DoCmd.RunSQL strSQL
Set db = Nothing
End Sub
(which is what Pete said in post #5)
-
Feb 18th, 2016, 08:39 AM
#10
Re: Syntax Error in field definition.
 Originally Posted by IkkeEnGij
try
"ALTER TABLE " & "[" & tbl & "]" & " ADD COLUMN "
 Originally Posted by saranmc
yes i tried to enclose them in single quotes but in VBA Editor it does not allow me to,....
the line turns red...
Code:
Sql = "ALTER TABLE" & 'tbl' & "ADD COLUMN" & 'col' & text(10)"
the error is with the single quotes
 Originally Posted by saranmc
It still does not work Ikke......
1) Um... I think you missed it... the quotes were to go around the field name INSIDE THE STRING... not around the variable..
2) don't use tick marks (single quotes) as that denotes a string... not an object name...
3) Square brackets IS what you want but you need to make sure it goes around the object name in the SQL string, NOT AROUND THE VARIABLE...
Ike almost had it, it just needed to be expanded:
Code:
"ALTER TABLE " & "[" & tbl & "]" & " ADD COLUMN [" & col & "] Text(128) "
What you're after is a string that ends up looking like this:
ALTER TABLE [tblSome Table] ADD COLUMN [some col] Text(128)
See how the brackets are around the object (table and col name)? that's what you're after.
-tg
-
Feb 18th, 2016, 08:50 AM
#11
Thread Starter
Lively Member
Re: Syntax Error in field definition.
That #7 post was for westconn1 and not Ikke...
Anyways... now it works perfectly... thanks for clearly things up techgnome....
Thanks guys for your valuable ideas...
i move on in my Project, will be back if i have more Trouble..
cheers!!!
saran
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|