Good evening all
When controlling the view of text in an InputBox or MsgBox, I have been in the habit of using Chr(13) where vbCrLf appears to be equivalent. I seek your advice on the advantages and or cautions on using one versus the other.
![]() |
There are isolated problems with current patches, but they are well-known and documented on this site. |
SIGN IN | Not a member? | REGISTER | PLUS MEMBERSHIP |
-
vbCrLf (VBA/all)
Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » vbCrLf (VBA/all)
- This topic has 10 replies, 5 voices, and was last updated 20 years, 3 months ago.
AuthorTopicDon Wells
AskWoody LoungerFebruary 23, 2005 at 1:42 am #416231Viewing 1 reply threadAuthorReplies-
WScharlotte
AskWoody Lounger -
Don Wells
AskWoody Lounger
-
-
WSAlanMiller
AskWoody LoungerFebruary 23, 2005 at 5:29 am #930363As Charlotte says, the two are not equivalent, although they may end up having an equivalent effect.
The Cr part is a Carriage return – Chr(13) and the Lf part is a Linefeed – Chr(10).
For the sorts of controls you’re speaking of, they have the same effect on the output, but some programs (Notepad for instance) won’t begin a new line unless there is a carriage return and linefeed. If there is only one or the other it displays the character missing box as a character. This is also a “gotcha” for text files formatted as ASCII vs. Unix.IMO, it’s always better to use one of the built-in VB constants because you get the bonuses of intellisense come implicit error checking if you use “Option Explicit”… and, of course, you do use “Option Explicit”… don’t you Don!
These are some of the common ones:vbCrLf = “n” # Carriage returnlinefeed combination
vbCr = chr(13) # Carriage return character
vbLf = chr(10) # Linefeed character
vbNewLine = “n” # Platform-specific new line character; whichever is appropriate for current platform
vbNullChar = chr(0) # Character having value 0
vbNullString = chr(0) # String having value 0 Not the same as a zero-length string (“”); used for calling external procedures
vbTab = chr(9) # Tab character
vbBack = chr(8) # Backspace character
vbFormFeed = chr(12) # Not useful in Microsoft Windows
vbVerticalTab = chr(11) # Not useful in Microsoft WindowsYou’ll find a more comprehensive list here.
Alan
-
Don Wells
AskWoody Lounger -
WSAlanMiller
AskWoody LoungerFebruary 23, 2005 at 11:20 am #930463I think that’s the way we all learned it, Don. We’re as bad as teenagers – won’t listen, but finally resign after getting sick & tired of being beaten black & blue. I’m surprised at myself on that one, having been weened on C/C++, where you have to declare absolutely everything.
Glad the info helped too.
Alan
-
-
WSGary Frieder
AskWoody LoungerFebruary 24, 2005 at 5:43 am #930840Alan,
Just a quibble on one of the entries in this helpful list:
vbNullString = chr(0) # String having value 0 Not the same as a zero-length string (“”); used for calling external procedures
– not sure that that is accurate.
From way back I’ve used vbNullString in place of a a zero-length string. Here’s a snippet from VB & VBA In a Nutshell, by Paul Lomax:
[indent]
vbNullString
Assigns or tests for a zero-length (empty) string. For example, the statement:
strVar1 = vbNullString
is equivalent to:
strVar1 = “”
[/indent]
The following code will return a blank message box, not one with “0” in it:Dim str As String str = vbNullString MsgBox str
Gary
-
WSHansV
AskWoody LoungerFebruary 24, 2005 at 7:29 am #930846The sentence[indent]
Not the same as a zero-length string (“”); used for calling external procedures
[/indent] is straight from the VBA help file – which is wrong here! You are correct that vbNullString is exactly the same as “”. I think that sentence was intended for vbNullChar, a string containing Chr(0); this is used where Windows API functions expect C-style (zero-terminated) strings.
-
WSAlanMiller
AskWoody LoungerFebruary 24, 2005 at 10:18 am #930876I must say that I skipped over that, but agree with you that it is incorrect. I thought the author might be confusing “Null is not the same as Empty” with “vbNullString is not the same as an empty string”.
However, I think there may be a subtle difference that the author didn’t realize (since he/she appears to have lifted it straight from the VBA help file, as Hans points out). Do correct me if I’m wrong on this, because it may be an inappropriate hangover from C/C++. An empty string “” is an actual string, of the string data type, and therefore carries the minimum overhead of any other string – some handful of bytes. But vbNullString is just a zero, denoting the equivalent of a NULL in C. Although the results of using either are the same at the end of the day, the length of the day might vary since vbNullString would be faster to process.
Alan
Edited – I think I’m right on this, as detailed in Faster Visual Basic Programs:
“A loop assigning an empty string to a string variable 10 million times gives surprising results, too. While absolute times may again differ on your system, the improvement when using vbNullString is significant:
e$ = “” 1.51 s
e$ = vbNullString 0.38 sNote that the predefined constant vbNullString is not exactly the same as an emtpy string, but it can be used as an equivalent and even returns true when comparing vbNullString = “” (while vbNullString is a null pointer, “” results in a pointer to a string with zero length; however, not all API/DLL functions handle them identically, so be careful).
Checking if a string is empty 10 million times can also be done in different ways. Here are the times for doing it 10 million times on our test system:
If “ABC” = “” 0.73 s
If “ABC” = vbNullString 0.57 s
If Len(“ABC”) = 0 0.46 s
If LenB(“ABC”) = 0 0.39 sSo the speed nearly doubles if you are using LenB instead of comparing the string with an empty string. Note that you should not replace Len by LenB in general, since LenB returns the byte position in the VB double-byte strings instead of the character position. But for simply checking if the length is zero, LenB is the fastest method.”
-
WSGary Frieder
AskWoody LoungerFebruary 24, 2005 at 3:08 pm #930977Alan,
Thanks for the link – an interesting read, and some good tips there.
The explanation of vbNullString gibes with what I’d read in the past (originally in an article by Ken Getz on ways to optimize VBA code – couldn’t find the original article, but a version that’s part of an article on optimizing Access applications can be found in the MSDN library
here). Don’t know if the increased speed matters anymore, but I prefer vbNullString to “” in my code simply because it stands out more clearly when I read it!Gary
-
WSAlanMiller
AskWoody LoungerFebruary 25, 2005 at 1:39 pm #931089Hi Gary
It’s an interesting but subtle point… but as you say, will the speed difference be significant at the end of the day? I too lean towards using the built-in constant names for other good reasons. I’ve also still got the “#define” C preprocessor monkey on my back (a good thing probably) that pushes me towards this kind of usage.
Alan
Just to add something concrete to the argument, the following code:
Public Sub testStrings() Dim strEmpty As String Dim strNull As String strEmpty = "" strNull = vbNullString Debug.Print LenB(strEmpty) Debug.Print StrPtr(strEmpty) Debug.Print LenB(strNull) Debug.Print StrPtr(strNull) End Sub
will output:
0
5032560
0
0Clearly, the two cases are different. First up, it appears that a “string” variable in VB is actually a (2-byte) pointer to a character array, similar to but not the same as those used in C. The fact that the pointer to strNull is zero would indicate that the variable strNull has not actually been initialized. By contrast, the pointer to strEmpty is not Null, so memory has been allocated to strEmpty.
Even though LenB(strEmpty) is zero, I think that this is a result of how the character array is stored, as well as how LenB() reports. It’s now my understanding that a VB string “abc” (not the pointer, but the actual contents) has the following structure:
length of array in bytes: 00 00 00 06 (4-byte value = 6, excluding NULL terminator)
unicode character array: 00 61 00 62 00 63 (6 bytes of unicode)
NULL terminator: 00 00I believe that strEmpty would therefore be stored as:
00 00 00 00
(nothing here)
00 00which is a total of 6 bytes of “nothingness”. The LenB() function appears to report the 4-byte length value i.e. the number of bytes after the first 4-byte length value, up to but not including the (2-byte Unicode) NULL terminator – so it returns zero for a “” empty string. But the 6-byte overhead is always there. Apparently, depending on what’s using the string, the unicode character array can contain other null characters not intended as terminators, so the 4-byte length value precedes the character array proper, in addition to the NULL terminator. I don’t understand the mechanics of this aspect myself.
-
-
-
-
Viewing 1 reply thread -

