Hello.
I am writing some VBA so it can grab the information i require from the database and put it into excel. No problem really, except for one apparent simple hurdle. One of my SQL statements as shown below, is far to large to fit onto one line, thus it tells me the syntax is incorrect. I have tried to break the line into multiple lines by using.
"+
_
Unfortunately it still tells me the syntax is incorrect. Any idea's? Thanks in advance
obj.Execute ("select csbgrp, csbwhseno, csbyear, csbtype, csbbudget1, 1 period from salesbudget where csbyear = " & chr(34) & "2007" & chr(34) & " and csbwhseno = " & chr(34) & "ELT" & chr(34) & " group by 1,2,3,4,5 union all select csbgrp, csbwhseno, csbyear, csbtype, csbbudget2, 2 period from salesbudget where csbyear = " & chr(34) & "2007" & chr(34) & " and csbwhseno = " & chr(34) & "ELT" & chr(34) & " group by 1,2,3,4,5 union all select csbgrp, csbwhseno, csbyear, csbtype, csbbudget3, 3 period from salesbudget where csbyear = " & chr(34) & "2007" & chr(34) & " and csbwhseno = " & chr(34) & "ELT" & chr(34) & " group by 1,2,3,4,5 union all select csbgrp, csbwhseno, csbyear, csbtype, csbbudget4, 4 period from salesbudget where csbyear = " & chr(34) & "2007" & chr(34) & " and csbwhseno = " & chr(34) & "ELT" & chr(34) & " group by 1,2,3,4,5 union all select csbgrp, csbwhseno, csbyear, csbtype, csbbudget5, 5 period from salesbudget where csbyear = " & chr(34) & "2007" & chr(34) & " and csbwhseno = " & chr(34) & "ELT" & chr(34) & " group by 1,2,3,4,5 union all select csbgrp, csbwhseno, csbyear, csbtype, csbbudget6, 6 period from salesbudget where csbyear = " & chr(34) & "2007" & chr(34) & " and csbwhseno = " & chr(34) & "ELT" & chr(34) & " group by 1,2,3,4,5 union all select csbgrp, csbwhseno, csbyear, csbtype, csbbudget7, 7 period from salesbudget where csbyear = " & chr(34) & "2007" & chr(34) & " and csbwhseno = " & chr(34) & "ELT" & chr(34) & " group by 1,2,3,4,5 union all select csbgrp, csbwhseno, csbyear, csbtype, csbbudget8, 8 period from salesbudget where csbyear = " & chr(34) & "2007" & chr(34) & " and csbwhseno = " & chr(34) & "ELT" & chr(34) & " group by 1,2,3,4,5 union all select csbgrp, csbwhseno, csbyear, csbtype, csbbudget9, 9 period from salesbudget where csbyear = " & chr(34) & "2007" & chr(34) & " and csbwhseno = " & chr(34) & "ELT" & chr(34) & " group by 1,2,3,4,5 union all select csbgrp, csbwhseno, csbyear, csbtype, csbbudget10, 10 period from salesbudget where csbyear = " & chr(34) & "2007" & chr(34) & " and csbwhseno = " & chr(34) & "ELT" & chr(34) & " group by 1,2,3,4,5 union all select csbgrp, csbwhseno, csbyear, csbtype, csbbudget11, 11 period from salesbudget where csbyear = " & chr(34) & "2007" & chr(34) & " and csbwhseno = " & chr(34) & "ELT" & chr(34) & " group by 1,2,3,4,5 union all select csbgrp, csbwhseno, csbyear, csbtype, csbbudget12, 12 period from salesbudget where csbyear = " & chr(34) & "2007" & chr(34) & " and csbwhseno = " & chr(34) & "ELT" & chr(34) & " group by 1,2,3,4,5 into temp a4;")
by: matthewspatrickPosted on 2007-06-25 at 14:47:30ID: 19359516
VBA has a limit on the number of characters allowed per "line" of code
(the underscore does not create a new "line" of code--it just indicates
a line continuation). Apparently, you've exceeded it.
Try breaking up the SQL into a few parts:
sql1 = "..."
sql2 = "..."
obj.Execute sql1 & sql2