0

我正在努力使以下查询更好:

Set rs = CurrentDb.OpenRecordset("SELECT CONSG.custpartno AS Assembly, SOM.sono AS SO, SOD.line_no AS Rel, PLD.shippedqty AS Qty, INV.descript AS Description, " & _
  "SHIPADDR.shipto AS ShipToCompany, SHIPADDR.address1 AS ShipToAddress, SHIPADDR.address2 AS ShipToAddress2, SHIPADDR.city AS ShipToCity, SHIPADDR.state AS ShipToState, SHIPADDR.zip AS ShipToZip,  " & _
  "BILLADDR.shipto AS BillToCompany, BILLADDR.address1 AS BillToAddress, BILLADDR.address2 AS BillToAddress2, BILLADDR.city AS BillToCity, BILLADDR.state AS BillToState, BILLADDR.zip AS BillToZip " & _
  "FROM inventor AS CONSG INNER JOIN ((((((plmain AS PLM INNER JOIN pldetail AS PLD ON PLM.packlistno = PLD.packlistno) INNER JOIN sodetail AS SOD ON PLD.uniqueln = SOD.uniqueln) INNER JOIN inventor AS INV ON SOD.uniq_key = INV.uniq_key) INNER JOIN somain AS SOM ON PLM.sono = SOM.sono) INNER JOIN shipbill AS SHIPADDR ON PLM.linkadd = SHIPADDR.linkadd) INNER JOIN shipbill AS BILLADDR ON PLM.blinkadd = BILLADDR.linkadd) ON CONSG.int_uniq = INV.uniq_key  " & _
  "WHERE Val(PLM.packlistno) = " & cboPL.value, dbOpenDynaset)

我想为账单地址设置一个字段,为发货地址设置一个字段,而不是组成地址的所有单个字段。我想要以下格式:

Company Name
110 Company Street Address
Address second line 
City, State Zip

对于某些地址,没有第二个地址行。所以,我想完全跳过那一行。此外,由于某种原因,每个不需要存在的字段后面都有额外的空间。

有谁知道我怎么能做到这一点?

4

1 回答 1

1
Set rs = CurrentDb.OpenRecordset("SELECT CONSG.custpartno AS Assembly, SOM.sono AS SO, SOD.line_no AS Rel, PLD.shippedqty AS Qty, INV.descript AS Description, " & _
  "Trim(SHIPADDR.shipto) & Chr(13) & Chr(10) & Trim(SHIPADDR.address1) & Chr(13) & Chr(10) & IIf(Len(Trim(SHIPADDR.address2) & '')<>0,Trim(SHIPADDR.address2) & Chr(13) & Chr(10),'') & Trim(SHIPADDR.city) & ', ' & Trim(SHIPADDR.state) & ' ' & Trim(SHIPADDR.zip) AS ShipTo,   " & _
  "Trim(BILLADDR.shipto) & Chr(13) & Chr(10) & Trim(BILLADDR.address1) & Chr(13) & Chr(10) & IIf(Len(Trim(BILLADDR.address2) & '')<>0,Trim(BILLADDR.address2) & Chr(13) & Chr(10),'') & Trim(BILLADDR.city) & ', ' & Trim(BILLADDR.state) & ' ' & Trim(BILLADDR.zip) AS BillTo " & _
  "FROM inventor AS CONSG INNER JOIN ((((((plmain AS PLM INNER JOIN pldetail AS PLD ON PLM.packlistno = PLD.packlistno) INNER JOIN sodetail AS SOD ON PLD.uniqueln = SOD.uniqueln) INNER JOIN inventor AS INV ON SOD.uniq_key = INV.uniq_key) INNER JOIN somain AS SOM ON PLM.sono = SOM.sono) INNER JOIN shipbill AS SHIPADDR ON PLM.linkadd = SHIPADDR.linkadd) INNER JOIN shipbill AS BILLADDR ON PLM.blinkadd = BILLADDR.linkadd) ON CONSG.int_uniq = INV.uniq_key  " & _
  "WHERE Val(PLM.packlistno) = " & cboPL.value, dbOpenDynaset)

在上面的Chr(13)= 回车和Chr(10)= 换行。我Trim()用来删除字段的任何前导和尾随空格。然后,如果没有空白地址行 2 的额外行,您可以检查长度,如果它大于 0,则仅添加到地址。

于 2013-10-02T17:08:11.827 回答