【问题标题】:"Run-time error 3464" - Data type mismatch in criteria expression“运行时错误 3464” - 标准表达式中的数据类型不匹配
【发布时间】:2013-03-10 16:01:11
【问题描述】:

[下面我在vba中设置参数发送到访问查询。该查询采用开始日期、结束日期、原因代码和子组。所有标准都是字符串,除了子组是一个整数。

Sub FinalDrill()

'Step 1: Declare your variables
Dim DatabasePath As String
DatabasePath = ThisWorkbook.Path & "\SpreadSheetData.accdb"
Dim MyDatabase As DAO.Database
Dim MyQueryDef As DAO.QueryDef
Dim MyRecordset As DAO.Recordset
Dim i As Integer

'Step 2: Identify the database and query
Set MyDatabase = DBEngine.OpenDatabase _
(DatabasePath)
Set MyQueryDef = MyDatabase.QueryDefs("FinalDrill")

'Step 3: Define the Parameters


With MyQueryDef
    .Parameters("[Start Date]") = GetStartDate()
    .Parameters("[End Date]") = GetEndDate()
    .Parameters("[Reason Code]") = "C"
    .Parameters("[Sub Group]") = 2
End With



'Step 4: Open the query
Set MyRecordset = MyQueryDef.OpenRecordset

最后一行抛出“运行时错误 3464” - 条件表达式中的数据类型不匹配

我不明白,因为我已经从不同的 subs 运行了这种确切类型的查询,只有开始日期和结束日期,它很有效....即使我注释掉了

'.Parameters("[Reason Code]") = "C"
'.Parameters("[Sub Group]") = 2

并将访问查询更改为仅开始日期和结束日期除外,我得到相同的错误。

也许我累了,错过了一些明显的东西,任何帮助都会很棒。

更新

以下查询有效,所以我猜问题出在我的访问查询中。

'Step 1: Declare your variables
Dim DatabasePath As String
DatabasePath = ThisWorkbook.Path & "\SpreadSheetData.accdb"
Dim MyDatabase As DAO.Database
Dim MyQueryDef As DAO.QueryDef
Dim MyRecordset As DAO.Recordset
Dim i As Integer

'Step 2: Identify the database and query
Set MyDatabase = DBEngine.OpenDatabase _
(DatabasePath)
Set MyQueryDef = MyDatabase.QueryDefs("levelOnePie")

'Step 3: Define the Parameters


With MyQueryDef
    .Parameters("[Start Date]") = GetStartDate()
    .Parameters("[End Date]") = GetEndDate()
End With



'Step 4: Open the query
Set MyRecordset = MyQueryDef.OpenRecordset

一旦我找到它们,将更新更多结果。

【问题讨论】:

  • 如果您评论日期参数错误仍然存​​在?我猜你的日期有问题。您的函数 GetStartDate() 是否返回日期或字符串值?
  • 错字? .Parameters("Sub Group]") 而不是 .Parameters("[Sub Group]")
  • @Wawrzyniec - GetStartDate() 和 GetEndDate() 在下面的查询中工作,唯一不同的是访问中的查询名称
  • @barrowc - 是的错字,但就像我说的那样,即使注释掉最后两个参数并将访问查询更改为仅开始和结束日期除外,它仍然会引发错误

标签: excel vba


【解决方案1】:

好的,尽管函数 GetStartDate() 和 GetEndDate() 在其他 subs() 中适用于先前的查询,但我考虑了 Wawrzyniec 所说的并将日期参数更改为保存它们的单元格。

With MyQueryDef
    .Parameters("[Start Date]") = Sheets("DASHBOARD").[C4]
    .Parameters("[End Date]") = Sheets("DASHBOARD").[C5]
    .Parameters("[Reason Code]") = "C"
    .Parameters("[Sub Group]") = 2
End With

我使用这些函数来获取日期的原因是仪表板中的一个功能是,如果 StartDate 单元格为空,它将使用数据开始的日期,而没有结束日期意味着它将使用今天的日期。

由于在此之前执行了另外两个向下钻取,因此我只需将日期放在单元格中,以便在最后的向下钻取中抓取它们。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-09
    相关资源
    最近更新 更多