Solved

Append query using DMax() function to increment number field

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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Some sers suddenly getting error popup msg 28 88
vba sql wild card passing in code 3 22
Run report based on combo box selection - access 2007 2 19
Access query expression 6 20
When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

776 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