Results 1 to 11 of 11

Thread: [RESOLVED] Syntax Error in field definition.

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2016
    Posts
    90

    Resolved [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.

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    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

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Feb 2016
    Posts
    90

    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...

  4. #4
    PowerPoster
    Join Date
    Oct 2008
    Location
    Midwest Region, United States
    Posts
    3,574

    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.

  5. #5
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    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

  6. #6
    Frenzied Member
    Join Date
    Jun 2014
    Posts
    1,084

    Re: Syntax Error in field definition.

    try
    "ALTER TABLE " & "[" & tbl & "]" & " ADD COLUMN "
    do not put off till tomorrow what you can put off forever

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Feb 2016
    Posts
    90

    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

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Feb 2016
    Posts
    90

    Re: Syntax Error in field definition.

    It still does not work Ikke......

  9. #9
    PowerPoster
    Join Date
    Oct 2008
    Location
    Midwest Region, United States
    Posts
    3,574

    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)

  10. #10
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Syntax Error in field definition.

    Quote Originally Posted by IkkeEnGij View Post
    try
    "ALTER TABLE " & "[" & tbl & "]" & " ADD COLUMN "
    Quote Originally Posted by saranmc View Post
    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
    Quote Originally Posted by saranmc View Post
    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Feb 2016
    Posts
    90

    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
  •  



Click Here to Expand Forum to Full Width