473,386 Members | 1,812 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,386 software developers and data experts.

Len() function for number not returning expected results

Seth Schrock
2,965 Expert 2GB
I just used the Len() function on a number and it returned some wacky result. For example, when I did Len(1), it returned 4. If I put quotes around the number, then it returned correctly. Any ideas?
Jan 15 '16 #1
10 4039
Luk3r
300 256MB
The len() function returns one of two values: 1) The length of a string, or 2) the number of bytes necessary to read the variable.

Expand|Select|Wrap|Line Numbers
  1. Dim testString As String = "1"
  2. Dim testLen As Integer = Len(testString)
Returns 1

Expand|Select|Wrap|Line Numbers
  1. Dim testLen As Integer = Len(1)
Returns 4
Jan 15 '16 #2
Seth Schrock
2,965 Expert 2GB
I knew that the LenB() function returned the number of bytes, but I didn't know that the Len() function would do the same on numbers. Based on different searches on Google, it seems like others have been able to use the Len() function in queries to be able to get the number of digits in a number, but maybe that is because it is SQL and not VBA, which is what I'm using.
Jan 15 '16 #3
Luk3r
300 256MB
As far as I know in VB/VBA you cannot use the Len() function to get the length of an integer. You would have to convert the integer to a string.
Jan 15 '16 #4
jforbes
1,107 Expert 1GB
I think the automatic Type conversion is getting you.

A String is a Reference Type and an Integer is a Value Type. The Len() function takes a String as it's type, so the Int needs to be converted to a String, which means a String Object has to be created. I think the constructor for this includes the sign in the resulting String. So I would expect you to get a String of " 1" for the Integer of 1, and "-1" for -1. I would also expect the result of Len(1) and Len(-1) to be 2, so I'm not sure why you are getting 4. I know that's not an answer, but hopefully it sheds some kind of light. If you want a headache, research boxing and unboxing. =)

You could try:
Expand|Select|Wrap|Line Numbers
  1. Len(Trim(intVariable))
Which is pretty much what Luk3r has recommended.
Jan 15 '16 #5
Seth Schrock
2,965 Expert 2GB
I had originally done it by converting it to a string, but then I thought I could bypass that step, but evidently not. Oh well. Thanks for your help.
Jan 15 '16 #6
zmbd
5,501 Expert Mod 4TB
Seth,
Would you mind posting the code you are using?

Luk3r,
Your code as posted, although valid in VB, is invalid in VBA
Unfortunately, VBA doesn't allow this syntax... would be nice, as there are many times I want to set the default value of the variable and have to do so in two steps.
Jan 16 '16 #7
Seth Schrock
2,965 Expert 2GB
I have already erased my code, so I will have to recreate it here with some air code.
Expand|Select|Wrap|Line Numbers
  1. Private Function ConvertToDecimal(lngBinary As Long) As Long
  2. 'Dim strNumber As String
  3. Dim i As Integer
  4. Dim lngAccumulator As Long
  5. Dim n As Integer
  6. Dim s As String
  7. Dim p As Integer
  8.  
  9. 'strNumber = Format(lngBinary, "00000000")
  10. p = 1
  11. For i = Len(lngBinary) To 1 Step -1
  12.     s = Mid(strNumber, p, 1)
  13.     If s = "1" Then
  14.         n = CInt(i) - 1
  15.         lngAccumulator = lngAccumulator + 2 ^ n
  16.     End If
  17.     p = p + 1
  18. Next
  19.  
  20. ConvertToDecimal = lngAccumulator
  21.  
  22. End Function
It would have been something like this. So if I passed the number 1 to the function, the Len(lngBinary) would return 4 and not 1. Passing 5 produced the same result. If I passed 110, it returned 12.
Jan 17 '16 #8
NeoPa
32,556 Expert Mod 16PB
This excerpt from the help page for Len() explains it all quite clearly I think :
Len() Help:

Len Function

Returns a Long containing the number of characters in a string or the number of bytes required to store a variable.

Syntax

Len(string | varname)

The Len function syntax has these parts:
Expand|Select|Wrap|Line Numbers
  1.   Part    Description 
  2. string    Any valid string expression.
  3.           If string contains Null, Null is returned. 
  4. varname   Any valid variable name.
  5.           If varname contains Null, Null is returned.
  6.           If varname is a VariantLen treats it the same as a String and always returns the number of characters it contains.
Jan 17 '16 #9
zmbd
5,501 Expert Mod 4TB
NeoPa, I do believe that you've hit the nail on the head so to speak :0)


I suspected this was the case; however, I wanted to be sure I hadn't missed anything
Jan 17 '16 #10
NeoPa
32,556 Expert Mod 16PB
Thank goodness MS are making such good progress bringing the Help System back to a usable state.

I'm still using 2003 so that's mostly easy for me. I understand many people nowadays tend to avoid it and use alternative ways of finding things out, but that's changing.

The Help System should be your friend, and it can be again in Access.
Jan 18 '16 #11

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

Similar topics

2
by: jeff | last post by:
I have a php file that contains a couple of arrays used for state/country pull-down lists. I have two global arrays and an accessor method for each. I have some simple logging methods, so I know a...
7
by: Dr John Stockton | last post by:
What are the best ways of returning multiple results from a subroutine ? I've been using ... return } which is inelegant. I'm used to Pascal's procedure X(const A, B : integer; var C, D :...
11
by: PC Datasheet | last post by:
How do I get the Len function to count a space at the end of a string? For example, if I enter AB(space), how do I get Len to return 3 instead of 2? Thanks, Steve
10
by: Sean | last post by:
I have a struct that I wrote to test a protocol. The idea I had was to just declare the elements of the struct in the order in which they are sent and received as defined by the protocol. ...
10
by: TC | last post by:
select 'y' value from sysibm.sysdummy1 where 1=1 union select 'x' value from sysibm.sysdummy1 where 1=2 Why is it that this will not return 'y'. It seem to return nothing unless both sides of...
0
by: k1ngdrew | last post by:
I am trying to use mshtml to parse a web page without using IE. I create the HTMLDocument object, and then assign to it the output value from createDocumentFromUrl(). I know that this is at least...
1
by: thepresidentis | last post by:
here is my problem, i have a website that compiles concert listings for users to search for shows in their local area, i ran into a problem with returning search results due to an improper format...
7
by: daveeboi | last post by:
..I have been strugling to get this part of my site working correctly even though I can't see anything wrong with my code. I am trying to search a database and display paged results. But everytime...
1
by: jonnyothan | last post by:
The following code isn't producing expected results: class Selectable { bool IsSelected() const; }; class Unit : public Selectable { };
8
by: neobrainless | last post by:
I *think* this is the right section as it's a VBA issue, sorry if it's not! So I'm tryign to get an excel file to open with all but certain worksheets hidden. I have found some code to make ONE...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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,...

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.