我有一个table1( tblPurchaseOrders )和一个table2 (的 tblPurchaseOrderDetails )。
表1包含 PurchaseOrder_ID ,Supplier_FK,Employee_FK, 状态,其中包含2条记录(新建和完成)。 table2包含PurchaseOrder_FK, Product_FK ,数量和价格。
tblPurchaseOrder.PurchaseOrder_ID = tblPurchaseOrderDetails.PurchaseOrder_FK
我想从table1,Product_FK获取PurchaseOrder_ID和Status 表2中的数量和数量,并将所有记录放入表3中 tblStockMovements。不只是一个记录,而是所有记录 上面的陈述。所以当我打开主表格时,让我们说吧 PurchaseOrder_ID为“10”,状态为“完成”且在子表单中 有2条记录:
表单中会有一个按钮,可以在点击时运行代码 并将上述记录移至tblStockMovements。点击后 按钮tblStockMovements将如图所示
我尝试的代码是下一个:
Private Sub cmdOrder_Click()
Dim strSQL As String
strSQL = ""
strSQL = strSQL & "INSERT INTO tblStockMovement ( "
strSQL = strSQL & " Product_FK "
strSQL = strSQL & ", Status "
strSQL = strSQL & ", Quantity "
strSQL = strSQL & ", PurchaseOrder_FK "
strSQL = strSQL & ") SELECT "
strSQL = strSQL & " tblPurchaseOrderDetails.Product_FK "
strSQL = strSQL & ", tblPurchaseOrder.Status "
strSQL = strSQL & ", tblPurchaseOrderDetails.Quantity "
strSQL = strSQL & ", tblPurchaseOrder.PurchaseOrder_ID "
strSQL = strSQL & "FROM tblPurchaseOrder "
strSQL = strSQL & "LEFT JOIN tblPurchaseOrderDetails "
strSQL = strSQL & "ON tblPurchaseOrder.PurchaseOrder_ID= tblPurchaseOrderDetails.PurchaseOrder_FK "
strSQL = strSQL & "WHERE PurchaseOrder_ID=" & Me!PurchaseOrder_ID
CurrentDB.Execute strSQL, dbFailOnError + dbSeeChanges
Me.Requery
End Sub