【发布时间】:2020-12-14 18:03:43
【问题描述】:
我在以下 Access VBA 代码中遇到运行时错误 '424:
Private Sub btnDetailRpt_Click()
Dim strReportName As String
Dim strWhere As String
Dim strAging As String
Dim TheDate As Date
strReportName = "rptIncidentDetail"
strWhere = "1 = 1 "
strAging = DateDiff("d", Now, TheDate)
TheDate = Format([tblIncident]![CREATED_DT], "short date") ***This where error is***
If Not IsNull(Me.txtAging1) Then
strWhere = strWhere & " and strAging > " & Me.txtAging1 & " "
End If
Debug.Print strWhere
DoCmd.OpenReport strReportName, acViewReport, , strWhere
End Sub
我为我们的用户创建了一个表单,用于按年龄进行过滤,但它似乎无法识别该字段。非常感谢您的帮助!
【问题讨论】:
-
不能像这样直接从表中提取数据。表单上是否有绑定到 CREATED_DT 的文本框?或者使用 DLookup() 域聚合函数。需要从哪条记录中提取值?
-
另外,WHERE 子句的构造不正确。在引号中嵌入变量是没有意义的。而应该是过滤器应应用于
" and fieldname > " & strAging的字段名称。此外,在填充 TheDate 之前无法计算 strAging。 -
为什么要为数值声明字符串变量 - strAging?