Question : Creating Custom IDs

We have IDs as follows: 20090001, 20090002 (basically the fiscal year plus 4 digits)

When a new year comes around (2011), I need the IDs to automatically start over at one again so the first ID of the year will be 20100001, the next year it will be 20110001 etc.

Right now I have the code as follows:

Dim maxIncident As Variant
Dim maxIncidentID As Variant

If Me.intIncidentID.Value = 0 Then

    'get the maximum Incident # Incidents table
    maxIncident = DMax("[intIncidentID]", "tblIncidents")

    'if the year in the maximum case number is not the current year then reset the numbers
    If CInt(Left(maxIncident, 4)) <> Year(Date) Then
        maxIncidentID = CInt(Left(Year(Date), 4)) + 1
    Else 'same year so increase by 1
        maxIncidentID = DMax("[intIncidentID]", "tblIncidents", Left(maxIncident, 4)) + 1

    End If

    'set default values on form
    Me.intIncidentID.Value = maxIncidentID

End If

Problem line of code is bold.  What I need is for the ID to have 3 leading zeros before the 1... right now the new year the + 1 so right now its 2011 but I need it so then it says 20100001

Answer : Creating Custom IDs

try this codes


Dim newVal As String, curVal As String, seq As String,maxIncidentID
If DMax("[intIncidentID]", "tblIncidents") > 0 Then
    curVal = DMax("[intIncidentID]", "tblIncidents")
 'test if the day is the same
    If Left(curVal, 4) = Cstr(Year(Date)) Then
    seq = Format(Right(curVal, 4) + 1, "0000")
    newVal = Year(Date) & seq
        Else
    newVal = Year(Date) & "0001"
    End If
    Else
    newVal = Year(Date) & "0001"
End If
    maxIncidentID=newVal
Random Solutions  
 
programming4us programming4us