create reference number based on old one...

C

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
 
A

Armin Zingler

Co said:
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
 
A

Armin Zingler

Co said:
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
 
F

Family Tree Mike

Co said:
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
 
F

Family Tree Mike

Co said:
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
 
C

Co

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
 
C

Co

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
 
C

Co

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

Thanks Mike,

that works great.

MArco
 
C

Co

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

Thanks Mike,

that works great.

MArco
 
M

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
 
M

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
 
C

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
 
C

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
 
M

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
 
M

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
 
A

Armin Zingler

Family said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top