Home All Groups Group Topic Archive Search About

create reference number based on old one...

Author
17 May 2009 10:46 AM
Co
Hi All,
I'm trying to create a function that will create a "reference number"
which is one higher then the latest one added.
The reference number consist of "2009000100"
which is the year (2009) followed by a 6 figur digit "000100"
The code must create the new reference number as : "2009000101"

'oldKenmerk = latest used reference number
Dim i As Integer
        Dim numberPart As String
        numberPart = Mid(oldKenmerk, 5, 6)
        Dim sNumPart As String = ""

        For i = 1 To 6
            If Mid(numberPart, i, 1) = "0" Then
                sNumPart = sNumPart & Mid(numberPart, i, 1)
            Else
                Exit For
            End If
        Next
        numberPart = Replace(numberPart, sNumPart, "")

        If Mid(oldKenmerk, 1, 4) = Year(Today) Then
            newKenmerk = Year(Today) & sNumPart & CInt(numberPart) + 1
        Else
            'the file is the first of the year
            newKenmerk = Year(Today) & "000100"
        End If

I don't think my code will work when you go from for example
"2009000199" to "2009000200"

Regards
Marco

Author
17 May 2009 11:41 AM
Armin Zingler
Co wrote:
Show quoteHide quote
> Hi All,
> I'm trying to create a function that will create a "reference number"
> which is one higher then the latest one added.
> The reference number consist of "2009000100"
> which is the year (2009) followed by a 6 figur digit "000100"
> The code must create the new reference number as : "2009000101"
>
> 'oldKenmerk = latest used reference number
> Dim i As Integer
>        Dim numberPart As String
>        numberPart = Mid(oldKenmerk, 5, 6)
>        Dim sNumPart As String = ""
>
>        For i = 1 To 6
>            If Mid(numberPart, i, 1) = "0" Then
>                sNumPart = sNumPart & Mid(numberPart, i, 1)
>            Else
>                Exit For
>            End If
>        Next
>        numberPart = Replace(numberPart, sNumPart, "")
>
>        If Mid(oldKenmerk, 1, 4) = Year(Today) Then
>            newKenmerk = Year(Today) & sNumPart & CInt(numberPart) + 1
>        Else
>            'the file is the first of the year
>            newKenmerk = Year(Today) & "000100"
>        End If
>
> I don't think my code will work when you go from for example
> "2009000199" to "2009000200"

Why do you use "Today"? Above you write you want to use the year in the
String. What should happen with "2009999999"?

If you assume always a 4 digit year and ignore an overflow of the counter,
the simplest solution is

    newnumber = (clng(oldnumber) + 1).tostring


Armin
Author
17 May 2009 11:51 AM
Co
Show quote Hide quote
On 17 mei, 13:41, "Armin Zingler" <az.nos***@freenet.de> wrote:
> Co wrote:
> > Hi All,
> > I'm trying to create a function that will create a "reference number"
> > which is one higher then the latest one added.
> > The reference number consist of "2009000100"
> > which is the year (2009) followed by a 6 figur digit "000100"
> > The code must create the new reference number as : "2009000101"
>
> > 'oldKenmerk = latest used reference number
> > Dim i As Integer
> >        Dim numberPart As String
> >        numberPart = Mid(oldKenmerk, 5, 6)
> >        Dim sNumPart As String = ""
>
> >        For i = 1 To 6
> >            If Mid(numberPart, i, 1) = "0" Then
> >                sNumPart = sNumPart & Mid(numberPart, i, 1)
> >            Else
> >                Exit For
> >            End If
> >        Next
> >        numberPart = Replace(numberPart, sNumPart, "")
>
> >        If Mid(oldKenmerk, 1, 4) = Year(Today) Then
> >            newKenmerk = Year(Today) & sNumPart & CInt(numberPart) + 1
> >        Else
> >            'the file is the first of the year
> >            newKenmerk = Year(Today) & "000100"
> >        End If
>
> > I don't think my code will work when you go from for example
> > "2009000199" to "2009000200"
>
> Why do you use "Today"? Above you write you want to use the year in the
> String. What should happen with "2009999999"?
>
> If you assume always a 4 digit year and ignore an overflow of the counter,
> the simplest solution is
>
>     newnumber = (clng(oldnumber) + 1).tostring
>
> Armin

Armin,

