#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2016
    Posts
    3
    Rep Power
    0

    VBScript: how to remove leading zeroes in counter


    I'm trying to make a VBScript produce a counter with this format: YY/nn (16/1 - 16/2).
    It has to handle numbers up to 4 characters (16/100, 16/1055).

    I have found this code, who does the job, but it displays leading zeroes (16/0001):

    Code:
    Dim yy, nn
    yy = Right(Year(Date()),2)
    If isnull(LastUsed) or (LastUsed="") Then
    	nn = 1
    ElseIf Left(LastUsed,2)=yy Then	
    	nn = CInt(Right(LastUsed,4)) + 1
    Else
    	nn = 1
    End If
    		
    Output = yy & "/" & Right("0000" & nn,4)

    Unfortunately, I have very little knowledge in this area, so I wonder I anyone here
    could be so kind to help me out in regard to get rid of leading zeroes in the counter?
  2. #2
  3. Lazy Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,263
    Rep Power
    9645
    Code:
    Right("0000" & nn,4)
    That looks relevant. Try something about that.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2016
    Posts
    3
    Rep Power
    0
    Originally Posted by requinix
    Code:
    Right("0000" & nn,4)
    That looks relevant. Try something about that.
    oh yes indeed. it doe's but removing the zeroes produced an error.

    Got a solution, from a form without sarcasm
    Code:
    Dim yy, nn
    
    yy = Right(Year(Date()), 2)
    
    If IsNull(LastUsed) Or (LastUsed = "") Then
       nn = 1
    ElseIf Left(LastUsed, 2) = yy Then      
       nn = CInt(Mid(LastUsed, 4)) + 1
    Else
       nn = 1
    End If
                
    Output = yy & "/" & nn
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2016
    Posts
    3
    Rep Power
    0
    Output = yy & "/" & nn didn't do it alone
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2010
    Posts
    125
    Rep Power
    160
    why do you do this
    Code:
    If IsNull(LastUsed) Or (LastUsed = "") Then
       nn = 1
    ElseIf Left(LastUsed, 2) = yy Then      
       nn = CInt(Mid(LastUsed, 4)) + 1
    Else
       nn = 1
    End If
    all you need do is increment the nn value

    here to help

IMN logo majestic logo threadwatch logo seochat tools logo