我在Excel中创建了CommandButton
并开始编码VBA
。
我的想法是将参数传递给我的CommandString
,以便用户可以过滤。
2个参数字段的数据类型为smalldatetime
SQL
以下是我的Click
之后执行的VBA代码:CommandButton
:
Private Sub CommandButton1_Click()
Dim FromDate As Date
Dim ToDate As Date
FromDate = Sheets("Bips Travel Summary").Range("J3").Value
ToDate = Sheets("Bips Travel Summary").Range("J4").Value
'Pass the Parameters values to the Stored Procedure used in the Data Connection
With ActiveWorkbook.Connections("192.168.0.3 Timesheets1").OLEDBConnection
.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],TS.DateWorked AS [DateTraveled],C.Customer_Name,NULL AS [WT],EC.AA_Rate AS [Amount Per Kilometer],NULL AS [Currency],TS.Travel AS [Number (Amount of km)],TT.TravelDescription,TS.Travel * CONVERT(float, EC.AA_Rate) AS [Total (per record)] FROM [Timesheets].[dbo].[timesheets] TS INNER JOIN [Timesheets].[dbo].[traveltype] TT ON TS.TravelTypeCode = TT.TravelTypeCode 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 '" & FromDate & "' AND '" & ToDate & "') as A GROUP BY ID, Employee, WT, [Amount Per Kilometer], Currency"
ActiveWorkbook.Connections("192.168.0.3 Timesheets1").Refresh
End With
End Sub
输入FromDate
的值为20100101
和ToDate
为20150813
我收到一条错误消息,该消息会落在此脚本上:
FromDate = Sheets("Bips Travel Summary").Range("J3").Value
错误消息显示:
Runtime error '13':
Type mismatch
我不知道从哪里开始,因为我是VBA
的新手。
有谁能指出我正确的方向来解决这个问题?
答案 0 :(得分:0)
错误表明您正在尝试分配错误的数据类型。
变量FromDate
和ToDate
被声明为Date
类型,但您正在尝试为其分配文本。
如果您的单元格中有此格式的日期:' 20100101'和' 20150813',您需要在分配给以下变量之前将它们转换为日期:
Private Sub CommandButton1_Click()
Dim txtFromDate As String
Dim txtToDate As String
Dim FromDate As Date
Dim ToDate As Date
txtFromDate = Sheets("Bips Travel Summary").Range("J3").Value
FromDate = DateSerial(Left(txtFromDate, 4), Mid(txtFromDate, 5, 2), Right(txtFromDate, 2))
txtToDate = Sheets("Bips Travel Summary").Range("J4").Value
ToDate = DateSerial(Left(txtToDate, 4), Mid(txtToDate, 5, 2), Right(txtToDate, 2))
'Pass the Parameters values to the Stored Procedure used in the Data Connection
With ActiveWorkbook.Connections("192.168.0.3 Timesheets1").OLEDBConnection
.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],TS.DateWorked AS [DateTraveled],C.Customer_Name,NULL AS [WT],EC.AA_Rate AS [Amount Per Kilometer],NULL AS [Currency],TS.Travel AS [Number (Amount of km)],TT.TravelDescription,TS.Travel * CONVERT(float, EC.AA_Rate) AS [Total (per record)] FROM [Timesheets].[dbo].[timesheets] TS INNER JOIN [Timesheets].[dbo].[traveltype] TT ON TS.TravelTypeCode = TT.TravelTypeCode 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 '" & FromDate & "' AND '" & ToDate & "') as A GROUP BY ID, Employee, WT, [Amount Per Kilometer], Currency"
ActiveWorkbook.Connections("192.168.0.3 Timesheets1").Refresh
End With
End Sub