your solution leaves me with "2009145" instead of "2009000145"

MArco
Author
17 May 2009 1:31 PM
Armin Zingler
Co wrote:
>> newnumber = (clng(oldnumber) + 1).tostring
>>
>
> your solution leaves me with "2009145" instead of "2009000145"

I correctly get "2009000145" here.



Armin
Author
17 May 2009 3:20 PM
Family Tree Mike
Show quote Hide quote
"Armin Zingler" <az.nospam@freenet.de> wrote in message
news:OojrYRv1JHA.1712@TK2MSFTNGP03.phx.gbl...
> Co wrote:
>>> newnumber = (clng(oldnumber) + 1).tostring
>>>
>>
>> your solution leaves me with "2009145" instead of "2009000145"
>
> I correctly get "2009000145" here.
>
>
>
> Armin


Wouldn't you need a formatting string to get that?  Your post did not
include any.

--
Mike
Author
17 May 2009 4:32 PM
Armin Zingler
Family Tree Mike wrote:
Show quoteHide quote
> "Armin Zingler" <az.nospam@freenet.de> wrote in message
> news:OojrYRv1JHA.1712@TK2MSFTNGP03.phx.gbl...
>> Co wrote:
>>>> newnumber = (clng(oldnumber) + 1).tostring
>>>>
>>>
>>> your solution leaves me with "2009145" instead of "2009000145"
>>
>> I correctly get "2009000145" here.
>>
>
>
> Wouldn't you need a formatting string to get that?  Your post did not
> include any.

Did you try it? I don't know how "000" in the _middle_ can be left out
without formatting. Only leading zeros would need a format string.

      Dim newnumber, oldnumber As String

      oldnumber = "2009000144"
      newnumber = (CLng(oldnumber) + 1).ToString
      MsgBox(newnumber)     '=> "2009000145"

Maybe I missed the point. I read the OP's first post again but I'm still
looking for the problem. He clearly said he has a 4-digit year followed by a
6-digit number in a string. The number is to be incremented. Nowhere I read
that the current year should be taken into account.


Armin
Author
17 May 2009 6:34 PM
Cor Ligthert[MVP]
Hi Armin,

May be his Dunglish (Dutch English) is more easier for me then for you but
the OP wrote.

"which is the year (2009) followed by a 6 figure digit "000100"

I understood from it a year in a string plus a 6 figur digit in a string.

As I write this I know that you are more strict then me and you are right it
is not exactly written, but this kind of sentenced do you not understand. I
know.

(I can see from words as "newKenmerk" that Co is a Dutch or maybe Afrikaans
speaker, "newKenmerk"  is English combined with Dutch which even is not
Dunglish.)

Cor
Author
17 May 2009 7:44 PM
Armin Zingler
Cor Ligthert[MVP] wrote:
> Hi Armin,
>
> May be his Dunglish (Dutch English) is more easier for me then for
> you but the OP wrote.
>
> "which is the year (2009) followed by a 6 figure digit "000100"
>
> I understood from it a year in a string plus a 6 figur digit in a
> string.

That's what I understand, too.

> As I write this I know that you are more strict then me and you are
> right it is not exactly written, but this kind of sentenced do you
> not understand. I know.

?? I still don't understand what's wrong with my solution.


<quote>
I'm trying to create a function that will create a "reference number"
which is one higher then the latest one added.
The reference number consist of "2009000100"
which is the year (2009) followed by a 6 figur digit "000100"
The code must create the new reference number as : "2009000101"
</quote>

His reference number is "2009000100" which is a 4 digit year followed by a 6
digit number. The new reference number is to be "2009000101". I can read it
another dozend times, but that's exactly what my code does.
Which part do I misunderstand? I don't get it.


