Ms Ms无法识别为有效字段名称

时间:2015-05-05 11:19:43

标签: sql vba ms-access access-vba

TRANSFORM IIF(IsNull(First(wrhCode)),'' ,First(wrhCode) & ', '  ) AS FirstOfwrhCode 
   SELECT whiitemID AS ItemID, 
          whiItemName AS Name, 
          Sum(Qty) AS Quantity
     FROM (SELECT wrhID,  
                  wrhCode, 
                  whiitemID,
                  whiItemName,
                  Sum(whiQty) AS QTY 
            FROM tblWarehouseItem AS WI 
                 INNER JOIN tblWarehouse AS W ON WI.whiwrhID = W.wrhID
           WHERE whiActive = True                           
             AND whiwrhID IN (Forms.frmStockControl.Form.txtwrhIDs) 
        GROUP BY wrhID, wrhCode, whiItemName, whiitemID)  AS [%$##@_Alias]
 GROUP BY whiitemID, whiItemName
    PIVOT 'wrhID' & wrhID;

我对我的表单有这个查询但它不起作用: 但如果我编辑:

 AND whiwrhID IN (Forms.frmStockControl.Form.txtwrhIDs)

为:

 AND whiwrhID IN (26,27,29)

正在处理任何想法如何修复错误

3 个答案:

答案 0 :(得分:1)

你不能。

    whiwrhID IN (26,27,29)

必须在SQL中进行硬编码。

您唯一的选择是在调用查询之前调整SQL。

答案 1 :(得分:1)

为什么不use VBA to build a dynamic query

您可以通过事件或宏启动子MakeAQuery,将IN语句修改为您需要的任何内容:

Sub MakeAQuery()
    Dim db As DAO.Database
    Dim strSQL As String
    Dim strElements As String

    Set db = CurrentDb()

'Your current SQL with a parameter in the IN clause
    strSQL = "TRANSFORM IIF(IsNull(First(wrhCode)),'' ,First(wrhCode) & ', '  ) _
    AS FirstOfwrhCode _
    SELECT whiitemID AS ItemID, _
      whiItemName AS Name, _
      Sum(Qty) AS Quantity _
    FROM (SELECT wrhID, _
              wrhCode, _
              whiitemID, _
              whiItemName, _
              Sum(whiQty) AS QTY _
        FROM tblWarehouseItem AS WI _
             INNER JOIN tblWarehouse AS W ON WI.whiwrhID = W.wrhID _
       WHERE whiActive = True _                  
         AND whiwrhID In ({0})_
       GROUP BY wrhID, wrhCode, whiItemName, whiitemID) _ 
    AS [%$##@_Alias]
    GROUP BY whiitemID, whiItemName
    PIVOT 'wrhID' & wrhID;"

    ' Retrieve your IDs for the IN clause
    strElements = Forms.frmStockControl.Form.txtwrhIDs

    ' Assign SQL to query
    db.QueryDefs!YourQuery.SQL = Replace(strSQL, "{0}", strElements)

End Sub

将此VBA子分配给表单上的按钮或其他控件...

  1. 转到Layout View获取表单。
  2. 点击某个控件,转到属性表上的Event标签。
  3. 点击On Click属性旁边的 ... 按钮。 (您可以对任何事件属性执行此操作;单击控件时OnClick可以正常工作。)
  4. 选择Code BuilderOk
  5. Call MakeTheQuery放入它为您构建的子目录中。

答案 2 :(得分:-1)

请改为尝试:

Forms.frmStockControl.Form.txtwrhIDs.Text