Too many line continuations - SQL server code

Rowanhf12

New Member
Joined
Jan 22, 2016
Messages
27
Code:
With ActiveWorkbook.Connections("Invoiced Sales").OLEDBConnection
        .BackgroundQuery = False
        .CommandText = Array( _
        "Select" & Chr(13) & "" & Chr(10) & "RBC.Year," & Chr(13) & "" & Chr(10) & "RBC.Period," & Chr(13) & "" & Chr(10) & "case when " & Chr(13) & "" & Chr(10) & "OD.OrderType = 'Return Order' then 'Returns'" & Chr(13) & "" & Chr(10) & "else 'Gross Sales'" & Chr(13) & "" & Chr(10) & "end a" _
        , _
        "s 'Order Type'," & Chr(13) & "" & Chr(10) & "p.Department," & Chr(13) & "" & Chr(10) & "p.Manufacturer," & Chr(13) & "" & Chr(10) & "p.CategoryName," & Chr(13) & "" & Chr(10) & "" & Chr(13) & "" & Chr(10) & "SUM(od.OrderedPrice/od.VATRate )  as ReportedSa" _
        , _
        "les," & Chr(13) & "" & Chr(10) & "SUM(od.OrderedPrice/od.VATRate - (OD.QuantityDespatched * isnull(HSCR.LastCost,P.LastCost))) as ReportedMargi" _
        , _
        "n" & Chr(13) & "" & Chr(10) & "" & Chr(13) & "" & Chr(10) & "From" & Chr(13) & "" & Chr(10) & "[WiggleBI].dbo.DevOrderDetail od" & Chr(13) & "" & Chr(10) & "left join [Wiggle-reporting].dbo.tblCombinedProductStockOrderSales as " _
        , _
        "P on od.ProductID = P.ProductID" & Chr(13) & "" & Chr(10) & "left join [wiggle-reporting].dbo.RBCalender RBC on CONVERT (date, od.InvoiceDate, " _
        , _
        "103) = RBC.Date" & Chr(13) & "" & Chr(10) & "left join [Wiggle-reporting].dbo.tblHistStockCostReceiptDate as HSCR on od.ProductID = HSCR.Produc" _
        , _
        "tID and rbc.Abs_Month = HSCR.AbsMonth" & Chr(13) & "" & Chr(10) & "left join [wiggle-reporting].dbo.RBKeyCountries2 KC on od.CountryID = KC.Cou" _
        , _
        "ntryID" & Chr(13) & "" & Chr(10) & "join [wiggle-reporting].dbo.udt_constantcurrencyrates c on od.CurrencyCode = c.currency" & Chr(13) & "" & Chr(10) & "join [wiggleBI].db" _
        , _
        "o.V6_Orders d on od.OrderID = d.OrderID" & Chr(13) & "" & Chr(10) & "left join (select Products, SUM(Quantity) AS PackSize from [wiggleBI].dbo." _
        , _
        "V6_ProductAssembly group by Products) PA on OD.ProductID = PA.Products " & Chr(13) & "" & Chr(10) & "" & Chr(13) & "" & Chr(10) & "Where" & Chr(13) & "" & Chr(10) & "RBC.Year >= 2015" & Chr(13) & "" & Chr(10) & "and" & Chr(13) & "" & Chr(10) & "P.Divisi" _
        , _
        "on not in ( 'Other' , 'Unknown' )" & Chr(13) & "" & Chr(10) & "and od.QuantityDespatched != 0" & Chr(13) & "" & Chr(10) & "" & Chr(13) & "" & Chr(10) & "Group by" & Chr(13) & "" & Chr(10) & "RBC.Year," & Chr(13) & "" & Chr(10) & "RBC.Period," & Chr(13) & "" & Chr(10) & "case when " & Chr(13) & "" & Chr(10) & "" _
        , _
        "OD.OrderType = 'Return Order' then 'Returns'" & Chr(13) & "" & Chr(10) & "else 'Gross Sales'" & Chr(13) & "" & Chr(10) & "end," & Chr(13) & "" & Chr(10) & "p.Department," & Chr(13) & "" & Chr(10) & "p.Manufacturer," & Chr(13) & "" & Chr(10) & "p.CategoryN" _
        , "ame")
        .CommandType = xlCmdSql
        .Connection = Array( _
        "OLEDB;Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=True;Initial Catalog=wiggle-reporting;Data Source=sqlexttrade" _
        , _
        "pack;Use Procedure for Prepare=1;Auto Translate=True;Packet Size=4096;Workstation ID=NH-LAPTOP-153;Use Encryption for Data=False" _
        , ";Tag with column collation when possible=False")
        .RefreshOnFileOpen = False
        .SavePassword = False
        .SourceConnectionFile = ""
        .ServerCredentialsMethod = xlCredentialsMethodIntegrated
        .AlwaysUseConnectionFile = False
    End With
    With ActiveWorkbook.Connections("Invoiced Sales")
        .Name = "Invoiced Sales"
        .Description = ""
    End With

