Link to home
Start Free TrialLog in
Avatar of jozne
jozne

asked on

variable looses all the zeros in + calc

ok I have one problem...
when I have number 000001 stored to variable, and I add 1 to it
(000001 + 1) I end up with 2, but I wanna have 000002. I know
that I can add those missing zeros with 00000 & 2 but I would like to
know if there is any way to not to loose those zeros in the calculation process.

This would help me a lot in what I have been coding
(my own coordinate system)
ASKER CERTIFIED SOLUTION
Avatar of mladenovicz
mladenovicz

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of anv
anv

the only way to do so is using string

let var1 stores the value 000001

then do following
dim var1Str as string, lastDigit as integer
dim otherDigits as string

var1str=cstr(var1)
lastDigit = cint(left(var1str,1)) 'will store 1

otherDigits = right(var1str,len(var1str)-1 'will store 00000

lastDigit=lastDigit +1 'will have 2 now

var1 = otherDigits + lastdigit

'now var1 will have 000002


Private Sub Command1_Click()
Var1 = "000001"
MsgBox Format(Var1 + 1, "000000")
End Sub
Hi..
I certainly go with the format string however the correct syntax for me is

newvalue=format (number, "00000#")

VIkasumit
http://sumitonnet.20m.com

Shauli's code will work but if u store the data in an integer variable the value will again go as  2 removing leading 0's

yes for storing as a string it will work...
like

Private Sub Command1_Click()
dim str1 as string
Var1 = "000001"
str1 =  Format(Var1 + 1, "00000#")
End Sub
Hi..

Yes He is right but Again when ever you need You can use Format Like while displaying, Storing or even manupulating. And If you just want to use it as a ID field than better use the string datatype once generated u doesn't need to change it....

VIkasumit
Var1 can be stored as String or as Varinat, as in:

Private Sub Command1_Click()
Dim Var1 As Variant, Var2 As Variant
Var1 = "000001"
Var2 = Format(Var1 + 1, "000000")
MsgBox Var2
End Sub

Either way will work. And there is no different if you go 00000# or 000000. The result is the same.

S
Avatar of jozne

ASKER

Thank you guys for your answers. My coordination system is up and running :)