Issue with line continuation when running SQL in VBA (1 Viewer)

JDubya

Registered User.
Local time
Today, 17:29
Joined
Oct 16, 2007
Messages
39
Hi All, If a run the following SQL string from within VBA and the string is all on one line, it works fine:-

SQL = "UPDATE tblContactImport INNER JOIN Customers ON tblContactImport.cuco_Company = Customers.cu_ID SET tblContactImport.cuco_NewPhone = Mid(Customers.cu_PhoneNumber,1,5)+' '+Mid(tblContactImport.cuco_NewPhone,1,3)+' '+Mid(tblContactImport.cuco_NewPhone,4,3);"

However, when I break it up onto several lines using line continuation (see below) so that the code better fits the window I get a syntax error, missing operator message.

SQL = "UPDATE tblContactImport INNER JOIN Customers ON tblContactImport.cuco_Company = Customers.cu_ID" & _
"SET tblContactImport.cuco_NewPhone =" & _
"Mid(Customers.cu_PhoneNumber,1,5)+' '+Mid(tblContactImport.cuco_NewPhone,1,3)+' '+Mid(tblContactImport.cuco_NewPhone,4,3);"

I've also tried puting the & on the next line but still but the same error.

SQL = "UPDATE tblContactImport INNER JOIN Customers ON tblContactImport.cuco_Company = Customers.cu_ID" _
& "SET tblContactImport.cuco_NewPhone =" _
& "Mid(Customers.cu_PhoneNumber,1,5)+' '+Mid(tblContactImport.cuco_NewPhone,1,3)+' '+Mid(tblContactImport.cuco_NewPhone,4,3);"



Can anybody advise on what i'm doing wrong?
Cheers, John
 

Ranman256

Well-known member
Local time
Today, 12:29
Joined
Apr 9, 2015
Messages
4,337
Either use the underscores OR the ampersands, not both.....

SQL = "UPDATE tblContactImport INNER JOIN Customers ON Sql = tblContactImport.cuco_Company = Customers.cu_ID"
SQL = Sql & " SET tblContactImport.cuco_NewPhone ="
SQL = Sql & " Mid(Customers.cu_PhoneNumber,1,5) & ' ' & Mid(tblContactImport.cuco_NewPhone,1,3) & ' ' & Mid tblContactImport.cuco_NewPhone,4,3);"
 

CJ_London

Super Moderator
Staff member
Local time
Today, 17:29
Joined
Feb 19, 2013
Messages
16,614
you are missing spaces - this

SQL = "UPDATE tblContactImport INNER JOIN Customers ON tblContactImport.cuco_Company = Customers.cu_ID" & _
"SET
tblContactImport.cuco_NewPhone =" &

needs to be

"UPDATE tblContactImport INNER JOIN Customers ON tblContactImport.cuco_Company = Customers.cu_ID" & _
" SET
tblContactImport.cuco_NewPhone =" &
 

JDubya

Registered User.
Local time
Today, 17:29
Joined
Oct 16, 2007
Messages
39
Thanks Ranman256, I guess it is true that there's more than one way to skin a cat. For the record I would just like to confirm that I have never ever skinned a cat, do not ever intend skinning cats, or condone the skinning of cats.
 

JDubya

Registered User.
Local time
Today, 17:29
Joined
Oct 16, 2007
Messages
39
Hi CJ, thanks for the catch, it works perfectly! Cheers, John
 

JDubya

Registered User.
Local time
Today, 17:29
Joined
Oct 16, 2007
Messages
39
Hi Minty, that's a really useful tip and made it simple for me to see where i went wrong. I'd give you an official thanks for your post if I could but for some reason I don't have the option....I don't know if it's because I've already thanked CJ?
 

Minty

AWF VIP
Local time
Today, 17:29
Joined
Jul 26, 2013
Messages
10,371
I'm not sure tbh, the reputation/thanks thing is a bit odd on here, not that it bothers me. A thank you post is more than sufficient.
 

Users who are viewing this thread

Top Bottom