Excel VBA ADODB DataType selection

谁都会走 提交于 2019-12-12 01:27:02

问题


I have a data set in MS Excel 2013 which looks like this:

+------+------------+----------------+
| Name | DateWorked | HoursCompleted |
+------+------------+----------------+
| abc  | 03/01/2016 | 4:53           |
| abc  | 03/02/2016 | 5:22           |
| ghi  | 03/03/2016 | 2:10           |
| jkl  | 03/04/2016 | 5:30           |
| mno  | 03/05/2016 | 4:20           |
| pqr  | 03/06/2016 | 0:20           |
+------+------------+----------------+

where Name is string, DateWorked is mm/dd/yyyy, HoursCompleted is h:mm data type/format.

I am using ADODB to get some results out this data, storing it in a ADODB.Recordset and displaying in another sheet.

My code/query looks something like this:

countCommand.CommandText = "SELECT SUM([HoursCompleted]) FROM [Sheet1$] WHERE [Name] = 'abc' AND [DateWorked] BETWEEN #02/28/2016# AND #03/02/2016#"

Set countResults = countCommand.Execute

Now this works like a charm and gives me a result of 10:15.

However if the first value under DateWorked is NOT a Date datatype then I am not getting the result. For example, if my data looks like this:

+------+------------+----------------+
| Name | DateWorked | HoursCompleted |
+------+------------+----------------+
| abc  | Not worked | 0:00           |
| abc  | 03/02/2016 | 5:22           |
| ghi  | 03/03/2016 | 2:10           |
| jkl  | 03/04/2016 | 5:30           |
| mno  | 03/05/2016 | 4:20           |
| pqr  | 03/06/2016 | 0:20           |
+------+------------+----------------+

How do I set the datatype for my columns?

来源:https://stackoverflow.com/questions/36307625/excel-vba-adodb-datatype-selection

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!