Plus Membership
Donations from Plus members keep this site going. You can identify the people who support AskWoody by the Plus badge on their avatars.
AskWoody Plus members not only get access to all of the contents of this site -- including Susan Bradley's frequently updated Patch Watch listing -- they also receive weekly AskWoody Plus Newsletters (formerly Windows Secrets Newsletter) and AskWoody Plus Alerts, emails when there are important breaking developments.
Get Plus!
Welcome to our unique respite from the madness.
It's easy to post questions about Windows 11, Windows 10, Win8.1, Win7, Surface, Office, or browse through our Forums. Post anonymously or register for greater privileges. Keep it civil, please: Decorous Lounge rules strictly enforced. Questions? Contact Customer Support.
Search Newsletters
Search Forums
View the Forum
Search for Topics
Recent Topics
-
Office gets current release
by
Susan Bradley
4 hours, 20 minutes ago -
FBI: Still Using One of These Old Routers? It’s Vulnerable to Hackers
by
Alex5723
9 hours, 42 minutes ago -
Windows AI Local Only no NPU required!
by
RetiredGeek
6 hours, 26 minutes ago -
Stop the OneDrive defaults
by
CWBillow
10 hours, 31 minutes ago -
Windows 11 Insider Preview build 27868 released to Canary
by
joep517
20 hours, 27 minutes ago -
X Suspends Encrypted DMs
by
Alex5723
22 hours, 39 minutes ago -
WSJ : My Robot and Me AI generated movie
by
Alex5723
22 hours, 57 minutes ago -
Botnet hacks 9,000+ ASUS routers to add persistent SSH backdoor
by
Alex5723
23 hours, 34 minutes ago -
OpenAI model sabotages shutdown code
by
Cybertooth
1 day ago -
Backup and access old e-mails after company e-mail address is terminated
by
M W Leijendekker
12 hours, 21 minutes ago -
Enabling Secureboot
by
ITguy
19 hours, 20 minutes ago -
Windows hosting exposes additional bugs
by
Susan Bradley
1 day, 8 hours ago -
No more rounded corners??
by
CWBillow
1 day, 3 hours ago -
Android 15 and IPV6
by
Win7and10
17 hours, 39 minutes ago -
KB5058405 might fail to install with recovery error 0xc0000098 in ACPI.sys
by
Susan Bradley
1 day, 20 hours ago -
T-Mobile’s T-Life App has a “Screen Recording Tool” Turned on
by
Alex5723
1 day, 23 hours ago -
Windows 11 Insider Preview Build 26100.4202 (24H2) released to Release Preview
by
joep517
1 day, 17 hours ago -
Windows Update orchestration platform to update all software
by
Alex5723
2 days, 6 hours ago -
May preview updates
by
Susan Bradley
1 day, 17 hours ago -
Microsoft releases KB5061977 Windows 11 24H2, Server 2025 emergency out of band
by
Alex5723
1 day, 9 hours ago -
Just got this pop-up page while browsing
by
Alex5723
1 day, 22 hours ago -
KB5058379 / KB 5061768 Failures
by
crown
1 day, 19 hours ago -
Windows 10 23H2 Good to Update to ?
by
jkitc
21 hours, 54 minutes ago -
At last – installation of 24H2
by
Botswana12
2 days, 21 hours ago -
MS-DEFCON 4: As good as it gets
by
Susan Bradley
18 hours, 41 minutes ago -
RyTuneX optimize Windows 10/11 tool
by
Alex5723
3 days, 10 hours ago -
Can I just update from Win11 22H2 to 23H2?
by
Dave Easley
1 day, 8 hours ago -
Limited account permission error related to Windows Update
by
gtd12345
3 days, 23 hours ago -
Another test post
by
gtd12345
3 days, 23 hours ago -
Connect to someone else computer
by
wadeer
1 hour, 2 minutes ago
Recent blog posts
Key Links
Want to Advertise in the free newsletter? How about a gift subscription in honor of a birthday? Send an email to sb@askwoody.com to ask how.
Mastodon profile for DefConPatch
Mastodon profile for AskWoody
Home • About • FAQ • Posts & Privacy • Forums • My Account
Register • Free Newsletter • Plus Membership • Gift Certificates • MS-DEFCON Alerts
Copyright ©2004-2025 by AskWoody Tech LLC. All Rights Reserved.