這么長(不想干了)
Me.RecordSource = "SELECT TOP 100 PERCENT dbo.tbl_Store_DeliveryNote.DocID, dbo.tbl_Store_DeliveryNote.Customer_PName, dbo.qry_Sales_SO_Active.sName,dbo.Qry_Sales_SO_Active.CS,dbo.Qry_Sales_SO_Active.salesman, " & _"dbo.tbl_Store_DeliveryNote.DeliveryAddress, dbo.tbl_Store_DeliveryNote.CountryOfOrigin, " & _
"dbo.tbl_Store_DeliveryNote.ContactP, dbo.tbl_Store_DeliveryNote.ContactTel, " & _
"dbo.tbl_Store_DeliveryNote.ContactFax, dbo.tbl_Store_DeliveryNote.Remark AS DN_Remark, " & _
"dbo.tbl_Store_DeliveryNote_Detail.Item_No,tbl_Custom_CustomerItem.Custom_Name_Out, " & _
"SUM(CASE WHEN A.UOM_Cust = 'pcs' THEN dbo.tbl_Store_DeliveryNote_Detail.Quantity_Ar/A.Cust2Std ELSE dbo.tbl_Store_DeliveryNote_Detail.Quantity_Ar END) AS quantity_ar," & _
"SUM(dbo.tbl_Store_DeliveryNote_Detail.NOP) AS nop, dbo.tbl_Store_DeliveryNote_Detail.Remark AS Detail_Remark, " & _
"dbo.tbl_ItemMaster.Item_Desc, dbo.tbl_ItemMaster.Custom_No, dbo.tbl_Store_DeliveryNote.DocDate, " & _
"SUM((CASE WHEN A.UOM_Cust = 'pcs' THEN dbo.tbl_Store_DeliveryNote_Detail.Quantity_Ar/A.Cust2Std ELSE dbo.tbl_Store_DeliveryNote_Detail.Quantity_Ar END) * (Case When T.UOM2Rate Is Null Then dbo.tbl_ItemMaster.UOM2Rate Else T.UOM2Rate End)) SumWeight, dbo.qry_Sales_SO_Active.cust_Item_No, " & _
" (Case when Left( dbo.tbl_Store_DeliveryNote_Detail.Item_No,1)='F' and A.UOM_Cust is null then 'PCS' When Left( dbo.tbl_Store_DeliveryNote_Detail.Item_No,1)<>'F' and A.UOM_Cust is null then 'K' else A.UOM_Cust end ) AS UOM, dbo.qry_Sales_SO_Active.Doc_ID, dbo.qry_Sales_SO_Active.po_No " & _
" FROM dbo.tbl_Store_DeliveryNote INNER JOIN dbo.tbl_Store_DeliveryNote_Detail ON " & _
" dbo.tbl_Store_DeliveryNote.DN_ID = dbo.tbl_Store_DeliveryNote_Detail.DN_ID INNER Join dbo.qry_Sales_SO_Active INNER JOIN " & _
" dbo.tbl_ItemMaster ON dbo.qry_Sales_SO_Active.item_No = dbo.tbl_ItemMaster.Item_NO ON " & _
" dbo.tbl_Store_DeliveryNote_Detail.So_ItemID = dbo.qry_Sales_SO_Active.SO_Item_ID " & _
" Left Outer Join (Select Customer_ID,Item_NO,Cust_Item_NO,Min(Custom_uKey) As Custom_uKey From tbl_Sales_CustomerItem Where Custom_uKey is not null Group By Customer_ID,Item_NO,Cust_Item_NO) TC ON Qry_Sales_SO_Active.Customer_ID=TC.Customer_ID AND Qry_Sales_SO_Active.Item_NO=TC.Item_NO AND Qry_Sales_SO_Active.Cust_Item_No=TC.Cust_Item_NO " & _
" Left Outer JOIN tbl_Custom_CustomerItem ON TC.Custom_uKey=tbl_Custom_CustomerItem.DetailKey AND TC.Customer_ID=tbl_Custom_CustomerItem.Customer_ID " & _
" Left Outer Join ( SELECT T1.item_No,T1.LotID,AVG(UOM2Rate) AS uom2rate from dbo.tbl_Store_FinProIN_Detail Right Outer Join (Select distinct Item_No,LotID From dbo.tbl_Store_DeliveryNote_Detail, dbo.tbl_Store_DeliveryNote where dbo.tbl_Store_DeliveryNote_Detail.dn_ID= dbo.tbl_Store_DeliveryNote.dn_ID and dbo.tbl_Store_DeliveryNote.DocID= '" & Str_DocID & "') T1 On dbo.tbl_Store_FinProIN_Detail.Item_No=T1.Item_No and dbo.tbl_Store_FinProIN_Detail.LotID=T1.LotID Group bY T1.item_No,T1.LotID) T ON dbo.tbl_Store_DeliveryNote_Detail.Item_No=T.Item_No and dbo.tbl_Store_DeliveryNote_Detail.LotID=T.LotID " & _
" LEFT OUTER Join (select distinct customer_ID,Item_no,(Case When Left(Cust_Item_no,1)='*' then Right(Cust_Item_no,Len(Cust_Item_no)-1) When Left(Cust_Item_no,2)='**' then Right(Cust_Item_no,Len(Cust_Item_no)-2) When Left(Cust_Item_no,3)='***' then Right(Cust_Item_no,Len(Cust_Item_no)-3) When Left(Cust_Item_no,4)='****' then Right(Cust_Item_no,Len(Cust_Item_no)-4) When Left(Cust_Item_no,5)='*****' then Right(Cust_Item_no,Len(Cust_Item_no)-5) When Left(Cust_Item_no,6)='******' then Right(Cust_Item_no,Len(Cust_Item_no)-6) else Cust_Item_no end) AS Cust_Item_no ,UOM_Cust,Cust2Std from dbo.tbl_sales_CustomerItem) A ON dbo.qry_Sales_SO_Active.customer_ID = A.customer_ID AND dbo.qry_Sales_SO_Active.item_No = A.Item_no AND " & _
" a.cust_Item_No = dbo.qry_Sales_SO_Active.cust_Item_No WHERE dbo.tbl_Store_DeliveryNote_Detail.Quantity_Ar>0 GROUP BY dbo.tbl_Store_DeliveryNote.DocID, dbo.tbl_Store_DeliveryNote.Customer_PName, " & _
" dbo.qry_Sales_SO_Active.sName,dbo.Qry_Sales_SO_Active.CS,dbo.Qry_Sales_SO_Active.salesman,dbo.tbl_Store_DeliveryNote.DeliveryAddress, dbo.tbl_Store_DeliveryNote.CountryOfOrigin, dbo.tbl_Store_DeliveryNote.ContactP, dbo.tbl_Store_DeliveryNote.ContactTel, " & _
"dbo.tbl_Store_DeliveryNote.ContactFax, dbo.tbl_Store_DeliveryNote.Remark, dbo.tbl_Store_DeliveryNote_Detail.Item_No, dbo.tbl_Store_DeliveryNote_Detail.Remark, dbo.tbl_ItemMaster.Item_Desc, " & _
"dbo.tbl_ItemMaster.Custom_No, dbo.tbl_Store_DeliveryNote.DocDate, dbo.qry_Sales_SO_Active.cust_Item_No, (Case when Left( dbo.tbl_Store_DeliveryNote_Detail.Item_No,1)='F' and A.UOM_Cust is null then 'PCS' When Left(dbo.tbl_Store_DeliveryNote_Detail.Item_No,1)<>'F' and A.UOM_Cust is null then 'K' else A.UOM_Cust end ), dbo.qry_Sales_SO_Active.Doc_ID,dbo.tbl_ItemMaster.UOM2Rate, " & _
" dbo.qry_Sales_SO_Active.po_No, A.Cust2Std,tbl_Custom_CustomerItem.Custom_Name_Out having (dbo.tbl_Store_DeliveryNote.DocID = '" & Str_DocID & "') " & _
"ORDER BY dbo.tbl_Store_DeliveryNote_Detail.Item_No, dbo.tbl_Store_DeliveryNote_Detail.Quantity_Ar "