名称的无效括号

时间:2020-06-28 14:46:09

标签: sql ms-access

我遇到错误Invalid bracketing of name [qryTemp.IEnduseID]

strSQL = "SELECT tbl_Item_Enduse.IEnduseID, tbl_Item_Enduse.IEnduseDesc, IIf([qryTemp.IEnduseID]>0,-1,0) AS Sel " & _
    "FROM tbl_Item_Enduse LEFT JOIN (SELECT tbl_ItemsEU.EUid FROM tbl_ItemsEU " & _
    "WHERE tbl_ItemsEU.ItemID=" & Nz(Me.ItemID, 0) & ")  AS qryTemp " & _
    "ON tbl_Item_Enduse.IEnduseID = qryTemp.EUid " & _
    "ORDER BY tbl_Item_Enduse.IEnduseDesc;"

如果我卸下支架-我得到的是'参数太少...

请帮助。

2 个答案:

答案 0 :(得分:1)

在您的JOIN表达式中,考虑使用EuId而不是IEnduseID,因为前者在您别名为qdyTemp的子查询中被引用。否则,引用后一列将导致未知参数。另外,请考虑使用较短的表别名以提高可读性。

strSQL = "SELECT t.IEnduseID, t.IEnduseDesc, " & _
         "       IIf([q.EUid]>0,-1,0) AS Sel " & _ 
         "FROM tbl_Item_Enduse AS t" & _
         "LEFT JOIN " & _
         " (SELECT sub.EUid " & _
         "  FROM tbl_ItemsEU sub " & _ 
         "  WHERE sub.ItemID=" & Nz(Me.ItemID, 0) & ") AS q " & _ 
         "ON t.IEnduseID = q.EUid " & _ 
         "ORDER BY t.IEnduseDesc;"

顺便说一句,不确定如何从SQL字符串使用上述动态查询,但是考虑使用保存的查询,该查询将使用JOIN更为有效,因为引擎可以节省最佳的执行计划。此外,保存的查询可以直接使用打开的表单控件。这样做还可以避免混乱的VBA串联。

SELECT t.IEnduseID, t.IEnduseDesc,
       IIF([q.EUid]>0,-1,0) AS Sel
FROM tbl_Item_Enduse AS t
LEFT JOIN
     (SELECT sub.EUid
      FROM tbl_ItemsEU sub
      WHERE sub.ItemID = Nz(Forms!myOpenForm!ItemID, 0) AS q
ON t.IEnduseID = q.EUid
ORDER BY t.IEnduseDesc;

在VBA中,具有GUI参数的存储查询可以以多种方式使用,涉及GUI操作(即表单,报表)。

DoCmd.OpenQuery "mySavedQuery"

Me.Form.RecordSource = "mySavedQuery"
Me.Form.Requery

Me.myComboOrListBox.RowSource = "" 
Me.myComboOrListBox.RowSource = "mySavedQuery"
Me.myComboOrListBox.Requery

答案 1 :(得分:0)

您正在混淆字段名称。

尝试一下(只是猜测):

strSQL = "SELECT tbl_Item_Enduse.IEnduseID, tbl_Item_Enduse.IEnduseDesc, IIf(qryTemp.IEnduseID>0,-1,0) AS Sel " & _
    "FROM tbl_Item_Enduse LEFT JOIN (SELECT tbl_ItemsEU.EUid As IEnduseID FROM tbl_ItemsEU " & _
    "WHERE tbl_ItemsEU.ItemID=" & Nz(Me.ItemID, 0) & ")  AS qryTemp " & _
    "ON tbl_Item_Enduse.IEnduseID = qryTemp.IEnduseID " & _
    "ORDER BY tbl_Item_Enduse.IEnduseDesc;"