我在Excel中使用excel-VBA中的ADO运行SQL。结果显示在Excel工作表上。
在工作表2014,2015,2016,2017中有一个名为Date的字段
数据示例:2/1 / 2014,7 / 1 / 2014,23 / 10/2014
此字段数据类型为日/月/年。日期由= DATE(cell1,cell2,cell3) - 年,月,日组合。表中的日期应全部为纯日期,因为我合并了3个单元格(年,月,日)到1个字段(日期)
'Dim two date variables
Dim fromDate As Date
Dim toDate As Date
'Dim 4 integer variables
Dim fromyear As Integer
Dim toyear As Integer
Dim frommonth As Integer
Dim ToMonth As Integer
'add combobox value
With FromYearC
.AddItem 2014
.AddItem 2015
.AddItem 2016
.AddItem 2017
End With
'add combobox value
With FromMonthC
.AddItem 1
.AddItem 2
.AddItem 3
.AddItem 4
.AddItem 5
.AddItem 6
.AddItem 7
.AddItem 8
.AddItem 9
.AddItem 10
.AddItem 11
.AddItem 12
End With
'Store combo box value into these 4 integer variables
fromyear = FromYearC.Value
frommonth = FromMonthC.Value
toyear = ToYearC.Value
ToMonth = ToMonthC.Value
'Now i want to combine these variables and store into fromDate and toDate
fromDate = DateSerial(fromyear, frommonth, 1)
toDate = DateSerial(toyear, ToMonth + 1, 1)
'it is still wrong , no matter orginal date format or new date format"dd/mm/yyyy"
fromDate = Format(fromDate, "dd/mm/yyyy")
toDate = Format(toDate, "dd/mm/yyyy")
VBA用户界面: 现在我想设置fromdate和todate之间的日期范围(包括两者)
我构建SQL引用: How to combine 3 VBA variable into date datatype in VBA
' This is the new SQL string
WHERE date >= #" & fromdate & "# AND date<#" & toDate & "#"
现在SQL结果完全错误(仍然有输出)。我猜SQL where子句有错误。由于SQL最初是正确的,因此where-date selection子句的SQL会出错。
或者我试图使用where-between子句。这仍然是错误的。
date between #" & fromDate & "# and #" & toDate & "#
完整的SQL在这里,但我认为它太长了
SELECT officer ,NULL, SUM(IIF( isnumeric(mkt) = true and Survey='CPI' and Activity='FI' and Outcome= 'C', Totalmin, 0 )/468) , SUM(IIF( isnumeric(Non) = true and Survey='CPI' and Activity='FI' and Outcome= 'C', Totalmin, 0 )/468) ,NULL ,NULL , IIF(ISNULL(sum(mkt)),0,sum(mkt)),Sum(Non),sum(ICP),(sum(mkt)+Sum(Non)+sum(ICP) ) ,NULL,NULL,NULL,count(IIF( Survey='CPI' and Activity='FI' ,Totalmin, NULL )),NULL,count(IIF( Survey='CPI' and Activity='FI' and (Outcome ='C' OR Outcome='D'OR Outcome='O') , Totalmin, NULL )),NULL,SUM(IIF( Survey='CPI' and Activity='FI' ,Totalmin, 0 )),NULL,SUM(IIF( Survey='CPI' and Activity='FI' and (Outcome ='C' OR Outcome='D') ,Totalmin, 0 )) From (select officer ,rank ,year ,month ,day , survey ,activity ,outcome ,mkt,non,totalmin,ICP ,date from [2014$] UNION ALL select officer ,rank ,year ,month ,day , survey ,activity ,outcome ,mkt,non,totalmin,ICP ,date from [2015$] UNION ALL select officer ,rank ,year ,month ,day , survey ,activity ,outcome ,mkt,non,totalmin,ICP ,date from
[2016$] UNION ALL select officer ,rank ,year ,month ,day , survey ,activity ,outcome ,mkt,non,totalmin,ICP ,date from [2017$])as table3 where officer is not null and officer <> '' and officer <> ' ' and date >= #" & fromDate & "# AND date<#" & toDate & "#" group by officer
我尝试使用硬代码
and date >= #1-3-2016# AND date<#31-3-2016#
测试SQL。
它像原始SQL一样失败。它可以输出结果,但结果是错误的。
我猜它与UNION ALL有关吗? 当我加入所有4个表记录成为一个大桌子。
我认为它与UNION ALL无关。 由于excel列日期合并为3个单元格 - 年,月,日,我再次复制并粘贴该值.SQL结果有变化。但是,结果仍然不正确。
然后我尝试使用硬代码进行测试。 例如,我尝试在2016年3月选择记录。 使用where where子句:
and month=3 and year=2016
好的。
然后我尝试使用下面的声明。这不是好的。
date >= #2016-03-01# AND date<#2016-04-01#
然后我相信它应该是excel或VBA中的日期数据类型问题。
然后,我尝试在下面进行这些测试:
对于Excel,原始数据类型为date .i更改为字符串,数字,常规...不行
日期样本:2016年6月1日 字符串示例:42375 数字样本:42375.00
对于VBA,我试图交换月和日。 - &GT; #2016-01-03#还不行
答案 0 :(得分:1)
您需要为fromDate和toDate日期创建String变量,并将它们传递给WHERE子句。我建议你使用ISO日期格式。除了避免列名称日期出现问题之外,让我们尝试将日期列括在方括号中(也为联合表执行此操作),如下所示:
fromDateStr = Format(fromDate, "yyyy-mm-dd")
toDateStr = Format(toDate, "yyyy-mm-dd")
"WHERE [date] >= #" & fromDateStr & "# AND [date]<#" & toDateStr & "#"
另一方面,当toMonth是12月时,这个表达式DateSerial(toyear, ToMonth + 1, 1)
会发生什么?你不应该使用DateAdd功能吗?
toDate = DateAdd("m", 1, DateSerial(toyear, ToMonth, 1))