获取Date Time Picker的值并使用BETWEEN过滤Ms Access数据库中的日期

时间:2015-06-16 05:09:28

标签: vb.net ms-access oledb

我有一个From和To DateTimePicker。我将它们转换为String并使用它来过滤特定范围的MsAccess database,但过滤存在问题。例如,我想从2015年1月1日到2015年1月31日进行过滤。结果包括前几年的Januaries。我的陈述似乎有什么问题?

这是我的代码:

Dim connectionstring As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Application.StartupPath & "\STSlog.mdb;Jet OLEDB:Database Password=password;"
Dim conn As New OleDbConnection(connectionstring)
Dim command As New OleDbCommand
Dim commstring As String
Dim fromdatestring As String = FromDPicker.Value.ToString("MM/dd/yyyy", CultureInfo.InvariantCulture)
Dim todatestring As String = ToDPicker.Value.ToString("MM/dd/yyyy", CultureInfo.InvariantCulture)

commstring = "SELECT Mark, ProjectCode, Project, Activity, today, Regular, OT FROM data WHERE today BETWEEN '" & fromdatestring & "' AND '" & todatestring & "' AND  empno = '" & TimeSheetAdmin.EmpNumCmbBox.SelectedItem.ToString & "' ORDER BY VAL(Mark) DESC"
command = New OleDbCommand(commstring, conn)

1 个答案:

答案 0 :(得分:1)

更改

..."WHERE today BETWEEN '" & fromdatestring & "' AND '" & todatestring & "'"...

..."WHERE today >= #" & fromdatestring & "# AND today <= #" & todatestring & "#"...

请注意,MS-Access将{-Delimiters用于Date - 值(afaik),这也可能是您查询中的问题。