473,407 Members | 2,359 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Exclude carriage returns from expression if they are not needed?

Hello,

Note: Can not use code on my system. Disabled Mode will not be enabled for me.

My query pulls data that requires carriage returns if there’s data, and no carriage return if there is none. How do I write a concatenating expression with carriage returns, but skip the carriage return if no data precedes the carriage return expression. I’m asking in order to eliminate white space in the query return due to a poorly written expression.

Currently written as [Expr1] + Chr(13) & Chr(10) + [Expr2] + Chr(13) & Chr(10) + etc because I was under the impression that + meant “optional”.

But, I need if [Expr1] is Null then skip the carriage return.

Thank You!
Apr 6 '16 #1

✓ answered by NeoPa

Hi. It looks to me as if you're struggling with a complicated expression and making a couple of minor errors in there. Hence dealing with it needs careful consideration of a number of the parts.

I'll start by pointing you towards an article (Using "&" and "+" in WHERE Clause) that explains the concept of using + and &. TBF you had the basic idea but were a little off on the details.

Now, as Smiley explains in his post, only those items to the immediate left and right of the + are examined to determine if the expression should resolve to Null or not. That means, if you want a carriage return / line feed pair to be optional depending on an expression then the carriage return / line feed pair must be enclosed within parentheses. Otherwise it only operates on the CR and leaves the LF in place.

Another important point to remember is that '' (or even "" if you're using Access' default quote char) is quite different from Null. The first is an empty string. The second isn't even of type string at all, and can only be used with strings when coerced. So '' + (Chr(13) & Chr(10)) resolves to Chr(13) & Chr(10), whereas Null + (Chr(13) & Chr(10)) resolves to Null.

A last point is that form and report controls typically have a value of Null when empty. Fields (String), on the other hand, can have either Null or empty string ('') depending on its attributes (.Required & .AllowZeroLength). So it's important to remember what you're dealing with. In your case it seems to be fields rather than controls.

Assuming your empty fields contain Nulls instead of empty strings you'll need something like :
Expand|Select|Wrap|Line Numbers
  1. [Expr1] + (Chr(13) & Chr(10)) & [Expr2] + (Chr(13) & Chr(10)) & [Expr3] + (Chr(13) & Chr(10)) & ...
If the values are empty strings instead :
Expand|Select|Wrap|Line Numbers
  1. [Expr1] & IIf([Expr1]>'',Chr(13) & Chr(10),'') & [Expr2] & IIf([Expr2]>'',Chr(13) & Chr(10),'') & ...
Notice this is more clumsy so I'd consider setting your fields up to contain Nulls when empty if you can.

6 1475
TheSmileyCoder
2,322 Expert Mod 2GB
You need to be clearer in your instructions to the machine. At current you are getting:
[Expr1] + Chr(13) & Chr(10)
evaluating to
Null & chr(10)
evaluating to
chr(10)
so instead, use:
([Expr1] + Chr(13) + Chr(10)) & ([Expr2] + Chr(13) + Chr(10))
By adding the paranthesis (changing a + to a & and a & to a +), we are now changing the order of evaluation of the expressions.

Hope that helps.
Apr 6 '16 #2
PhilOfWalton
1,430 Expert 1GB
I think You're missing the point. The carriage returns go with Expr2, not Expr1.
So you need something like
FinalExpr = IIf(Nz(Expr1) > "", Expr1) & IIf(Nz(Expr2) > "", Chr$(13) & Chr$(10) & Expr2) & IIf(Nz(Expr3) > "", Chr$(13) & Chr$(10) & Expr3)
etc etc.
So see if ExprX has a value, and only if it exists, add the preceding line feeds and ExprX

Phil
Apr 6 '16 #3
NeoPa
32,556 Expert Mod 16PB
Hi. It looks to me as if you're struggling with a complicated expression and making a couple of minor errors in there. Hence dealing with it needs careful consideration of a number of the parts.

I'll start by pointing you towards an article (Using "&" and "+" in WHERE Clause) that explains the concept of using + and &. TBF you had the basic idea but were a little off on the details.

