Solved

variable looses all the zeros in + calc

Posted on 2004-09-01
8
210 Views
Last Modified: 2010-05-02
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
Comment
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
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 8

Accepted Solution

by:
mladenovicz earned 125 total points
ID: 11950366
Format(number, "000000")
0
 
LVL 10

Expert Comment

by:anv
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

by:Shauli
ID: 11951144
Private Sub Command1_Click()
Var1 = "000001"
MsgBox Format(Var1 + 1, "000000")
End Sub
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 4

Expert Comment

by:VIkasumit
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

by:anv
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

by:VIkasumit
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

by:Shauli
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

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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

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 …
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question