[RESOLVED] how to keep leading 0's
hi all,
i have been using the search function of this forum alot (THANKS!!)
but now im kinda stuck on something thats prob to easy to find on the forums :(
i got a text box that contains the value 001
but it could also contain a value like : 010 or 0234 or 12317398782435
i want to add 1 to the value the thing im trying now it this :
Me.Controls("Text" & e - 10).Text = Me.Controls("Text" & e - 10).Text + 1
this changes 001 in to 2
and not 002 as i would like.
any ideas are welcome :)
Re: how to keep leading 0's
Welcome to the forums.
Well, that can get kind of tricky.
Let me ask you this, what if you subtracted 1, would the result be 0, 00 or 000?
What if you subtracted 10, would result be -9 or -009?
Maybe you can give some more details scenarios?
Otherwise, if you are always adding, you might just want to keep track of how many leading zeroes...
Change Text1 to your textbox names
Code:
Dim nrLead0 As Integer, newVal As Double
newVal = Val(Text1.Text)
nrLead0 = Len(Text1.Text) - Len(CStr(newVal))
Text1.Text = String(nrLead0, "0") & newVal + 1
Above works only for non-negative values
Re: how to keep leading 0's
This is probably pretty close to what you want:
Code:
Text1.Text = "0012345678" ' Set this to any string of digits
Do While I < Len(Text1.Text)
If Mid$(Text1.Text, I + 1, 1) = "0" Then
I = I + 1
Else: Exit Do
End If
Loop
MsgBox String$(I, "0") & Format$(Val(Text1.Text) + 1, String$(Len(Text1.Text), "#"))
Is that close enough? (Also works only for non-negative values if leading zeroes exist. If a negative string, the value is correct but the leading zeroes are truncated.)
Re: how to keep leading 0's
You can also use Format().
Such as:
result = Format(2,"00#")
Re: how to keep leading 0's
Quote:
Originally Posted by
FireXtol
You can also use Format().
Such as:
result = Format(2,"00#")
I fear this does not work if the leading zeroes are not already there. This adds them automatically. :ehh:
That's why I wrote a loop to check for the presence of any leading zeroes in the string.
Re: how to keep leading 0's
Something like this:
Code:
Dim sValue As String
sValue = Trim(Me.Controls("Text" & e - 10).Text)
Me.Controls("Text" & e - 10).Text = Format(Val(sValue) + 1, String$(Len(sValue), "0"))
Re: how to keep leading 0's
thanks all for the fast and good replys,
i never tought what would happen if the value would become negative.
tho it should not be able to be negative (coz the code if for building a url) ill need to build some sort of error trapping for that
all the codes came down to the same solution (strip the leading 0's and add them again after adding +1) i used anhn's code coz it fits my way of coding abit better.
indeed adding a format to the value doesnt work coz the value changes on user input.
Quote:
Originally Posted by
anhn
Something like this:
Code:
Dim sValue As String
sValue = Trim(Me.Controls("Text" & e - 10).Text)
Me.Controls("Text" & e - 10).Text = Format(Val(sValue) + 1, String$(Len(sValue), "0"))
thanks all for the great help this one is solved :) :thumb::thumb::thumb: