473,385 Members | 1,392 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Find last (most recent) order and add 'one' to OrderID

stonward
145 100+
Hi folks,

Happily installing my new system, when the buyer pipes up saying he needs to continue using his old invoice number sequence.

So I've had to remove my autonumber sequence for Customer Orders (OrderID). When the orders form opens it needs to add one to the last (most recent) orderid and use as orderid.

I can't just find the largest orderid and add one, 'cause the numbers the user has in the past are a mess (and he doesn't want to change them!).

I've tried adding a field to the orders table (mycount) and adding one to that, but couldn't get it to work....I feel a query to find the most recent is the way to go, but I wanna find just the most recent one - not a list.

Not sure how to go about this (normally I'd just graft at it for weeks - but I'm really pushed today!).

Thanks

RPE
Jan 30 '13 #1

✓ answered by Seth Schrock

You could query your table setting the Order By to have the newest on top and have the query just get the first record using the SELECT TOP 1 ... FROM .... You could then use DLookup() to get the OrderID and add 1.

Just curious, do you have a time field so that the records can be sorted not just by date (as I would assume that there could be many orders on the same day), but also include the time so that you could have more assurance that you did get the newest record?

Also something to consider would be that if the previous order numbers are a mess, are you sure that you can always just add 1 and not conflict with another order ID? If you can't just use the highest Order ID, then you will eventually run into a situation where adding 1 to the previous number will duplicate the Order ID. So I would suggest possibly using a loop to add 1 to the previous Order ID and then check for a match. If one is found, then loop back through and add 1 again and then check again, and so on. A loop might not be exactly what you want, but possibly a "Manual loop" where you use a GoTo to loop back up might be. I haven't come up with an exact solution, but I think something along this line needs to be considered.

7 1749
Seth Schrock
2,965 Expert 2GB
You could query your table setting the Order By to have the newest on top and have the query just get the first record using the SELECT TOP 1 ... FROM .... You could then use DLookup() to get the OrderID and add 1.

Just curious, do you have a time field so that the records can be sorted not just by date (as I would assume that there could be many orders on the same day), but also include the time so that you could have more assurance that you did get the newest record?

Also something to consider would be that if the previous order numbers are a mess, are you sure that you can always just add 1 and not conflict with another order ID? If you can't just use the highest Order ID, then you will eventually run into a situation where adding 1 to the previous number will duplicate the Order ID. So I would suggest possibly using a loop to add 1 to the previous Order ID and then check for a match. If one is found, then loop back through and add 1 again and then check again, and so on. A loop might not be exactly what you want, but possibly a "Manual loop" where you use a GoTo to loop back up might be. I haven't come up with an exact solution, but I think something along this line needs to be considered.
Jan 30 '13 #2
TheSmileyCoder
2,322 Expert Mod 2GB
Why exactly can't you simply add 1 to largest current ID? How will you prevent duplicates if you just add 1 to the most recent and the currently used order numbers are a mess?


My suggestion would still be to use autonumber and insert the existing numbers (the autonumber will then automatically continue from the largest number).

Of course this can be accomplished in code, but I don't see a good reason to apply a code based custom solution for something autonumber handles so efficiently.
Jan 30 '13 #3
stonward
145 100+
The user has added numbers to the usual 6 digit number to account for returns and such like. I've now also found gaps in the usual sequence which also complicates updating as an autonumber sequence. It's just nasty.
Thanks for your time.

RPE
Jan 30 '13 #4
NeoPa
32,556 Expert Mod 16PB
Many IDs are not specifically numeric. People often want to see information within their IDs such as, for instance, a year value and/or something to indicate a parent for sub-tables.

The usual way to handle this is to use a DMax() call with a filter specifying the pattern that it needs to match. Sorting then selecting TOP 1 is an innovative approach, but possibly more complicated than necessary.
Jan 31 '13 #5
Seth Schrock
2,965 Expert 2GB
I never remember those domain functions (except for DLookup). DMax would probably work better since my idea already had a domain function in it and DMax just combines the two steps.

Thanks for pointing this out NeoPa.
Jan 31 '13 #6
stonward
145 100+
Hi Guys,

NeoPa's is the best (most correct?!) answer methinks, but i have used Seth's purely because I know and understand those particular domain functions rather better.

But neoPa's idea will find its way into my first update, I feel certain.

As always, great thanks for your time and patience,



Stonward
Feb 5 '13 #7
NeoPa
32,556 Expert Mod 16PB
Great attitude. I'm going to reset Best Answer for you, but only in order to choose the post of Seth's where he gave his answer.
Feb 5 '13 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Nova's Taylor | last post by:
Hi folks, I am a newbie to Python and am hoping that someone can get me started on a log parser that I am trying to write. The log is an ASCII file that contains a process identifier (PID),...
0
by: Gary | last post by:
Does anyone know how to do a sort to find the most recent distinct records. We have records with equipment, and activities the equipment was used for, but I need a distinct list of where the...
2
by: Shaiguy | last post by:
I have a table containing the following fields: ProjectUpdateID (PrimaryKey) ProjectID UpdateDate I would like to create a Query in Ms Access 2000 which will return me the most recent 2...
6
by: Mark | last post by:
hi, i want to display the five most recent rows in my table (there is a timestamp), but sorted in ascending order. the problem is that when i call SELECT * FROM spam ORDER BY time ASC LIMIT...
4
by: Lee | last post by:
I have an application where I need to find the most recent file placed in a folder. For example, my users periodically place an image file with the format "ImageXXXX.jpg" (where XXXX is some...
9
by: psuaudi | last post by:
i have a table with dates that events occured. it looks something like this: 11/18/2006 1:00PM Open 11/18/2006 1:25PM Close I created a select query that selects the last (most recent)...
4
by: Sector 7G | last post by:
I'm working with a SQL query for a Human Resources database. Its intended purpose is to find all the paycheck records with a check date (prckhist.chkdate ) more recent than eleven days past the...
2
by: robert.waters | last post by:
I need to perform the following: - select the most recent X number of records in a table (there is a timestamp field) - select the Nth occurrence of X number of records ex: - most recent 10...
3
by: AnthonyT | last post by:
Hi All I have a major problem with an access query and I am near the end of my tether! I have taken over a project with a badly built access database and as resources are not available to start...
1
by: lutz | last post by:
I fully admit, I am begging for some help. I am at the mercy of anyone's generous nature. I wish I had a template to follow for this one. I thank you in advance for anyone's advice. Maybe...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.