问题定义 Querydef 参数,运行 时间错误 13 类型不匹配

Issue defining Querydef parameters, run time error 13 type mismatch

我想打开一个基于查询的记录集。该查询有 10 个与表单匹配的参数。参数有点复杂,因为它们正在搜索部分字符串匹配。

我 运行 遇到了多个问题,看来访问权限不允许我打开基于从表单中提取参数的查询的记录集。相反,我现在试图打开一个 querydef 并将其传递给记录集。当我现在尝试 运行 函数时,出现 运行 时间错误 13 类型不匹配。有没有更好的方法来做我想做的事情?有没有人对修复此错误有任何见解?错误突出显示了第一个参数(出口国),但如果这是一个问题,我相信它们都是。谢谢!

*修改为尝试通过记录集传递条件参数

Function StatementUpdate()

Dim dbs As DAO.Database
Dim rstStatements As DAO.Recordset
Dim rstCBG As DAO.Recordset
Dim concStatement As String
Dim strSQL As Variant


Set dbs = CurrentDb()
strSQL = "SELECT [Statement] FROM [St_Gen_Qry] WHERE" _
     & " (([Statement Category]='General Information')" _
     & " And ([Export Country] Like '*" & Forms!New_Shipment_Home_frm.[Export Country] & "*'" _
     & " Or [Export Country]='All')" _
     & " And ([Export State] Like '*" & Forms!New_Shipment_Home_frm.[Export State] & "*'" _
     & " Or [Export State]='All')" _
     & " And ([Import Country] Like '*" & Forms!New_Shipment_Home_frm.[Import Country] & "*'" _
     & " Or [Import Country]='All')" _
     & " And ([Import State] Like '*" & Forms!New_Shipment_Home_frm.[Import State] & "*'" _
     & " Or [Import State]='All')" _
     & " And ([Shipment Type] Like '*" & Forms!New_Shipment_Home_frm.[Shipment Type] & "*'" _
     & " Or [Shipment Type]='All')" _
     & " And ([Material Category] Like '*" & Forms!New_Shipment_Home_frm.[Material Category] & "*'" _
     & " Or [Material Category]='All')" _
     & " And ([Sub Category] Like '*" & Forms!New_Shipment_Home_frm.[Sub Category] & "*'" _
     & " Or [Sub Category]='All')" _
     & " And ([Transgenic/ Conventional] Like '*" & Forms!New_Shipment_Home_frm.RegCode & "*'" _
     & " Or [Transgenic/ Conventional]='All')" _
     & " And ([Intended Use] Like '*" & Forms!New_Shipment_Home_frm.[Intended Use] & "*'" _
     & " Or [Intended Use]='All')" _
     & " And ([Permit] Like '*" & Forms!New_Shipment_Home_frm.[Permit Required] & "*'" _
     & " Or [Permit]='All')" _
     & " And ([Active]='Yes'));"

Set rstStatements = dbs.OpenRecordset(strSQL, dbOpenDynaset)
Set rstCBG = dbs.OpenRecordset("SELECT Cross_Border_Grid_Table.ID,    Cross_Border_Grid_Table.St_General FROM Cross_Border_Grid_Table WHERE   (Cross_Border_Grid_Table.ID)= " & [Forms]![New_Shipment_Home_frm]![Text105])


rstCBG.MoveFirst

'loop through each record in the CBG that matches select query
Do Until rstCBG.EOF
    concStatement = ""
    rstStatements.MoveFirst
    Do Until rstStatements.EOF
        concStatement = concStatement & vbCrLf & rstStatements(0) & vbCrLf
        rstStatements.MoveNext
    Loop
        rstCBG.Edit
        rstCBG![St_General] = concStatement
        rstCBG.Update
        rstCBG.MoveNext
        Loop

rstCBG.Close
rstStatements.Close

Set rstStatements = Nothing
Set rstCBG = Nothing
Set dbs = Nothing

Debug.Print "Done"

End Function

您的主要问题是您试图在 querydef 参数中传递条件表达式。因此,每个参数语句中的第一个 OR 必须是字符串的一部分,在外部作为 VBA 运算符。其次,您需要 LIKE * 子句来按字符串模式进行搜索。然后,您可能需要将表单值用单引号引起来。也许这是可能的,但 querydef 参数通常采用单个值而不是您布局时的条件表达式。

话虽如此,您绝对可以在 VBA 记录集中(类似于其他记录集)传递带有动态条件表达式的表单值:

strSQL = "SELECT [Statement] FROM [St_Gen_Qry] WHERE" _
         & " (([Statement Category]='General Information')" _
         & " And ([Export Country] Like '*" & Forms!New_Shipment_Home_frm.[Export Country] & "*'" _
         & " Or [Export Country]='All')" _
         & " And ([Export State] Like '*" & Forms!New_Shipment_Home_frm.[Export State] & "*'" _
         & " Or [Export State]='All')" _
         & " And ([Import Country] Like '*" & Forms!New_Shipment_Home_frm.[Import Country] & "*'" _
         & " Or [Import Country]='All')" _
         & " And ([Import State] Like '*" & Forms!New_Shipment_Home_frm.[Import State] & "*'" _
         & " Or [Import State]='All')" _
         & " And ([Shipment Type] Like '*" & Forms!New_Shipment_Home_frm.[Shipment Type] & "'*" _
         & " Or [Shipment Type]='All')" _
         & " And ([Material Category] Like '*" & Forms!New_Shipment_Home_frm.[Material Category] & "*'" _
         & " Or [Material Category]='All')" _
         & " And ([Sub Category] Like '*" & Forms!New_Shipment_Home_frm.[Sub Category] & "*'" _
         & " Or [Sub Category]='All')" _
         & " And ([Transgenic/ Conventional] Like '*" & Forms!New_Shipment_Home_frm.RegCode & "*'" _
         & " Or [Transgenic/ Conventional] ='All')" _
         & " And ([Intended Use] Like '*" & Forms!New_Shipment_Home_frm.[Intended Use] & "*'" _
         & " Or [Intended Use]='All')" _
         & " And ([Permit Like '*" & Forms!New_Shipment_Home_frm.[Permit Required] & "*'" _
         & " Or Permit='All') And (Active='Yes'))"

Set rst = dbs.OpenRecordset(strSQL, dbOpenDynaset)