Hi All

I have the above code linked to a SQL server, that produces a Pivot table directly.
At the moment, this code is set to the maximum amount of lines available (24 I believe)
'Cus when I try and add extra data fields to the SQL query, it tells me 'too many line continuations' in VBA.

I understand there is a way to work around this, so you can have as many lines for a SQL server query as you like.
From what I've read, it's something along the lines of:
strSql = line 1
line 2
....
line 24
strSql = strSql & line 25
line 26
....

I'm not sure how to apply this to my query, or if this method is possible.
Any help would be greatly appreciated

Kind regards.
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Like this for example:
Code:
Dim sSQL As String

sSQL = "Select" & Chr(13) & "" & Chr(10) & "RBC.Year," & Chr(13) & "" & Chr(10) & "RBC.Period," & Chr(13) & "" & Chr(10) & "case when " & Chr(13) & "" & Chr(10) & "OD.OrderType = 'Return Order' then 'Returns'" & Chr(13) & "" & Chr(10) & "else 'Gross Sales'" & Chr(13) & "" & Chr(10) & "end a"
sSQL = sSQL & "s 'Order Type'," & Chr(13) & "" & Chr(10) & "p.Department," & Chr(13) & "" & Chr(10) & "p.Manufacturer," & Chr(13) & "" & Chr(10) & "p.CategoryName," & Chr(13) & "" & Chr(10) & "" & Chr(13) & "" & Chr(10) & "SUM(od.OrderedPrice/od.VATRate )  as ReportedSa"
sSQL = sSQL & "les," & Chr(13) & "" & Chr(10) & "SUM(od.OrderedPrice/od.VATRate - (OD.QuantityDespatched * isnull(HSCR.LastCost,P.LastCost))) as ReportedMargi"
sSQL = sSQL & "n" & Chr(13) & "" & Chr(10) & "" & Chr(13) & "" & Chr(10) & "From" & Chr(13) & "" & Chr(10) & "[WiggleBI].dbo.DevOrderDetail od" & Chr(13) & "" & Chr(10) & "left join [Wiggle-reporting].dbo.tblCombinedProductStockOrderSales as "
sSQL = sSQL & "P on od.ProductID = P.ProductID" & Chr(13) & "" & Chr(10) & "left join [wiggle-reporting].dbo.RBCalender RBC on CONVERT (date, od.InvoiceDate, "
sSQL = sSQL & "103) = RBC.Date" & Chr(13) & "" & Chr(10) & "left join [Wiggle-reporting].dbo.tblHistStockCostReceiptDate as HSCR on od.ProductID = HSCR.Produc"
sSQL = sSQL & "tID and rbc.Abs_Month = HSCR.AbsMonth" & Chr(13) & "" & Chr(10) & "left join [wiggle-reporting].dbo.RBKeyCountries2 KC on od.CountryID = KC.Cou"
sSQL = sSQL & "ntryID" & Chr(13) & "" & Chr(10) & "join [wiggle-reporting].dbo.udt_constantcurrencyrates c on od.CurrencyCode = c.currency" & Chr(13) & "" & Chr(10) & "join [wiggleBI].db"
sSQL = sSQL & "o.V6_Orders d on od.OrderID = d.OrderID" & Chr(13) & "" & Chr(10) & "left join (select Products, SUM(Quantity) AS PackSize from [wiggleBI].dbo."
sSQL = sSQL & "V6_ProductAssembly group by Products) PA on OD.ProductID = PA.Products " & Chr(13) & "" & Chr(10) & "" & Chr(13) & "" & Chr(10) & "Where" & Chr(13) & "" & Chr(10) & "RBC.Year >= 2015" & Chr(13) & "" & Chr(10) & "and" & Chr(13) & "" & Chr(10) & "P.Divisi"
sSQL = sSQL & "on not in ( 'Other' , 'Unknown' )" & Chr(13) & "" & Chr(10) & "and od.QuantityDespatched != 0" & Chr(13) & "" & Chr(10) & "" & Chr(13) & "" & Chr(10) & "Group by" & Chr(13) & "" & Chr(10) & "RBC.Year," & Chr(13) & "" & Chr(10) & "RBC.Period," & Chr(13) & "" & Chr(10) & "case when " & Chr(13) & "" & Chr(10) & ""
sSQL = sSQL & "OD.OrderType = 'Return Order' then 'Returns'" & Chr(13) & "" & Chr(10) & "else 'Gross Sales'" & Chr(13) & "" & Chr(10) & "end," & Chr(13) & "" & Chr(10) & "p.Department," & Chr(13) & "" & Chr(10) & "p.Manufacturer," & Chr(13) & "" & Chr(10) & "p.CategoryN"
sSQL = sSQL & "ame"
With ActiveWorkbook.Connections("Invoiced Sales").OLEDBConnection
        .BackgroundQuery = False
        .CommandText = sSQL
 
