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.
![]() |
Patch reliability is unclear. Unless you have an immediate, pressing need to install a specific patch, don't do it. |
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, 2 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
-
CFPB Quietly Kills Rule to Shield Americans From Data Brokers
by
Alex5723
1 hour, 55 minutes ago -
89 million Steam account details just got leaked,
by
Alex5723
10 hours, 20 minutes ago -
KB5058405: Linux – Windows dual boot SBAT bug, resolved with May 2025 update
by
Alex5723
10 hours, 28 minutes ago -
A Validation (were one needed) of Prudent Patching
by
Nibbled To Death By Ducks
1 hour, 26 minutes ago -
Master Patch Listing for May 13, 2025
by
Susan Bradley
3 hours, 50 minutes ago -
Installer program can’t read my registry
by
Peobody
3 hours, 26 minutes ago -
How to keep Outlook (new) in off position for Windows 11
by
EspressoWillie
23 hours, 58 minutes ago -
Intel : CVE-2024-45332, CVE-2024-43420, CVE-2025-20623
by
Alex5723
6 hours, 34 minutes ago -
False error message from eMClient
by
WSSebastian42
21 hours, 36 minutes ago -
Awoke to a rebooted Mac (crashed?)
by
rebop2020
1 day, 6 hours ago -
Office 2021 Perpetual for Mac
by
rebop2020
1 day, 7 hours ago -
AutoSave is for Microsoft, not for you
by
Will Fastie
4 hours, 29 minutes ago -
Difface : Reconstruction of 3D Human Facial Images from DNA Sequence
by
Alex5723
1 day, 11 hours ago -
Seven things we learned from WhatsApp vs. NSO Group spyware lawsuit
by
Alex5723
12 hours, 26 minutes ago -
Outdated Laptop
by
jdamkeene
1 day, 16 hours ago -
Updating Keepass2Android
by
CBFPD-Chief115
1 day, 22 hours ago -
Another big Microsoft layoff
by
Charlie
1 day, 21 hours ago -
PowerShell to detect NPU – Testers Needed
by
RetiredGeek
1 day, 1 hour ago -
May 2025 updates are out
by
Susan Bradley
18 minutes ago -
Windows 11 Insider Preview build 26200.5600 released to DEV
by
joep517
2 days, 3 hours ago -
Windows 11 Insider Preview build 26120.3964 (24H2) released to BETA
by
joep517
2 days, 4 hours ago -
Drivers suggested via Windows Update
by
Tex265
2 days, 3 hours ago -
Thunderbird release notes for 128 esr have disappeared
by
EricB
2 days, 1 hour ago -
CISA mutes own website, shifts routine cyber alerts to X, RSS, email
by
Nibbled To Death By Ducks
2 days, 10 hours ago -
Apple releases 18.5
by
Susan Bradley
2 days, 5 hours ago -
Fedora Linux 40 will go end of life for updates and support on 2025-05-13.
by
Alex5723
2 days, 12 hours ago -
How a new type of AI is helping police skirt facial recognition bans
by
Alex5723
2 days, 12 hours ago -
Windows 7 ISO /Windows 10 ISO
by
ECWS
21 hours, 18 minutes ago -
No HP software folders
by
fpefpe
2 days, 20 hours ago -
Which antivirus apps and VPNs are the most secure in 2025?
by
B. Livingston
1 day, 17 hours 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.