Question related to a double quotation in Len() function (1 Viewer)

Melfaramawi

New member
Local time
Today, 16:00
Joined
Jun 1, 2022
Messages
6
I have a question related to this code please : Len(“”, me.searchbox), what does it mean. I understand all the components of the code but I am trying to put all the pieces together to interpret it correctly. It is a part of the following code , if Len (“”, me.searchbox) = 0 then do something. Why we need double quotation in the code, why not just if len(me.txtsearch) = 0 then do something. Thanks in advance for any explanation.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 14:00
Joined
Oct 29, 2018
Messages
21,521
Hi. Welcome to AWF!

Are you sure the code was actually
Code:
Len("", Me.Searchbox) = 0
and not
Code:
Len("" & Me.Searchbox) = 0
?
 

Melfaramawi

New member
Local time
Today, 16:00
Joined
Jun 1, 2022
Messages
6
Yes I am, the code is working fine but I needed to understand what it means
 

Melfaramawi

New member
Local time
Today, 16:00
Joined
Jun 1, 2022
Messages
6
Hi. Welcome to AWF!

Are you sure the code was actually
Code:
Len("", Me.Searchbox) = 0
and not
Code:
Len("" & Me.Searchbox) = 0
?
Sorry I have made a mistake. You are right, it is the second one ,Len (“”& me.searchbox) = 0
 

Gasman

Enthusiastic Amateur
Local time
Today, 22:00
Joined
Sep 21, 2011
Messages
14,400
Yes I am, the code is working fine but I needed to understand what it means
That makes a refreshing change. :)

I believe it is to account for the item appended to "" being Null. If you tested just the control by itself, you could get 'Invalid use of Null' error

I have seen it used mostly the other way and used it this way myself, but that is just my preference.

IF Len(ControlName & "") = 0 Then
 

theDBguy

I’m here to help
Staff member
Local time
Today, 14:00
Joined
Oct 29, 2018
Messages
21,521
Sorry I have made a mistake. You are right, it is the second one ,Len (“”& me.searchbox) = 0
Okay, I thought so. I looked at the definition of the Len() function, and it only accepts one argument - not two.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 16:00
Joined
Feb 28, 2001
Messages
27,285
Let's do it from a different perspective. You asked about

Code:
If Len("" & Me.searchbox) = 0

You could get generally the same kind of thing with

Code:
If NZ( Me.searchbox, "" ) = ""

The "" syntax in VBA is what is called a "zero-length string" (ZLS) or an "empty string." A null can be concatenated (using &) to any string and will have no effect on it. The length of a zero-length string is ... well... zero. So the first example EITHER adds an empty string to the front of Me.searchbox or, if Me.searchbox is NULL, assures that SOMETHING - the ZLS - is there to be measured. Because If Me.searchbox is null then you cannot measure the length of the string because there isn't one.

The second option uses the NZ function to reach a similar result. If Me.searchbox IS null, you get a ZLS. If not, you get whatever was in Me.searchbox.

In both cases, if Me.searchbox is null, the test doesn't fail. And if Me.searchbox is NOT null, again the test doesn't fail - but it gives a slightly different result. In both cases, though, the "TRUE" result would come about if Me.searchbox WAS null.
 

Users who are viewing this thread

Top Bottom