通过Access中的VBA运行查询时,我始终收到运行时错误“2465”。

错误:Microsoft Access找不到表达式中引用的字段“| 1”

我似乎找不到发生此问题的位置。以下是我当前用于重新查询表单的VBA代码。

Dim Test As String
Test = "*" & Combo161.Value

Dim strSQL As String
Dim strWhere As String
strWhere = (Chr(34) + Test + (Chr(34)))

'MsgBox (strWhere)

strSQL = "SELECT * FROM Test_Query WHERE TestID " & strWhere

'MsgBox (strSQL)
[Form_Test (subform)].RecordSource = strSQL
[Form_Test (subform)].Requery
TestID具有文本的字段格式,而不是数字。这有关系吗?

最佳答案

尝试:

Dim Test As String
Test = "*" & Combo161.Value

Dim strSQL As String
Dim strWhere As String
strWhere = (Chr(34) & Test & (Chr(34)))

'MsgBox (strWhere)

strSQL = "SELECT * FROM Test_Query WHERE TestID Like " & strWhere

'To test
'Debug.print strSQL

如果这是子窗体,则:
Me.[Form_Test (subform)].Form.RecordSource = strSQL
''Not needed when changing record source
''Me.[Form_Test (subform)].Form.Requery

您没有等号/Like,VBA中的连接器是&,而不是+,使用+可能会导致null问题,但是在这种情况下,我认为问题是缺少的Like,即
TestID Like "*something"

您可以使用组合框和链接字段来控制子表单的内容:

08-05 14:38