2014-08-28 75 views
2

我無法使我的報表正常工作。我有一個purchaseOrder表,其中包含1個地址ID,代表送貨地址。與purchaseOrder關聯的供應商也有一個地址ID,代表賬單地址。Microsoft Access如何從1個表中顯示其他2個表的信息

以下是您與我的解釋混淆的情況。 Relationship

我想知道如何告訴我的報告,地址記錄屬於供應商或purchaseOrder。任何建議或協助將不勝感激。

Report PO

查詢的原始SQL,忽略了長期選擇。

SELECT tbl_purchaseOrder.ID, tbl_purchaseOrder.purchaseOrderDate, tbl_vendor.salesContact, tbl_vendor.phone, tbl_vendor.extension, tbl_vendor.fax, tbl_vendor.vendorNo, tbl_vendor.vendorName, tbl_address.address, tbl_address.city, tbl_address.provinceOrState, tbl_address.postalCode, tbl_address.country, tbl_purchaseOrder.shipVIA, tbl_purchaseOrder.FOB, tbl_purchaseOrder.term, tbl_PODetails.quantityOrder, tbl_rawItemList.ItemSerialCode, tbl_PODetails.dueDate, tbl_PODetails.unitCost, tbl_PODetails.unit, tbl_rawItemList.description, tbl_PODetails.specialInstructions, tbl_purchaseOrder.requistionedBy, [unitCost]*[quantityOrder] AS Total 
FROM tbl_rawItemList INNER JOIN (((tbl_address INNER JOIN tbl_purchaseOrder ON tbl_address.ID = tbl_purchaseOrder.addressID) INNER JOIN tbl_vendor ON (tbl_vendor.ID = tbl_purchaseOrder.vendorID) AND (tbl_address.ID = tbl_vendor.addressID)) INNER JOIN tbl_PODetails ON tbl_purchaseOrder.ID = tbl_PODetails.purchaseOrderID) ON tbl_rawItemList.ID = tbl_PODetails.rawItemListID 
WHERE (((tbl_purchaseOrder.ID)=[Enter a PO number:])); 
+2

您將可以基於給予'tbl_adress'的別名。你應該有兩個別名,因爲你鏈接了兩次。你能告訴我們查詢的樣子嗎? – Linger 2014-08-28 19:57:33

+1

只是一個快速的想法:爲什麼你有一個獨立的地址表?每個供應商都有自己的地址,每個客戶/採購員都有自己的地址。您不會通過擁有獨立地址表來保存數據庫空間,因爲您要爲每個供應商輸入地址。 您的情況:您需要爲供應商添加兩個地址表,一個用於採購訂單並獲取來自purchaseorder_address表的地址 – 2014-08-28 20:07:42

+0

@krishKM當時,它似乎是合乎邏輯的,因爲我需要5個不同表中的地址,所以我認爲爲它創建1個表併爲其分配AddressType會更容易。 – 2014-08-28 23:43:50

回答

1

你真正需要做的是鏈接到tbl_Address兩次,如下圖所示:

enter image description here

這裏是你的FROM子句中應該是什麼樣子:

FROM ((((tbl_PODetails 
INNER JOIN tbl_rawItemList ON tbl_PODetails.rawItemListID = tbl_rawItemList.ID) 
INNER JOIN tbl_purchaseOrder ON tbl_PODetails.purchaseOrderID = tbl_purchaseOrder.ID) 
INNER JOIN tbl_vendor ON tbl_purchaseOrder.VendorID = tbl_vendor.ID) 
INNER JOIN tbl_address AS POAddress ON tbl_purchaseOrder.AddressID = POAddress.ID) 
INNER JOIN tbl_address AS VendorAddress ON tbl_vendor.AddressID = VendorAddress.ID; 

公告我鏈接到tbl_address兩次,我給他們別名POAddressVendorAddress。因此,要爲供應商,你會綁定您的控件到地址信息:VendorAddress.addressVendorAddress.city,等

你有你的SELECT語句將有地址字段改爲:

POAddress.address, POAddress.city, POAddress.provinceOrState, POAddress.postalCode, POAddress.country, VendorAddress.address, VendorAddress.city, VendorAddress.provinceOrState, VendorAddress.postalCode, VendorAddress.country 

因此,您的完整查詢應該如下所示:

SELECT tbl_purchaseOrder.ID, tbl_purchaseOrder.purchaseOrderDate, 
     tbl_vendor.salesContact, tbl_vendor.phone, tbl_vendor.extension, 
     tbl_vendor.fax, tbl_vendor.vendorNo, tbl_vendor.vendorName, 
     tbl_purchaseOrder.shipVIA, tbl_purchaseOrder.FOB, tbl_purchaseOrder.term, 
     tbl_PODetails.quantityOrder, tbl_rawItemList.ItemSerialCode, 
     tbl_PODetails.dueDate, tbl_PODetails.unitCost, tbl_PODetails.unit, 
     tbl_rawItemList.description, tbl_PODetails.specialInstructions, 
     tbl_purchaseOrder.requistionedBy, [unitCost]*[quantityOrder] AS Total, 
     POAddress.address, POAddress.city, POAddress.provinceOrState, 
     POAddress.postalCode, POAddress.country, VendorAddress.address, 
     VendorAddress.city, VendorAddress.provinceOrState, 
     VendorAddress.postalCode, VendorAddress.country 
FROM ((((tbl_PODetails 
    INNER JOIN tbl_rawItemList ON tbl_PODetails.rawItemListID = tbl_rawItemList.ID) 
    INNER JOIN tbl_purchaseOrder ON tbl_PODetails.purchaseOrderID = tbl_purchaseOrder.ID) 
    INNER JOIN tbl_vendor ON tbl_purchaseOrder.VendorID = tbl_vendor.ID) 
    INNER JOIN tbl_address AS POAddress ON tbl_purchaseOrder.AddressID = POAddress.ID) 
    INNER JOIN tbl_address AS VendorAddress ON tbl_vendor.AddressID = VendorAddress.ID; 
WHERE (((tbl_purchaseOrder.ID)=[Enter a PO number:])); 
相關問題