Upvote 0
I think you seriously need to get a handle on your SQL query for the sake of code maintenance :)

Code:
Dim sSQL As String

sSQL = "Select      RBC.Year," & vbCrLf
sSQL = sSQL & "            RBC.Period," & vbCrLf
sSQL = sSQL & "            case when OD.OrderType = 'Return Order' then 'Returns' else 'Gross Sales' end as 'Order Type'," & vbCrLf
sSQL = sSQL & "            p.Department," & vbCrLf
sSQL = sSQL & "            p.Manufacturer," & vbCrLf
sSQL = sSQL & "            p.CategoryName," & vbCrLf
sSQL = sSQL & "            SUM(od.OrderedPrice/od.VATRate ) as ReportedSales," & vbCrLf
sSQL = sSQL & "            SUM(od.OrderedPrice/od.VATRate - (OD.QuantityDespatched * isnull(HSCR.LastCost,P.LastCost))) as ReportedMargin" & vbCrLf
sSQL = sSQL & "From        [WiggleBI].dbo.DevOrderDetail od" & vbCrLf
sSQL = sSQL & "left join   [Wiggle-reporting].dbo.tblCombinedProductStockOrderSales as P on od.ProductID = P.ProductID" & vbCrLf
sSQL = sSQL & "left join   [wiggle-reporting].dbo.RBCalender RBC on CONVERT (date, od.InvoiceDate, 103) = RBC.Date" & vbCrLf
sSQL = sSQL & "left join   [Wiggle-reporting].dbo.tblHistStockCostReceiptDate as HSCR on od.ProductID = HSCR.ProductID and rbc.Abs_Month = HSCR.AbsMonth" & vbCrLf
sSQL = sSQL & "left join   [wiggle-reporting].dbo.RBKeyCountries2 KC on od.CountryID = KC.CountryID" & vbCrLf
sSQL = sSQL & "join        [wiggle-reporting].dbo.udt_constantcurrencyrates c on od.CurrencyCode = c.currency" & vbCrLf
sSQL = sSQL & "join        [wiggleBI].dbo.V6_Orders d on od.OrderID = d.OrderID" & vbCrLf
sSQL = sSQL & "left join   (select Products, SUM(Quantity) AS PackSize from [wiggleBI].dbo.V6_ProductAssembly group by Products) PA on OD.ProductID = PA.Products" & vbCrLf
sSQL = sSQL & "Where       RBC.Year >= 2015" & vbCrLf
sSQL = sSQL & "and         P.Division not in ( 'Other' , 'Unknown' )" & vbCrLf
sSQL = sSQL & "and         od.QuantityDespatched != 0" & vbCrLf
sSQL = sSQL & "Group by    RBC.Year," & vbCrLf
sSQL = sSQL & "            RBC.Period," & vbCrLf
sSQL = sSQL & "            case when OD.OrderType = 'Return Order' then 'Returns' else 'Gross Sales' end," & vbCrLf
sSQL = sSQL & "            p.Department," & vbCrLf
sSQL = sSQL & "            p.Manufacturer," & vbCrLf
sSQL = sSQL & "            p.CategoryName" & vbCrLf

WBD
 
Upvote 0

Forum statistics

Threads
1,223,229
Messages
6,170,881
Members
452,364
Latest member
springate

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top