将所有地址字段合并到一个字段中

时间:2013-10-02 17:01:23

标签: vba ms-access access-vba

我正在努力改进以下查询:

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)

我想为Bill To地址设置一个字段,为Ship To地址设置一个字段,而不是组成地址的所有单个字段。我希望它采用以下格式:

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

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

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

1 个答案:

答案 0 :(得分: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时才添加到地址。