How to keep Leading Zero's ?

pds8475

Registered User.
Local time
Today, 00:48
Joined
Apr 26, 2015
Messages
84
Hi
Firstly sorry if this is not the right section for this.

I have a String variable that gets it's value from a textbox.

This value can be anywhere from
0000001 to 9999999
Or
000001 to 999999
Or
00001 to 99999

I need to be able to add one to the value on each iteration of a loop.
I.e.
00001 loops 10 times= 00010

but no matter what I have tried the result is
10
instead of
00010

Is there a way that I can preserve the leading zero's?

any help will be much appreciated.
 
convert to a numberr and just format it to have the leading zeros - in the format property put 000000 for as many characters your require
 
Last edited:
The problem was that the format changed in length. But I have solved it.

I used

Code:
 Public Function CountStrOcc(strFind As String) As Long
CountStrOcc = Len([BarTxt]) - Len(CStr(Val([BarTxt])))
End Function

to count the leading zeros

and then just used an IF ElseIF statement to create a string variable of the right amount of zero's. Then just stuck the initial string to this.
 
note that you can do this by simply doing this

Code:
 Public Function stringwithleadingzeroes(thenumber as long, stringlength as long) as string
 stringwithleadingzeroes= right("00000000000" & cstr(thenumber),stringlength)
 end function
 
Last edited:
Public Function fnLeadZeroPlusOne(Byval strSource As String, Optional Byval strFormat As String = "000000")
fnLeadZeroPlusOne = Format( Val(strSource) + 1, strFormat)
End Function
 
Decided to use yours. I created a function for all possible string lengths. Then set a variable to a count of the length of the string in the textbox. Then used that number to call the appropriate function.
 

Users who are viewing this thread

Back
Top Bottom