?
Solved

Append query using DMax() function to increment number field

Posted on 2004-09-24
4
Medium Priority
?
1,298 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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
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…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

764 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