Access SQL Error

D Willett
SilverLounger
Posts: 1728
Joined: 25 Jan 2010, 08:34
Location: Stoke on Trent - Staffordshire - England

Access SQL Error

Post by D Willett »

Hey Guys
I'm getting syntax error in the following SQL Insert Into statement.
Its a routine to copy over a set of records from a form into another table (tblAddresses). I hate SQL !!!!!, can any of you guys assist?

Code: Select all

            If Me.RecordsetClone.RecordCount > 0 Then
                strSql = "INSERT INTO [tblAddresses] (CustomerID, Company, Last Name, First Name, E-mail Address, Job Title, Business Phone, Home Phone, Mobile Phone, Fax Number, Address, City, County, Postal Code, Notes) " & _
                    "SELECT CustomerID, Company, Last Name, First Name, E-mail Address, Job Title, Business Phone, Home Phone, Mobile Phone, Fax Number, Address, City, County, Postal Code, Notes" & _
                    "FROM [tblCustomers] WHERE CustomerID = " & Me.CustomerID & ";"
                CurrentDb.Execute strSql, dbFailOnError
            End If
Kind Regards
Cheers ...

Dave.

User avatar
HansV
Administrator
Posts: 78589
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Access SQL Error

Post by HansV »

Field names that contain spaces or punctuation must be enclosed in square brackets [ ]. And you forgot the space between Notes and FROM.

Code: Select all

                strSql = "INSERT INTO [tblAddresses] (CustomerID, Company, [Last Name], [First Name], " & _
                    "[E-mail Address], [Job Title], [Business Phone], [Home Phone], [Mobile Phone], " & _
                    "[Fax Number], Address, City, County, [Postal Code], Notes) " & _
                    "SELECT CustomerID, Company, [Last Name], [First Name], [E-mail Address], " & _
                    "[Job Title], [Business Phone], [Home Phone], [Mobile Phone], [Fax Number], " & _
                    "Address, City, County, [Postal Code], Notes " & _
                    "FROM [tblCustomers] WHERE CustomerID = " & Me.CustomerID & ";"
Best wishes,
Hans

Mark L
3StarLounger
Posts: 331
Joined: 11 Feb 2010, 03:55
Location: Land O Lakes, FL

Re: Access SQL Error

Post by Mark L »

As Hans said, you need to use brackets with non-conforming object names.

I'd suggest you stick to using letters, numbers, and the underscore character when naming objects. It will make your life a lot easier in the long run; and it will require a lot less typing (always a big plus for me)!

I'd also suggest that you learn to embrace SQL. When I first learned Access, I thought the same thing. Who needs it? After all, that's what the query grid is for. But I quickly learned otherwise! If your intent is to be a real Access developer, you have to know SQL.
Mark Liquorman
Land O Lakes, FL
see my website http://www.liquorman.net for Access Tips and Tricks, and for my Liquorman Utilities.

D Willett
SilverLounger
Posts: 1728
Joined: 25 Jan 2010, 08:34
Location: Stoke on Trent - Staffordshire - England

Re: Access SQL Error

Post by D Willett »

Thank you guys.
I should have known about the spaces !! SQL has always defeated me, perhaps because of no intellisense for a guide.
The table in question has been inherited so I'm tasked with trying to tidy it up, my usual naming convention contains no spaces on fields in the table design which I've learned over the years by some catastrophic errors..
Suppose we've all been there..

Thank you again for the help and support guys, its very much appreciated.
Merry Xmas all :-)
Cheers ...

Dave.

Mark L
3StarLounger
Posts: 331
Joined: 11 Feb 2010, 03:55
Location: Land O Lakes, FL

Re: Access SQL Error

Post by Mark L »

Also be careful of dashes, as in E-Mail. Without brackets, Access reads that to be "E minus Mail"!

A good addition to any professional developers toolbox is Rick Fisher's "Find and Replace". This allows you to make wholesale changes in a database quickly and easily.
Mark Liquorman
Land O Lakes, FL
see my website http://www.liquorman.net for Access Tips and Tricks, and for my Liquorman Utilities.

D Willett
SilverLounger
Posts: 1728
Joined: 25 Jan 2010, 08:34
Location: Stoke on Trent - Staffordshire - England

Re: Access SQL Error

Post by D Willett »

Thanks Mark.
Cheers ...

Dave.