将VBA代码继续到新行

时间:2015-08-14 04:51:41

标签: sql excel vba excel-vba

我在VBACommandButton后面写了Excel代码,但似乎CommandText只能容纳一定数量的字符。

这是我的代码(在一行上):

.CommandText = "SELECT ID,Employee,WT,[Amount Per Kilometer],Currency,SUM([Number (Amount of km)]) AS [Number (Amount of km)],SUM([Total (per record)]) AS [Total (per record)] FROM (SELECT S.ID,S.FirstName + ' ' + S.LastName AS [Employee],C.Customer_Name,NULL AS [WT],EC.AA_Rate AS [Amount Per Kilometer],NULL AS [Currency],TS.Travel AS [Number (Amount of km)],TS.Travel * CONVERT(float,EC.AA_Rate) AS [Total (per record)] FROM [Timesheets].[dbo].[timesheets] TS INNER JOIN [Timesheets].[dbo].[staff] S ON TS.Staff_Code = S.Staff_Code INNER JOIN [Timesheets].[dbo].[enginecapacity] EC ON TS.EngineCapacityCode = EC.EngineCapacityCode INNER JOIN [Timesheets].[dbo].[customers] C ON TS.Cust_Code = C.Cust_Code WHERE TS.DateWorked BETWEEN LEFT('"& FromDate &"', 4) + SUBSTRING('"& FromDate &"',CHARINDEX('/','"& FromDate &"')+1,LEN('"& FromDate &"') - CHARINDEX('/','2014/12/31') - CHARINDEX('/',REVERSE('2014/12/31'))) + RIGHT('"& FromDate &"',2) AND LEFT('"& ToDate &"', 4) + SUBSTRING('"& ToDate &"',CHARINDEX('/','"& ToDate"')+1,LEN('"& ToDate &"') - CHARINDEX('/','"& ToDate &"') - CHARINDEX('/',REVERSE('"& ToDate &"'))) + RIGHT('"& ToDate &"',2)) as A GROUP BY ID,Employee,WT,[Amount Per Kilometer],Currency"

我需要将上面的代码分成2行或3行。

这就是我的尝试:

.CommandText = "SELECT ID, " & Resp & _
        "Employee, " & Resp & _
        "WT, " & Resp & _
        "[Amount Per Kilometer], " & Resp & _
        "Currency, " & Resp & _
        "SUM([Number (Amount of km)]) AS [Number (Amount of km)], " & Resp & _
        "SUM([Total (per record)]) AS [Total (per record)] " & Resp & _ 
        "FROM ( " & Resp & _
        "SELECT S.ID, " & Resp & _
        "S.FirstName + ' ' + S.LastName AS [Employee], " & Resp & _
        "C.Customer_Name, " & Resp & _
        "NULL AS [WT], " & Resp & _
        "EC.AA_Rate AS [Amount Per Kilometer], " & Resp & _
        "NULL AS [Currency], " & Resp & _
        "TS.Travel AS [Number (Amount of km)], " & Resp & _
        "TS.Travel * CONVERT(float,EC.AA_Rate) AS [Total (per record)] " & Resp & _
        "FROM [Timesheets].[dbo].[timesheets] TS " & Resp & _ 
        "INNER JOIN [Timesheets].[dbo].[staff] S ON TS.Staff_Code = S.Staff_Code " & Resp & _
        "INNER JOIN [Timesheets].[dbo].[enginecapacity] EC ON TS.EngineCapacityCode = EC.EngineCapacityCode " & Resp & _ 
        "INNER JOIN [Timesheets].[dbo].[customers] C ON TS.Cust_Code = C.Cust_Code " & Resp & _
        "WHERE TS.DateWorked BETWEEN " & Resp & _
        "LEFT('"& FromDate &"', 4) + SUBSTRING('"& FromDate &"',CHARINDEX('/','"& FromDate &"')+1,LEN('"& FromDate &"') - CHARINDEX('/','2014/12/31') - CHARINDEX('/',REVERSE('2014/12/31'))) + RIGHT('"& FromDate &"',2) " & Resp & _ 
        "AND LEFT('"& ToDate &"', 4) + SUBSTRING('"& ToDate &"',CHARINDEX('/','"& ToDate"')+1,LEN('"& ToDate &"') - CHARINDEX('/','"& ToDate &"') - CHARINDEX('/',REVERSE('"& ToDate &"'))) + RIGHT('"& ToDate &"',2)) " & Resp & _ 
        "as A GROUP BY ID,Employee,WT,[Amount Per Kilometer],Currency"

运行上述内容时我得到Syntax Error

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:2)

问题是在倒数第二行的第3个&之后缺少ToDate