2016-07-25 128 views
0

我在excel-VBA中使用ADO在Excel中运行SQL。结果显示在Excel工作表上。由于SQL日期比较设置错误导致SQL结果不准确

有一个名为日期在工作表2014,2015,2016,2017

数据字段示例:2/1/2014,7/1/2014,23 /二千零十四分之十

这个字段数据类型是在日/月/年。日期由= 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用户界面: enter image description here 现在我想设置没有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选择子句会导致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个表格记录加入成为一张大表格时。

更新2

我认为这与UNION ALL无关。 由于excel列日期合并为3个单元格 - 年,月,日,我再次复制并粘贴该值。SQL结果已更改。但结果仍不正确。

然后我试着用硬代码来测试。 例如,我尽量选择在2016年三月的 记录使用此where子句:

and month=3 and year=2016 

它是好的。

然后我试着用下面的where语句。这不是OKAY。

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

然后我相信它应该是在Excel或VBA中的日期数据类型问题。

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

对于Excel,原始数据类型是日期。我改变为字符串,数字,一般... ...不行

日期样品:6/1/2016 字符串样本:42375 数字样本:42375.00

对于VBA,我试图交换月份和日期。 - >#2016-01-03#仍然不OKAY

+0

什么是您所在的地区日期设置dd/mm/yyyy或mm/dd/yyyy? – Jules

+0

@Jules excel中的日期是dd/mm/yyyy。我更改为dd/mm/yyyy为VBA日期变量。但是,它仍然是错误的。 'fromDate =格式(fromDate,“dd/mm/yyyy”) toDate =格式(toDate,“dd/mm/yyyy”)' – Monchhichi

+0

为什么使用'date'作为列名? – KyloRen

回答

1

您需要为fromDate和toDate日期创建字符串变量并将它们传递给WHERE子句。我建议你使用ISO日期格式。此外,为了避免与列名日期的问题,让我们尝试封闭在方括号中的日期列(这样做也为被联合表),就像这样:

fromDateStr = Format(fromDate, "yyyy-mm-dd") 
    toDateStr = Format(toDate, "yyyy-mm-dd") 

"WHERE [date] >= #" & fromDateStr & "# AND [date]<#" & toDateStr & "#" 

在一个侧面说明,什么有发生这个表达式DateSerial(toyear, ToMonth + 1, 1)当toMonth是12月?你不应该使用DateAdd函数吗?

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

试过的原因。仍然错误。 – Monchhichi

+0

附加信息,获取月份的最后一天DateAdd(“d”, - 1,DateAdd(“m”,1,“1 Jan 2016”)) – Jules

+0

或者使用格式(fromDate,“dd mmm yyyy”) – Jules