由于错误的SQLdate比较设置,SQL结果不准确

我在Excel中使用ADO在excel-VBA中运行SQL。 结果显示在Excel工作表上。

在工作表2014,2015,2016,2017中有一个名为Date的字段

数据示例:2/1 / 2014,7 / 1 / 2014,23 / 10/2014

这个字段的数据types是在日/月/年。和date合并= DATE(cell1,cell2,cell3) – 年,月,日。表中的date应该是纯粹的date,因为我把3个单元格,date)分成1个字段(date)

'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之间的date范围(包括两者)

我build立了SQL引用: 如何在VBA中将3个VBAvariables合并到date数据types中

  ' This is the new SQL string WHERE date >= #" & fromdate & "# AND date<#" & toDate & "#" 

问题:

现在的SQL结果是完全错误的(仍然有输出)。我猜SQL的where子句有什么问题。 由于SQL最初是正确的,所以SQL在where-dateselect子句中变得错误。

或者我试图使用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# 

testingSQL。

它像原来的SQL一样失败。 它可以输出结果,但结果是错误的。

我想这是关系到联盟所有? 当我join所有4表logging成为一个大表。

更新2

我相信这和UNION ALL没有关系。 由于excel列date合并为3个单元格 – 年,月,日,我再次复制并粘贴该值。SQL结果已更改,但结果仍不正确。

然后我试图用硬代码来testing。 例如,我尝试在2016年3月selectlogging。使用此where子句:

 and month=3 and year=2016 

没关系 。

然后我试图用下面的这个声明。这不是OKAY。

 date >= #2016-03-01# AND date<#2016-04-01# 

那么我相信它应该是Excel或VBA中的date数据types问题。

然后,我试着做下面的这些testing:

对于Excel,原始数据types是date。我更改为string,数字,一般…不好

date样本:6/1/2016string样本:42375数字样本:42375.00

对于VBA,我试图交换月份和date。 – >#2016-01-03#还不行

您需要为fromDate和toDatedate创buildstringvariables并将它们传递给WHERE子句。 我build议你使用ISOdate格式。 除此之外,为了避免列名date的问题,让我们尝试在方括号中包含date列(对于联合表也这样做),如下所示:

  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)expression式DateSerial(toyear, ToMonth + 1, 1)发生什么? 不应该使用DateAdd函数吗?

 toDate = DateAdd("m", 1, DateSerial(toyear, ToMonth, 1))