Solved

Append query using DMax() function to increment number field

Posted on 2004-09-24
4
1,219 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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Most if not all databases provide tools to filter data; even simple mail-merge programs might offer basic filtering capabilities. This is so important that, although Access has many built-in features to help the user in this task, developers often n…
Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.

743 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

11 Experts available now in Live!

Get 1:1 Help Now