|
-
Oct 2nd, 2000, 03:44 PM
#1
Thread Starter
Lively Member
Hello ,
Can someone tell me how can i add 0 to the left side of a string if it has one char/number only!
if it has 2 char/num i want it to stay the same?
example : "6" to "06"
"A" to "0A"
but when it is "7A" or 2 other char/num i want it to stay the same.
(the format("string","00") - only works on number!)
thanks in advance ([email protected])
-
Oct 2nd, 2000, 03:47 PM
#2
str = iif((len(str)=1),"0" & str , str)
-
Oct 2nd, 2000, 04:38 PM
#3
More efficient:
Code:
sString = Right("00" & sValue, 2)
-
Oct 2nd, 2000, 04:51 PM
#4
I must prove you wrong on that one Aaron.
example : "6" to "06"
"A" to "0A"
but when it is "7A" or 2 other char/num i want it to stay the same.
If you use your code, say like:
Code:
sValue = "60A"
sString = Right("00" & sValue, 2)
Debug.Print sString 'returns 0A, where's the 6?
Now, if you use the code sarun gave, like:
Code:
sValue = "60A"
sString = IIf((Len(sValue) = 1), "0" & sValue, sValue)
Debug.Print sString 'returns 60A
Understand? So your code is not more efficient.
Sorry to prove you wrong .
-
Oct 2nd, 2000, 04:56 PM
#5
Moyalt did say though:
if it has 2 char/num i want it to stay the same?
Indicating that the value would never be more than 2 digits.
-
Oct 2nd, 2000, 05:01 PM
#6
_______
<?>
Aaron Wrong? Not likely. 
"A myth is not the succession of individual images,
but an integerated meaningful entity,
reflecting a distinct aspect of the real world."
___ Adolf Jensen
-
Oct 2nd, 2000, 05:06 PM
#7
Hyperactive Member
I think you will find this works in every case.
Code:
'Assume x holds your 1 or two character string
Debug.Print Iif(Len(x)<2 ,Format(x,"!0&"), x)
Cheers
P.S. I see that in the time I wrote this a debate has started Cool
-
Oct 2nd, 2000, 05:09 PM
#8
I guess you are right Aaron, the Right function is faster than the IIf function, at least from what I've heard. But, just keep in mind, that if the string is more than 2 digits, than you can use the first code. Or just change Aaron's code to 3 instead of 2.
Code:
sString = Right("00" & sValue, 3)
-
Oct 2nd, 2000, 05:18 PM
#9
Hyperactive Member
Fuel to the Fire
Matthew,
Here is a case for you 
sValue = "60A"
sString = IIf((Len(sValue) = 1), "0" & sValue, sValue)
Debug.Print sString 'returns 60A
What if sValue is "". You see, in your previous post, you asked "what if there were three characters"...so it's only fair to scrutinize your example with the counter case of 0 chars 
And I know it was sarun's example, but since you were having a bit of fun poking holes I thought you'd appreciate being poked back...All in fun of course 
Aren't I a bas***rd 
Cheers
P.S I like the simplicity of Aaron's case over my own as well
-
Oct 2nd, 2000, 05:28 PM
#10
Q: What if sValue is "".
Code:
sValue = ""
If sValue = "" Then Msgbox "Enter a Value!": Exit Sub
sString = IIf((Len(sValue) = 1), "0" & sValue, sValue)
Debug.Print sString
Quit poking at me! 
Sorry Aaron .
-
Oct 2nd, 2000, 05:52 PM
#11
<code>
if len(str) = 2 then str = "0" & str
<code>
why that would not work?
-
Oct 2nd, 2000, 05:53 PM
#12
if len(str)= 1 then str = "0" & str
i wanted to say
-
Oct 2nd, 2000, 06:05 PM
#13
_______
"A myth is not the succession of individual images,
but an integerated meaningful entity,
reflecting a distinct aspect of the real world."
___ Adolf Jensen
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|