使用嵌套在其中的多个选择查询更新查询

时间:2018-05-11 14:40:47

标签: sql vba excel-vba ms-access ado.net

  • 我有一个Access IC,我用Excel连接到VBA *

我有一个更新查询(在vba中),用户可以转到已下订单,并更新订单必须继续的路线。它有多个标准。

每个条件都有一个外键,可以从Int Id字段中获取值,但用户会选择descriptionString的条件。

这意味着在我可以执行更新查询之前,首先必须获取字符串条件的所有ID,因为外键字段采用ID(Int)而不是描述(String)。但是现在我试图在一次传递中完成所有操作,并使用嵌套在其中的select查询进行更新查询。

以下查询不起作用,并提供错误Operation must use an update-able query

updateOrdersRoute = "UPDATE tbl_orders " & _
    "SET tbl_orders.route = (SELECT tbl_deliveryRoutes.ID FROM tbl_deliveryRoutes WHERE tbl_deliveryRoutes.routes = '" & routeNameNew & "') " & _
    "WHERE ([tbl_orders.route] = (SELECT [tbl_deliveryRoutes.ID] FROM [tbl_deliveryRoutes] WHERE [tbl_deliveryRoutes.routes] = '" & routeNameOld & "') " & _
    "AND [tbl_orders.weekNo] = " & weekNo & " " & _
    "AND [tbl_orders.loadingDay] = (SELECT [tbl_weekday.ID] FROM [tbl_weekday] WHERE [tbl_weekday.weekDay] = '" & weekday & "') " & _
    "AND [tbl_orders.CustomerName] = '" & shopName & "')"

但以下查询确实有效:(仅更改了第二行)

updateOrdersRoute = "UPDATE tbl_orders " & _
    "SET tbl_orders.route = " & routeNumberNew & " " & _
    "WHERE ([tbl_orders.route] = (SELECT [tbl_deliveryRoutes.ID] FROM [tbl_deliveryRoutes] WHERE [tbl_deliveryRoutes.routes] = '" & routeNameOld & "') " & _
    "AND [tbl_orders.weekNo] = " & weekNo & " " & _
    "AND [tbl_orders.loadingDay] = (SELECT [tbl_weekday.ID] FROM [tbl_weekday] WHERE [tbl_weekday.weekDay] = '" & weekday & "') " & _
    "AND [tbl_orders.CustomerName] = '" & shopName & "')"

但是我必须首先使用额外的查询获取路径ID号,所以问题似乎是在查询的开头,但我无法弄清楚在哪里 - 或者是否有任何方式它将会工作。我确实尝试了this和其他解决方案,但它们似乎没有接近我想做的事情。

0 个答案:

没有答案