Now, as Smiley explains in his post, only those items to the immediate left and right of the + are examined to determine if the expression should resolve to Null or not. That means, if you want a carriage return / line feed pair to be optional depending on an expression then the carriage return / line feed pair must be enclosed within parentheses. Otherwise it only operates on the CR and leaves the LF in place.

Another important point to remember is that '' (or even "" if you're using Access' default quote char) is quite different from Null. The first is an empty string. The second isn't even of type string at all, and can only be used with strings when coerced. So '' + (Chr(13) & Chr(10)) resolves to Chr(13) & Chr(10), whereas Null + (Chr(13) & Chr(10)) resolves to Null.

A last point is that form and report controls typically have a value of Null when empty. Fields (String), on the other hand, can have either Null or empty string ('') depending on its attributes (.Required & .AllowZeroLength). So it's important to remember what you're dealing with. In your case it seems to be fields rather than controls.

Assuming your empty fields contain Nulls instead of empty strings you'll need something like :
Expand|Select|Wrap|Line Numbers
  1. [Expr1] + (Chr(13) & Chr(10)) & [Expr2] + (Chr(13) & Chr(10)) & [Expr3] + (Chr(13) & Chr(10)) & ...
If the values are empty strings instead :
Expand|Select|Wrap|Line Numbers
  1. [Expr1] & IIf([Expr1]>'',Chr(13) & Chr(10),'') & [Expr2] & IIf([Expr2]>'',Chr(13) & Chr(10),'') & ...
Notice this is more clumsy so I'd consider setting your fields up to contain Nulls when empty if you can.
Apr 7 '16 #4
NeoPa
32,556 Expert Mod 16PB
Hi Phil.

While it probably doesn't matter whether the new line characters are dropped depending on the previous or subsequent expressions, the OP's original code seems to indicate their intention of linking it to the previous.

As I say though, at the end of the day both work perfectly well.
Apr 7 '16 #5
NeoPa,
Your answer not only fixed this problem for me, but also led to answering a bunch of other problems I was having. I now have a better understanding of these above issues.

Thank you for your time Sir!

And thanks to all of you for doing what you do here. Much appreciated!
Apr 8 '16 #6
NeoPa
32,556 Expert Mod 16PB
I'm very pleased to be able to help.

I've also made a note of your kind post to show to my MVP lead next time my award comes up for renewal, so that's helpful for me too :-)
Apr 8 '16 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Dr. Laurence Leff | last post by:
I am writing a Java program to read in XML file, modify some elements slightly, and then write it out. That XML file is prepared in Docbook. It works fine, except that it is disturbing the...
1
by: dave | last post by:
I have placed html in the database. I am trying to capture items between two text blocks. The content string is like the following </FONT><BR> <BR> <BR> some text here<BR> <A NAME=" I am...
4
by: Simon Harris | last post by:
Hi All, I am trying to write a function that will remove all carriage returns from a string, so that the string of words can be used as meta keywords. So far I have: Function...
4
by: Les Juby | last post by:
Can someone please help with a suggestion as to how I can keep the formatting (carriage returns) that the user enters into a memo field and then display that later. I figured I might be able to...
3
by: Dave | last post by:
I need to strip out carriage returns from a memo field. Basically, one of the commercial databases we have uses a memo field for the address, and what I'd like to do is get rid of the carriage...
2
by: Matt Mercer | last post by:
Hi all, I am having a frustration problem, and I have read about 25 newsgroup postings that do not have a satisfying answer :) The problem appears to be common where carriage returns are lost...
8
by: TheDude5B | last post by:
Hi, I have some data which is stored in my MySQL database as TEXT. when the data is entered in, it has some carriage returns in it, and this can be seen when querying the data using MySQL Query...
7
by: mattrapoport | last post by:
I have a page with a div on it. The div displays a user comment. When the user logs into this page, their current comment is pulled from a db and displayed in the div. The user can edit the...
0
by: markus.shure | last post by:
Hi, I'm noticed a problem testing a JAX-WS client with a WSE server. The JAX-WS client adds carriage returns to a SOAP header element that is signed. This causes the WSE server to raise an...
7
by: newbtemple | last post by:
Hey all. I'm having a bugger of a time trying to remove carriage returns. I have a large text file with a bunch of little squares. I'm guessing they're carriage returns from what i've googled. They...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.