Invoice Number Equals Current Year Plus One

koolwaters

Active Member
Joined
May 16, 2007
Messages
403
Happy New Year Everyone!

We have a database we are using for invoicing and we want the invoice number to be the current year and an incremented number, but we want the incremented number to restart at 1 for each year (2015-01, 2015-02, 2015-03, 2015-04, 2016-01, 2016-02, etc.).

The code below worked fine for 2015 but I need some help in adjusting it for 2016. The last invoice number for 2016 is 781 so the current code returns 2016-0782 as the invoice number but we want it to be 2016-01 and then increment from there (2016-01, 2016-02, 2016-03, etc.).

Any suggestions?

Code:
Dim strNextInvoiceNo As Integer
Dim strInvoiceNo As String
strForm = "frmInvoice"
   
    strNextInvoiceNo = Nz(DMax("[LastInvoiceNo]", "tblInvoice"), 0) + 1
    strInvoiceNo = DatePart("yyyy", Now()) & "-" & "0" & strNextInvoiceNo
        
    DoCmd.OpenForm stDocName, acNormal, , , acFormAdd
    Forms.frmInvoice.InvoiceNo = strInvoiceNo
    Forms.frmInvoice.LastInvoiceNo = strNextInvoiceNo

Thanks in advance!
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Hi,

Not sure what the design of your "tblinvoice" so can ony help by a few pointers.
You need a check performed BEFORE adding 1 to the StrNextInvoiceNo.
1. I assume there's a field in the DB in which you register the invoice date; create a variable holding the year part of the last invoice date (assume it's called [Invoicedate])
2. perform a test of the value of year([InvoiceDate]) against Datapart ("yyyy", Now())
3. if test = equal do LastInvoiceN0 + 1
4. if test <> equal do strNextInvoiceNo = 1

It will probably look something close to this.

Code:
Dim strNextInvoiceNo As Integer
Dim LastInvoiceYear as String
Dim strInvoiceNo As String
strForm = "frmInvoice"
   
    LastInvoiceYear = DMAX("year([Invoicedate])","tblInvoice")

    If LastInvoiceYear = datepart("YYYY", Now()) then
      strNextInvoiceNo = Nz(DMax("[LastInvoiceNo]", "tblInvoice"), 0) + 1
    Else
      strNextInvoiceNo = 1
    End if

    strInvoiceNo = DatePart("yyyy", Now()) & "-" & "0" & strNextInvoiceNo
        
    DoCmd.OpenForm stDocName, acNormal, , , acFormAdd
    Forms.frmInvoice.InvoiceNo = strInvoiceNo
    Forms.frmInvoice.LastInvoiceNo = strNextInvoiceNo

Hope this helps.
 
Last edited:
Upvote 0
It only worked for the first invoice.

When I attempted to try a second time, I get the following error:
The expression you entered as a query parameter produced this error: '[strLastInvoiceYear]'

The invoice date field uses a default value =Date()
 
Last edited:
Upvote 0
What seems to be happening is that when it finds the year in the last invoice date for 2015, it adds 1. However, when the year in the invoice date is 2016, it looks for the highest invoice number that was used, which is 781 for 2015 and returns 2016-0782, which is incorrect.
 
Upvote 0
Hi,

Not sure why this error comes up. It could be because the year argument probably returns more than 1 value so to restrict that try changing this line :
Code:
LastInvoiceYear = DMAX("year([Invoicedate])","tblInvoice")

with

Code:
LastInvoiceYear = DMAX("year([Invoicedate])","tblInvoice", "Nz(DMax("[LastInvoiceNo]", "tblInvoice"), 0)")
 
Upvote 0
Here is a little different way you may want to consider using going forward:

Set up a table with two fields: Year (Primary Key field) and LastInvoiceNumber
Then make an entry for each year (i.e. 2015, 2016, and putting out some future years with a LastInvoiceNumber of 0)

Then, when you create an invoice create a VBA script or Update Query that looks up the LastInvoiceNumber by year, adds one, and updates the LastInvoiceNumber field in that table.
 
Upvote 0
When I replace the code, I get a compile error:
Expected: list separator or )
and it highlights
Code:
[LastInvoiceNo]
 
Upvote 0
Hi,

Figured it out.
Try it like this:
Code:
Dim strNextInvoiceNo As Integer
Dim LastInvoiceYear As String
Dim strInvoiceNo As String

   
    LastInvoiceYear = DatePart("YYYY", (Nz(DMax("[InvoiceDate]", "tblInvoice"), 1)))
    LastInvoiceDate = Nz(DMax("[InvoiceDate]", "tblInvoice"), 1)
    CurrDate = DatePart("YYYY", Now())

    
    If LastInvoiceYear = CurrDate Then
      strNextInvoiceNo = Nz(DMax("LastInvoiceNo", "tblinvoice", "year([invoicedate])=year(now())"), 0) + 1
    Else
      strNextInvoiceNo = 1
    End If

    strInvoiceNo = DatePart("yyyy", Now()) & "-" & "0" & strNextInvoiceNo
    
    DoCmd.OpenForm stDocName, acNormal, , , acFormAdd
    Forms.frmInvoice.InvoiceNo = strInvoiceNo
    Forms.frmInvoice.LastInvoiceNo = strNextInvoiceNo
 
Upvote 0
No problem, your welcome.
we got there in the end.
Glad i could help.
 
Upvote 0

Forum statistics

Threads
1,221,829
Messages
6,162,232
Members
451,756
Latest member
tommyw

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top