简单的问题。例如,我有一个客户,有10个订单,每个订单包括6-10个项目。
我想要创建一个vba查询,它将取消特定客户的所有项目。
我的问题是:
x = CustomerNum
Query = "Select OrderNum from CustomerOrderT Where CustomerNum = " & x
Set result = CurrentDb.OpenRecordset(Query)
y = result!OrderNum
'(there is a lot of orders on y)
Query = "Select Product From OrderProducts Where OrderNum = " & y
Set result = CurrentDb.OpenRecordset(Query)问题是,我只看到第一个订单的产品,而不能看到我在第一个查询中选择的所有订单的产品。需要一些帮助来处理这种情况。非常感谢
发布于 2016-07-21 03:49:00
您只需对所有订单执行一个查询:
x = CustomerNum
Query = " SELECT CustomerOrderT.CustomerNum, " & _
CustomerOrderT.OrderNum, " & _
" OrderProducts.Product " & _
" FROM CustomerOrderT INNER JOIN OrderProducts " & _
ON CustomerOrderT.OrderNum = OrderProducts.OrderNum " & _
" WHERE (((CustomerOrderT.CustomerNum)=" & x & ")) " & _
"ORDER BY CustomerOrderT.OrderNum, " & _
" OrderProducts.Product;"然后遍历所有记录,记录OrderNum中的每一个变化。
但是,如果您不控制如何分配变量CustomerNum,则请注意像这样构建SQL,因为您对SQL注入攻击是开放的。
https://stackoverflow.com/questions/38490524
复制相似问题