Solved

Append query using DMax() function to increment number field

Posted on 2004-09-24
4
1,234 Views
Last Modified: 2008-12-10
I have used an append query to update field invoice number for next highest one in table . Search for by no date and then give those invoice numbers .

Problem  append Query   (Batch invoicing)

 UPDATE Invoices SET Invoices.[Invoice Date] = Date$(), Invoices.[Invoice Number] = DMax("[Invoice Number]","[invoices]")+1
WHERE (((Invoices.[Invoice Date]) Is Null));

It does find next number nut numbers all of them same example highest is 1001 it lables all 1002
How do I get it to label 1002,1003 etc

0
Comment
Question by:jetcompute
  • 2
  • 2
4 Comments
 
LVL 50

Accepted Solution

by:
Steve Bink earned 125 total points
ID: 12146994
You cannot do them all in one query like that.  The UPDATE does not take affect on just a single record at a time.  ALL UPDATES will be implemented after Access has pulled the recordset and determined which records require updating.  As such, all records will be given the same number, since Access will execute the DMax call only once.

Instead, you will have to pull a recordset (SELECT * FROM INVOICES WHERE [INVOICE_DATE] IS NULL) and go record-by-record changing each individually.  Something like this, I suppose:

Public Sub RefreshIDField()
Dim rs as DAO.Recordset

set rs = CurrentDB.OpenRecordset("SELECT [Invoice Date], [Invoice Number] FROM Invoices WHERE [Invoice Date] IS NULL")
rs.MoveFirst

do while not rs.EOF
    rs.edit
    rs![Invoice Number] = DMax("[Invoice Number]","[Invoices]") + 1
    rs![Invoice Date] = Now()
    rs.Update
    rs.MoveNext
loop

rs.Close
set rs = Nothing
End Sub
0
 
LVL 41

Expert Comment

by:shanesuebsahakarn
ID: 12147010
Use a recordset and loop through it:

Dim rst As Recordset

Set rst=CurrentDb.OpenRecordset("SELECT * FROM Invoices WHERE [Invoice Date] Is Null")
While not rst.EOF
   rst.Edit
   rst![Invoice Date]=Date()
   rst![Invoice Number]=DMax("[Invoice Number]","Invoices")+1
   rst.Update
   rst.MoveNext
Wend
rst.Close
Set rst=Nothing
0
 
LVL 41

Expert Comment

by:shanesuebsahakarn
ID: 12147012
Too slow...
0
 
LVL 50

Expert Comment

by:Steve Bink
ID: 12147291
LOL!  People are going to start thinking I have two accounts.  
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now