[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Append query using DMax() function to increment number field

Posted on 2004-09-24
4
Medium Priority
?
1,327 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 51

Accepted Solution

by:
Steve Bink earned 375 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 51

Expert Comment

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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
If you need a simple but flexible process for maintaining an audit trail of who created, edited, or deleted data from a table, or multiple tables, and you can do all of your work from within a form, this simple Audit Log will work for you.
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

650 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