日期字段的访问类型与 SQL Server Express 后端不匹配
我有一个简单的访问表单,其中有一个未绑定的文本框,用于搜索具有匹配日期的记录,结果显示在子表单中。后端数据库存储在 SQL Express 2008 实例中,可通过 ODBC 连接 (SQL Server Native Client 10.0) 进行访问。客户端是Access 2007,运行在64位Windows 7 Professional上。
当用户单击窗体上的搜索按钮时,将执行以下 VBA 代码来更新子窗体。
Private Sub UpdateSfmQuery()
Dim query As String
If IsDate(txtDate.Value) And IsNumeric(cboStaff.Value) Then
query = "SELECT * FROM TimesheetEntries " & _
"WHERE StaffId = " & cboStaff.Value & " " & _
"AND [Date] = '" & _
Format(txtDate.Value, "YYYY/MM/DD HH:MM:SS") & "' "
Else
query = "SELECT * FROM TimesheetEntries WHERE 0 = 1;"
End If
frmTimesheetUpdateSfm.Form.RecordSource = query '<--- Fails here
End Sub
SQL Server 后端 [Date] 字段中的数据类型是 DateTime,上面的代码在上面突出显示的行上失败,并出现错误:条件表达式中的数据类型不匹配
。
我尝试将 txtDate 的格式属性从短日期更改为一般日期,并且还修改了上面格式化 txtDate.Value 的行中的日期格式。
上述代码摘录中的变量 query
中包含的 SQL 查询(值如下所示)在复制并粘贴到 SQL Express Management Studio 中时可以完美执行。
SELECT * FROM TimesheetEntries WHERE StaffId = 14 AND [Date] = '2011/11/22 00:00:00'
我做错了什么?
I have a simple access form with an unbound textbox that is used to search for records with a matching date, with the results displayed in a subform. The backend database is stored in an SQL Express 2008 instance, accessed via an ODBC connection (SQL Server Native Client 10.0). The client is Access 2007, running on 64 bit Windows 7 professional.
When the user clicks on the search button on the form, the following VBA code is executed to update the subform.
Private Sub UpdateSfmQuery()
Dim query As String
If IsDate(txtDate.Value) And IsNumeric(cboStaff.Value) Then
query = "SELECT * FROM TimesheetEntries " & _
"WHERE StaffId = " & cboStaff.Value & " " & _
"AND [Date] = '" & _
Format(txtDate.Value, "YYYY/MM/DD HH:MM:SS") & "' "
Else
query = "SELECT * FROM TimesheetEntries WHERE 0 = 1;"
End If
frmTimesheetUpdateSfm.Form.RecordSource = query '<--- Fails here
End Sub
The datatype in the SQL Server back end [Date] field is DateTime, and the code above fails on the highlighted line above with the error, Data type mismatch in criteria expression
.
I've tried changing the format property of txtDate from short date to general date, and I've also modified the date format in the line above that formats txtDate.Value.
The SQL query contained in the variable query
in the above code excerpt (value shown below), executes perfectly when copied and pasted into SQL Express Management Studio.
SELECT * FROM TimesheetEntries WHERE StaffId = 14 AND [Date] = '2011/11/22 00:00:00'
What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是本机 Access 查询中的日期值需要用井号/井号而不是引号括起来。基本上,与任何 SQL 数据库一样,Access 有其自己的 SQL 方言,并且与 SQL Server 略有不同。
请记住,您始终可以获取查询的模型并使用 Access 的查询编辑器来尝试使查询正常工作。
作为旁注,我建议您使用 ms-access 标记来标记此类问题,除非您的问题特定于某个版本的 Access。
The problem is that date values inside native Access queries need to be surrounded by pound/hash signs instead of quotes. Basically, like any SQL database, Access has it's own dialect of SQL and it is slightly different from SQL Server.
Remember, you can always take a mockup of your query and use Access's query editor to try to make the query work.
As a side note, I recommend you tag questions like this with the ms-access tag unless your problem is specific to a certain version of Access.