Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
Solved

# variable looses all the zeros in + calc

Posted on 2004-09-01
Medium Priority
213 Views
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)
0
Question by:jozne
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 2
• 2
• 2
• +2

LVL 8

Accepted Solution

ID: 11950366
Format(number, "000000")
0

LVL 10

Expert Comment

ID: 11950448
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

0

LVL 19

Expert Comment

ID: 11951144
Private Sub Command1_Click()
Var1 = "000001"
MsgBox Format(Var1 + 1, "000000")
End Sub
0

LVL 4

Expert Comment

ID: 11951153
Hi..
I certainly go with the format string however the correct syntax for me is

newvalue=format (number, "00000#")

VIkasumit
http://sumitonnet.20m.com

0

LVL 10

Expert Comment

ID: 11951296
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
0

LVL 4

Expert Comment

ID: 11952739
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
0

LVL 19

Expert Comment

ID: 11952756
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
0

Author Comment

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

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
###### Suggested Courses
Course of the Month9 days, 7 hours left to enroll