Armin
Author
17 May 2009 9:30 PM
Family Tree Mike
Show quote Hide quote
"Armin Zingler" <az.nospam@freenet.de> wrote in message
news:%23QTT4gy1JHA.1712@TK2MSFTNGP03.phx.gbl...
> Cor Ligthert[MVP] wrote:
>> Hi Armin,
>>
>> May be his Dunglish (Dutch English) is more easier for me then for
>> you but the OP wrote.
>>
>> "which is the year (2009) followed by a 6 figure digit "000100"
>>
>> I understood from it a year in a string plus a 6 figur digit in a
>> string.
>
> That's what I understand, too.
>
>> As I write this I know that you are more strict then me and you are
>> right it is not exactly written, but this kind of sentenced do you
>> not understand. I know.
>
> ?? I still don't understand what's wrong with my solution.
>
>
> <quote>
> I'm trying to create a function that will create a "reference number"
> which is one higher then the latest one added.
> The reference number consist of "2009000100"
> which is the year (2009) followed by a 6 figur digit "000100"
> The code must create the new reference number as : "2009000101"
> </quote>
>
> His reference number is "2009000100" which is a 4 digit year followed by a
> 6
> digit number. The new reference number is to be "2009000101". I can read
> it
> another dozend times, but that's exactly what my code does.
> Which part do I misunderstand? I don't get it.
>
>
> Armin
>


I interpreted his desire, that, if the code ran on 31 December 2009 beyond
midnight, that the code needed to handle the change of year.  I now
understand your interpretation.  I had assumed in your post that you were
only describing the index part, not including the first four digits.  Sorry
about my confusion.

--
Mike
Author
17 May 2009 9:51 PM
Armin Zingler
Family Tree Mike wrote:
Show quoteHide quote
>> <quote>
>> I'm trying to create a function that will create a "reference number"
>> which is one higher then the latest one added.
>> The reference number consist of "2009000100"
>> which is the year (2009) followed by a 6 figur digit "000100"
>> The code must create the new reference number as : "2009000101"
>> </quote>
>>
>> His reference number is "2009000100" which is a 4 digit year
>> followed by a 6
>> digit number. The new reference number is to be "2009000101". I can
>> read it
>> another dozend times, but that's exactly what my code does.
>> Which part do I misunderstand? I don't get it.
>>
>>
>> Armin
>>
>
>
> I interpreted his desire, that, if the code ran on 31 December 2009
> beyond midnight, that the code needed to handle the change of year. I now
> understand your interpretation.  I had assumed in your post
> that you were only describing the index part, not including the first
> four digits.  Sorry about my confusion.

Yes, I left the year unchanged because he didn't state that it also has to
be changed. I also asked why he uses "Today" in his code - unanswered til
now. Maybe that's the missing part. :)


Armin
Author
17 May 2009 8:13 PM
Cor Ligthert[MVP]
Armin,

That is where I wrote you are more strict then me, I was reading between the
lines that the number exist from a year plus a serial number.

That means that he has to start every year again. I took for the year the
Now.year in my example, however that can be of course every variable from
four digits separated from the serial number.

But strictly is your example is in my opinion a correct answer on the
question from the Op and in nothing wrong.

Cor
Author
17 May 2009 11:48 AM
Family Tree Mike
Show quote Hide quote
"Co" <vonclausow***@gmail.com> wrote in message
news:72cfbffb-a039-4c56-9933-fc1893851acc@g19g2000vbi.googlegroups.com...
> Hi All,
> I'm trying to create a function that will create a "reference number"
> which is one higher then the latest one added.
> The reference number consist of "2009000100"
> which is the year (2009) followed by a 6 figur digit "000100"
> The code must create the new reference number as : "2009000101"
>
> 'oldKenmerk = latest used reference number
> Dim i As Integer
>        Dim numberPart As String
>        numberPart = Mid(oldKenmerk, 5, 6)
>        Dim sNumPart As String = ""
>
>        For i = 1 To 6
>            If Mid(numberPart, i, 1) = "0" Then
>                sNumPart = sNumPart & Mid(numberPart, i, 1)
>            Else
>                Exit For
>            End If
>        Next
>        numberPart = Replace(numberPart, sNumPart, "")
>
>        If Mid(oldKenmerk, 1, 4) = Year(Today) Then
>            newKenmerk = Year(Today) & sNumPart & CInt(numberPart) + 1
>        Else
>            'the file is the first of the year
>            newKenmerk = Year(Today) & "000100"
>        End If
>
> I don't think my code will work when you go from for example
> "2009000199" to "2009000200"
>
> Regards
> Marco


You will need to worry about what happens when your code is restarted, but
the following should work.  As your subject says, I based my code on taking
in an old id from which I create the new one.

    Function NewID(ByVal s As String) As String
        Dim oldid As Integer = Integer.Parse(s.Substring(4))
        Dim oldyear As Integer = Integer.Parse(s.Substring(0, 4))
        Dim id As Integer
        Dim y as Integer = DateTime.Now.Year

        ' checks if the year has changed
        If (oldyear <> y) Then
            id = 1
        Else
            id = oldid + 1
        End If

        Return String.Format("{0}{1:d6}", y, id)
    End Function

