【问题标题】:MSAccess VBA code error: Run-time error '424' object requiredMS Access VBA 代码错误:需要运行时错误“424”对象
【发布时间】: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?

标签: vba ms-access


【解决方案1】:

不太清楚你在做什么,但试试这些修改:

strWhere = "1 = 1 "

If Not IsNull(Me.txtAging1) Then
    strWhere = strWhere & " and DateDiff('d', [CREATED_DT], Now()) > " & Str(Me.txtAging1) & ""
End If

【讨论】:

  • 我为用户创建了一个表单,用于运行基于“Created_dt”中的老化的报告。 Created_dt 是 tblIncident 表中的一个字段。所以基本上我从'created_dt'和今天的日期(DateDiff(“d”,TheDate,Now)计算老化。用户将输入ie10来生成大于10天的报告(例如)。在哪里天数的类型是一个未绑定的框标题 txtAging1,一旦这个工作,我会调整说,即给我从 10 到 20 天的任何东西,等等。但我正在尝试第一部分......
  • 但是报告的数据是从哪里提取的?
  • 直接从表中,tblIncident
  • Dim strReportName As String Dim TheDate As Date Dim strAging As Long Dim strWhere As String strReportName = "rptDGIncDetail" TheDate = [tbldgincident]![CREATED_DT] strAging = DateDiff("d", Now, TheDate) strWhere = "1 = 1 " If Not IsNull(Me.txtAging1) Then strWhere = strWhere & " and strAging > " & Me.txtAging1 & " " End If Debug.Print strWhere DoCmd.OpenReport strReportName, acViewReport, , strWhere End Sub 更改将代码转换为上述格式,现在我收到错误“2465”找不到您的表达式中提到的字段“|1”。
  • 好的。请查看简化答案。
猜你喜欢
  • 1970-01-01
  • 2013-05-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-24
相关资源
最近更新 更多