Results 1 to 11 of 11

Thread: [RESOLVED] Best way to increment numbers containing a string

  1. #1

    Thread Starter
    PowerPoster isnoend07's Avatar
    Join Date
    Feb 2007
    Posts
    3,237

    Resolved [RESOLVED] Best way to increment numbers containing a string

    I am creating invoice, contract numbers etc to an ini file. When opening, increase the number number by 1.
    Like PO1000
    Inv1000
    Chg1000
    what would be the easiest way to do this?
    The letters will always be on the left part of the string
    Waiting for a full featured smart phone with out marrying a provider
    Go Android
    Go raiders

  2. #2
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: Best way to increment numbers containing a string

    What is the expected (agreed) range of numbers top be used?

    For example: 0000 to 1000, or 1000 to 9999999

  3. #3

    Thread Starter
    PowerPoster isnoend07's Avatar
    Join Date
    Feb 2007
    Posts
    3,237

    Re: Best way to increment numbers containing a string

    Quote Originally Posted by Bruce Fox
    What is the expected (agreed) range of numbers top be used?

    For example: 0000 to 1000, or 1000 to 9999999
    They are going to start at 1,000 and not go past 100,000
    Waiting for a full featured smart phone with out marrying a provider
    Go Android
    Go raiders

  4. #4

    Thread Starter
    PowerPoster isnoend07's Avatar
    Join Date
    Feb 2007
    Posts
    3,237

    Re: Best way to increment numbers containing a string

    Thanks i got it

    Private Function OnlyNumbers(ByVal Data As String) As String
    Dim I As Integer
    For I = 1 To Len(Data)
    If InStr("0123456789,", Mid(Data, I, 1)) > 0 Then
    OnlyNumbers = OnlyNumbers & Mid(Data, I, 1)
    End If
    Next
    End Function
    Waiting for a full featured smart phone with out marrying a provider
    Go Android
    Go raiders

  5. #5
    PowerPoster CDRIVE's Avatar
    Join Date
    Jul 2007
    Posts
    2,620

    Re: [RESOLVED] Best way to increment numbers containing a string

    Quote Originally Posted by isnoend07
    I am creating invoice, contract numbers etc to an ini file. When opening, increase the number number by 1.
    Like PO1000
    Inv1000
    Chg1000
    what would be the easiest way to do this?
    The letters will always be on the left part of the string
    Well, I don't know if this is the best way but it is one way. Create a text file in the VB98 dir and type 1000 in it before saving it. The Invoice number will increment by 1 each time you run this app.
    Code:
    Option Explicit
    
    
    Private Sub Form_Load()
       Dim lngInvNum As Long
       Dim strInvNum As String
       
       Open "MyInvNum.txt" For Input As #1
          Input #1, lngInvNum
          lngInvNum = lngInvNum + 1
          strInvNum = "Invoice " & lngInvNum
          Label1.Caption = strInvNum             ' Display the Invoice number
       Close #1
    
       Open "MyInvNum.txt" For Output As #1
          Print #1, lngInvNum                    ' Store the new number
       Close #1
    End Sub
    Last edited by CDRIVE; Nov 26th, 2008 at 11:49 PM. Reason: change Integer to Long
    <--- Did someone help you? Please rate their post. The little green squares make us feel really smart!
    If topic has been resolved, please pull down the Thread Tools & mark it Resolved.


    Is VB consuming your life, and is that a bad thing??

  6. #6
    PowerPoster CDRIVE's Avatar
    Join Date
    Jul 2007
    Posts
    2,620

    Re: [RESOLVED] Best way to increment numbers containing a string

    I have a suspicion that I misunderstood the question. Sorry!
    <--- Did someone help you? Please rate their post. The little green squares make us feel really smart!
    If topic has been resolved, please pull down the Thread Tools & mark it Resolved.


    Is VB consuming your life, and is that a bad thing??

  7. #7

    Thread Starter
    PowerPoster isnoend07's Avatar
    Join Date
    Feb 2007
    Posts
    3,237

    Re: [RESOLVED] Best way to increment numbers containing a string

    Quote Originally Posted by CDRIVE
    I have a suspicion that I misunderstood the question. Sorry!
    No prob, i got it handled, thanks
    Waiting for a full featured smart phone with out marrying a provider
    Go Android
    Go raiders

  8. #8
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: Best way to increment numbers containing a string

    Quote Originally Posted by isnoend07
    Thanks i got it

    Private Function OnlyNumbers(ByVal Data As String) As String
    Dim I As Integer
    For I = 1 To Len(Data)
    If InStr("0123456789,", Mid(Data, I, 1)) > 0 Then
    OnlyNumbers = OnlyNumbers & Mid(Data, I, 1)
    End If
    Next
    End Function
    For what it's worth, that is inefficent.

    You know that at least the last 6 locations could be numbers, so account for that in the loop so you are not iterating past each and every location.

    One way would be to start the loop 6 positions back into the string (as the max will be 6 digits, with 4 being the minimum).

    Another way with a short loop (two times) would be to take the last six positions (could all be numbers, OR may contain at max two characters) and loop past those two left locations and check for numericy.
    (IsNumeric)

    And another, to extract the text and numbers, start the loop from the left until you IsNumeric, then use that position to extract the Right$() (maybe Mid())portion and Left$() to get the textual component << more than likely the faster of the three.
    Last edited by Bruce Fox; Nov 27th, 2008 at 02:52 AM.

  9. #9

    Thread Starter
    PowerPoster isnoend07's Avatar
    Join Date
    Feb 2007
    Posts
    3,237

    Re: Best way to increment numbers containing a string

    Quote Originally Posted by Bruce Fox
    For what it's worth, that is inefficent.

    You know that at least the last 6 locations could be numbers, so account for that in the loop so you are not iterating past each and every location.

    One way would be to start the loop 6 positions back into the string (as the max will be 6 digits, with 4 being the minimum).

    Another way with a short loop (two times) would be to take the last six positions (could all be numbers, OR may contain at max two characters) and loop past those two left locations and check for numericy.
    (IsNumeric)

    And another, to extract the text and numbers, start the loop from the left until you IsNumeric, then use that position to extract the Right$() (maybe Mid())portion and Left$() to get the textual component << more than likely the faster of the three.
    Thanks for your reply. Actually I have found I don't need to do anything to the string as the letters are for display only.
    Waiting for a full featured smart phone with out marrying a provider
    Go Android
    Go raiders

  10. #10
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: [RESOLVED] Best way to increment numbers containing a string

    Why don't you simply save a line with just the number and then append that to PO, Inv, Chg as you need?

  11. #11
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: [RESOLVED] Best way to increment numbers containing a string

    NO Loop Option

    Actually, if you did have the combination, using InStr() for the first occurance of an integer would do. Using that position (+1) you could extract from there til the end and you would have the Numbers - NO loops required in this case.

    Anyway glad it's working the way you need

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