--
Mike
Author
17 May 2009 12:04 PM
Co
Show quote Hide quote
On 17 mei, 13:48, "Family Tree Mike" <FamilyTreeM***@ThisOldHouse.com>
wrote:
> "Co" <vonclausow***@gmail.com> wrote in message
>
> news:72cfbffb-a039-4c56-9933-fc1893851acc@g19g2000vbi.googlegroups.com...
>
>
>
> > Hi All,
> > I'm trying to create a function that will create a "reference number"
> > which is one higher then the latest one added.
> > The reference number consist of "2009000100"
> > which is the year (2009) followed by a 6 figur digit "000100"
> > The code must create the new reference number as : "2009000101"
>
> > 'oldKenmerk = latest used reference number
> > Dim i As Integer
> >        Dim numberPart As String
> >        numberPart = Mid(oldKenmerk, 5, 6)
> >        Dim sNumPart As String = ""
>
> >        For i = 1 To 6
> >            If Mid(numberPart, i, 1) = "0" Then
> >                sNumPart = sNumPart & Mid(numberPart, i, 1)
> >            Else
> >                Exit For
> >            End If
> >        Next
> >        numberPart = Replace(numberPart, sNumPart, "")
>
> >        If Mid(oldKenmerk, 1, 4) = Year(Today) Then
> >            newKenmerk = Year(Today) & sNumPart & CInt(numberPart) + 1
> >        Else
> >            'the file is the first of the year
> >            newKenmerk = Year(Today) & "000100"
> >        End If
>
> > I don't think my code will work when you go from for example
> > "2009000199" to "2009000200"
>
> > Regards
> > Marco
>
> You will need to worry about what happens when your code is restarted, but
> the following should work.  As your subject says, I based my code on taking
> in an old id from which I create the new one.
>
>     Function NewID(ByVal s As String) As String
>         Dim oldid As Integer = Integer.Parse(s.Substring(4))
>         Dim oldyear As Integer = Integer.Parse(s.Substring(0, 4))
>         Dim id As Integer
>         Dim y as Integer = DateTime.Now.Year
>
>         ' checks if the year has changed
>         If (oldyear <> y) Then
>             id = 1
>         Else
>             id = oldid + 1
>         End If
>
>         Return String.Format("{0}{1:d6}", y, id)
>     End Function
>
> --
> Mike

Thanks Mike,

that works great.

MArco
Author
17 May 2009 12:20 PM
Mike
Try this.

     function GetNextNumber(byval RefNum as String) as string

        dim Y as integer = cint(val(left(RefNum,4)))
        dim N as Integer = cint(val(Mid(RefNum,5)))+1

        if Year(Today) > Y then   ' check for new year
            Y = Year(Today) : N = 101
        end if
        if (N < 101) then N = 101 ' check minimum value

        RefNum = Y.ToString.trim().PadLeft(4,"0"c) + _
                 N.ToString.trim().PadLeft(6,"0"c)
        Return Refnum
     end function

Test it with this:

     sub test(byval s as string)
         dim nf as string = GetNextNumber(s)
         console.writeline("refnum: {0, -15} next => {1,-15}",s, nf)
     end sub
     Sub Main()
         test("200900212")
         test("200900000")
         test("200801121")
         test("200900199")
         test("") ' expecting first reference of year
         console.readkey()
     End Sub

refnum: 200900212       next => 2009000213
refnum: 200900000       next => 2009000101
refnum: 200801121       next => 2009000101
refnum: 200900199       next => 2009000200
refnum:                 next => 2009000101

--

