在 Ms Access 查询中插入换行符

Posted

技术标签:

【中文标题】在 Ms Access 查询中插入换行符【英文标题】:Insert Linebreak in Ms Access Query 【发布时间】:2015-03-02 08:04:36 【问题描述】:

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

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

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

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) &

但还是要坚持下去

未定义的函数字符

谁能帮忙?

【问题讨论】:

拼写,CHR,而不是 CHAR:***.com/questions/2203710/… 另外,请查看使用加号 (+) 作为连接符如何与 MS Access 配合使用。 【参考方案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

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

【讨论】:

我认为如果PafAddress.line1 为空,此代码将在任何其他代码之前将空行输出到AddressLabel 至少会有一行被填满,这将是第一行。【参考方案2】:

丑得要命,但这应该适用于 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,

【讨论】:

无论填充多少行,这都会添加一个跟踪换行符。

以上是关于在 Ms Access 查询中插入换行符的主要内容,如果未能解决你的问题,请参考以下文章

MS Access 报告:如何根据显示的文本在文本框中添加换行符

MS Access 报告中的换行符

MS Access 中是不是有回车、换行常量?

MS Access 报告 - 字符处换行?

在java中插入超过8000个字符并带有换行符的sql列

如何在informix的插入查询中添加行跳转?