在代码中包含查询时出现语法错误
Syntax error when including query in code
我想我的查询有语法错误,我找不到错误。我在“1/1/1990”收到错误。有人知道我在这里做错了吗?
Set rsCanCounter = db.OpenRecordset("Select * from tblActionLog Where groupNum = '" & txtGroupNr.Value & "' And DateClosed < '" & 1/1/1900, dbOpenDynaset)
使用字符串变量来保存您的 SELECT
语句。解决引用问题后,您可以在 VBA 中创建有效字符串,然后将该变量与 OpenRecordset
.
一起使用
例如,如果您的 groupNum 和 DateClosed 字段的数据类型都是文本 ...
Dim strSelect As String
strSelect = "Select * from tblActionLog Where groupNum ='" & _
Me.txtGroupNr.Value & "' And DateClosed < '1/1/1900'"
Debug.Print strSelect '<-- Ctrl+g to go to Immediate window and see the statement text
Set rsCanCounter = db.OpenRecordset(strSelect, dbOpenDynaset)
如果 DateClosed 是 Date/Time 数据类型,请使用 #
而不是引号来分隔日期值:
strSelect = "Select * from tblActionLog Where groupNum ='" & _
Me.txtGroupNr.Value & "' And DateClosed < #1900-1-1#"
我想我的查询有语法错误,我找不到错误。我在“1/1/1990”收到错误。有人知道我在这里做错了吗?
Set rsCanCounter = db.OpenRecordset("Select * from tblActionLog Where groupNum = '" & txtGroupNr.Value & "' And DateClosed < '" & 1/1/1900, dbOpenDynaset)
使用字符串变量来保存您的 SELECT
语句。解决引用问题后,您可以在 VBA 中创建有效字符串,然后将该变量与 OpenRecordset
.
例如,如果您的 groupNum 和 DateClosed 字段的数据类型都是文本 ...
Dim strSelect As String
strSelect = "Select * from tblActionLog Where groupNum ='" & _
Me.txtGroupNr.Value & "' And DateClosed < '1/1/1900'"
Debug.Print strSelect '<-- Ctrl+g to go to Immediate window and see the statement text
Set rsCanCounter = db.OpenRecordset(strSelect, dbOpenDynaset)
如果 DateClosed 是 Date/Time 数据类型,请使用 #
而不是引号来分隔日期值:
strSelect = "Select * from tblActionLog Where groupNum ='" & _
Me.txtGroupNr.Value & "' And DateClosed < #1900-1-1#"