在Ms Access Query中插入换行符

时间:2015-03-02 08:04:36

标签: php ms-access

您好我正在开发一个PHP应用程序,不幸的是必须使用MS Access作为后端,与mysql相比,我不是100%最新的访问权限。

我正在尝试在customer表中生成一个地址标签字段。我们的想法是拥有一个地址行(最多7个),如果不为null则包含行,然后为下一行包含换行符,这样就可以获得没有空行的邮资标签样式。

到目前为止,我在查询中有这个...

SELECT
tblCompany.CompanyId,
tblCompany.CompanyTypeId,
tblCompany.CompanyName,
    IIF(Len(PafAddress.line1)>0,PafAddress.line1,"") & " "  &
    IIF(Len(PafAddress.line2)>0,PafAddress.line2,"") & " " &
    IIF(Len(PafAddress.line3)>0,PafAddress.line3,"") & " " &
    IIF(Len(PafAddress.line4)>0,PafAddress.line4,"") & " " &
    IIF(Len(PafAddress.Line5)>0,PafAddress.line5,"") & " " &
    IIF(Len(PafAddress.post_town)>0,PafAddress.post_town,"") & " " &
    IIF(Len(PafAddress.county)>0,PafAddress.county,"") & " " &
    IIF(Len(PafAddress.postcode)>0,PafAddress.postcode,"") 
    AS AddressLabel,
FROM tblCompany
LEFT OUTER JOIN PafAddress ON tblCompany.PafAddressId = PafAddress.id

然而,这只是在每个条目旁边放了几个空格而不是换行符,我试过

  

& CHAR(13)+CHAR(10) &

但只是继续

  

未定义的功能字符

有人可以帮忙吗?

2 个答案:

答案 0 :(得分:1)

空字段不是零长度,而是Null,您必须使用Access SQL和函数名称:

SELECT
tblCompany.CompanyId,
tblCompany.CompanyTypeId,
tblCompany.CompanyName,
    PafAddress.line1 &
    (Chr(13) + Chr(10) + PafAddress.line2) &
    (Chr(13) + Chr(10) + PafAddress.line3) &
    (Chr(13) + Chr(10) + PafAddress.line4) &
    (Chr(13) + Chr(10) + PafAddress.line5) &
    (Chr(13) + Chr(10) + PafAddress.post_town) &
    (Chr(13) + Chr(10) + PafAddress.county) &
    (Chr(13) + Chr(10) + PafAddress.postcode)
AS AddressLabel,
FROM tblCompany
LEFT OUTER JOIN PafAddress 
    ON tblCompany.PafAddressId = PafAddress.id

这会在非空字段之前插入新行。

答案 1 :(得分:0)

丑陋,但这应该适用于AddressLabel字段:

Left(IIF(Not(IsNull(PafAddress.line1)),PafAddress.line1 & Chr(13) & Chr(10),"") & IIF(Not(IsNull(PafAddress.line2)),PafAddress.line2 & Chr(13) & Chr(10),"") & IIF(Not(IsNull(PafAddress.line3)),PafAddress.line3 & Chr(13) & Chr(10),"") & IIF(Not(IsNull(PafAddress.line4)),PafAddress.line4 & Chr(13) & Chr(10),"") & IIF(Not(IsNull(PafAddress.Line5)),PafAddress.line5 & Chr(13) & Chr(10),"") & IIF(Not(IsNull(PafAddress.post_town)),PafAddress.post_town & Chr(13) & Chr(10),"") & IIF(Not(IsNull(PafAddress.county)),PafAddress.county & Chr(13) & Chr(10),"") & IIF(Not(IsNull(PafAddress.postcode)),PafAddress.postcode & Chr(13) & Chr(10),""), Len(IIF(Not(IsNull(PafAddress.line1)),PafAddress.line1 & Chr(13) & Chr(10),"") & IIF(Not(IsNull(PafAddress.line2)),PafAddress.line2 & Chr(13) & Chr(10),"") & IIF(Not(IsNull(PafAddress.line3)),PafAddress.line3 & Chr(13) & Chr(10),"") & IIF(Not(IsNull(PafAddress.line4)),PafAddress.line4 & Chr(13) & Chr(10),"") & IIF(Not(IsNull(PafAddress.Line5)),PafAddress.line5 & Chr(13) & Chr(10),"") & IIF(Not(IsNull(PafAddress.post_town)),PafAddress.post_town & Chr(13) & Chr(10),"") & IIF(Not(IsNull(PafAddress.county)),PafAddress.county & Chr(13) & Chr(10),"") & IIF(Not(IsNull(PafAddress.postcode)),PafAddress.postcode & Chr(13) & Chr(10),"")-2)) AS AddressLabel,