Co wrote:
Show quoteHide quote
> Hi All,
> I'm trying to create a function that will create a "reference number"
> which is one higher then the latest one added.
> The reference number consist of "2009000100"
> which is the year (2009) followed by a 6 figur digit "000100"
> The code must create the new reference number as : "2009000101"
>
> 'oldKenmerk = latest used reference number
> Dim i As Integer
>         Dim numberPart As String
>         numberPart = Mid(oldKenmerk, 5, 6)
>         Dim sNumPart As String = ""
>
>         For i = 1 To 6
>             If Mid(numberPart, i, 1) = "0" Then
>                 sNumPart = sNumPart & Mid(numberPart, i, 1)
>             Else
>                 Exit For
>             End If
>         Next
>         numberPart = Replace(numberPart, sNumPart, "")
>
>         If Mid(oldKenmerk, 1, 4) = Year(Today) Then
>             newKenmerk = Year(Today) & sNumPart & CInt(numberPart) + 1
>         Else
>             'the file is the first of the year
>             newKenmerk = Year(Today) & "000100"
>         End If
>
> I don't think my code will work when you go from for example
> "2009000199" to "2009000200"
>
> Regards
> Marco
Author
17 May 2009 1:49 PM
Cor Ligthert[MVP]
I don't know if I've sent this twice

Co

A variation on the string format in this case

\\\\
Dim a = "2009000199"
a = (CInt(a) + 1).ToString
a = Now.Year.ToString & a.Substring(4)
///

Cor

Show quoteHide quote
"Co" <vonclausow***@gmail.com> wrote in message
news:72cfbffb-a039-4c56-9933-fc1893851acc@g19g2000vbi.googlegroups.com...
> Hi All,
> I'm trying to create a function that will create a "reference number"
> which is one higher then the latest one added.
> The reference number consist of "2009000100"
> which is the year (2009) followed by a 6 figur digit "000100"
> The code must create the new reference number as : "2009000101"
>
> 'oldKenmerk = latest used reference number
> Dim i As Integer
>        Dim numberPart As String
>        numberPart = Mid(oldKenmerk, 5, 6)
>        Dim sNumPart As String = ""
>
>        For i = 1 To 6
>            If Mid(numberPart, i, 1) = "0" Then
>                sNumPart = sNumPart & Mid(numberPart, i, 1)
>            Else
>                Exit For
>            End If
>        Next
>        numberPart = Replace(numberPart, sNumPart, "")
>
>        If Mid(oldKenmerk, 1, 4) = Year(Today) Then
>            newKenmerk = Year(Today) & sNumPart & CInt(numberPart) + 1
>        Else
>            'the file is the first of the year
>            newKenmerk = Year(Today) & "000100"
>        End If
>
> I don't think my code will work when you go from for example
> "2009000199" to "2009000200"
>
> Regards
> Marco
Author
17 May 2009 2:11 PM
Mike
The reason what you want to break it apart first before conversion to
integer is because of potential out of range conditions.  In this
case, the OP's grandchildren will have a Year 2147 problem :-)

Probably using

     a = (Ctype(a,uint32) + 1).ToString

Cor Ligthert[MVP] wrote:
Show quoteHide quote
> I don't know if I've sent this twice
>
> Co
>
> A variation on the string format in this case
>
> \\\\
> Dim a = "2009000199"
> a = (CInt(a) + 1).ToString
> a = Now.Year.ToString & a.Substring(4)
> ///
>
> Cor
>
> "Co" <vonclausow***@gmail.com> wrote in message
> news:72cfbffb-a039-4c56-9933-fc1893851acc@g19g2000vbi.googlegroups.com...
>> Hi All,
>> I'm trying to create a function that will create a "reference number"
>> which is one higher then the latest one added.
>> The reference number consist of "2009000100"
>> which is the year (2009) followed by a 6 figur digit "000100"
>> The code must create the new reference number as : "2009000101"
>>
>> 'oldKenmerk = latest used reference number
>> Dim i As Integer
>>        Dim numberPart As String
>>        numberPart = Mid(oldKenmerk, 5, 6)
>>        Dim sNumPart As String = ""
>>
>>        For i = 1 To 6
>>            If Mid(numberPart, i, 1) = "0" Then
>>                sNumPart = sNumPart & Mid(numberPart, i, 1)
>>            Else
>>                Exit For
>>            End If
>>        Next
>>        numberPart = Replace(numberPart, sNumPart, "")
>>
>>        If Mid(oldKenmerk, 1, 4) = Year(Today) Then
>>            newKenmerk = Year(Today) & sNumPart & CInt(numberPart) + 1
>>        Else
>>            'the file is the first of the year
>>            newKenmerk = Year(Today) & "000100"
>>        End If
>>
>> I don't think my code will work when you go from for example
>> "2009000199" to "2009000200"
>>
>> Regards
>> Marco
>