Results 1 to 7 of 7

Thread: [RESOLVED] how to keep leading 0's

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2010
    Posts
    2

    Resolved [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
    Last edited by bananiel; Sep 3rd, 2010 at 04:01 AM.

  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    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
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  3. #3
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,354

    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

  4. #4

  5. #5
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,354

    Re: how to keep leading 0's

    Quote Originally Posted by FireXtol View Post
    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.
    Doctor Ed

  6. #6
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    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"))
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  7. #7

    Thread Starter
    New Member
    Join Date
    Sep 2010
    Posts
    2

    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 View Post
    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
  •  



Click Here to Expand Forum to Full Width