名称的括号无效

hjqgdpho  于 2021-08-09  发布在  Java
关注(0)|答案(2)|浏览(358)

我出错了 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;"

如果我去掉括号-我得到的参数太少了。。。
请帮忙。
jq6vz3qz

jq6vz3qz1#

从你的 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字符串动态查询的,但是考虑一个保存的查询,它将更有效地使用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
l3zydbqr

l3zydbqr2#

你把字段名弄混了。
尝试(只是猜测):

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;"

相关问题