|
-
Sep 2nd, 2010, 06:54 PM
#1
Thread Starter
New Member
-
Sep 2nd, 2010, 07:48 PM
#2
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
-
Sep 2nd, 2010, 07:50 PM
#3
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.)
Last edited by Code Doc; Sep 2nd, 2010 at 07:55 PM.
Doctor Ed
-
Sep 2nd, 2010, 07:53 PM
#4
Re: how to keep leading 0's
You can also use Format().
Such as:
result = Format(2,"00#")
Software I use and highly recommend: Opera, Miranda IM, Peerblock, Winamp, Unlocker Assistant, JoyToKey, Virtual CloneDrive, Secunia PSI, ExplorerXP, GOM Player, Real Alternative, Quicktime Alternative,Sumatra PDF, and non-freeware: Photoshop and VB6( ).
My codebank: AllRGB, Rounded Rectangle(math), Binary Server, Buddy Paint, LoadPictureGDI+, System GUID/Volume Serial, HexToAsc, List all processes and their paths, quasiString matching
Strings(search, extraction, retrieval etc): Retrieve BBCode Link from HTML, RemoveBetween ()'s, strFindBetween(str1,str2), Insert text in HTML, HTML - GetSpanByID
-
Sep 2nd, 2010, 08:02 PM
#5
Re: how to keep leading 0's
 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. 
That's why I wrote a loop to check for the presence of any leading zeroes in the string.
-
Sep 2nd, 2010, 09:23 PM
#6
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"))
-
Sep 3rd, 2010, 04:00 AM
#7
Thread Starter
New Member
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.
 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  
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|