Solved

Create an expression in a query that will number line items beginning at "1".

Posted on 2015-02-12
6
166 Views
Last Modified: 2016-02-11
I have two main data tables, one for a material list header, one for material list line items. When creating a purchase order from certain line items in my detail subform, I have created a sequence of queries that will create a unique purchase order number and update my detail field in my line items table to that purchase order number. I need to create a query which will automatically assign purchase order line numbers beginning with "1" whenever a new purchase order is created. Since my material list detail could have up to a few hundred lines in it, with groups of lines making up a dozen or so purchase orders, I need a way to generate PO line numbers. I already have a material list line item, but need each PO numbered separately for my reports etc.

I have made the following function work for me, but it's not numbering each sequence beginning with "1" as I had hoped it would. (see attachments)

Function Serialize(qryname As String, keyname As String, keyvalue) As Long

Dim rs As Recordset


On Error GoTo Err_Serialize

Set rs = CurrentDb.OpenRecordset(qryname, dbOpenDynaset, dbReadOnly)

rs.FindFirst Application.BuildCriteria(keyname, rs.Fields(keyname).Type, keyvalue)

Serialize = Nz(rs.AbsolutePosition, -1) + 1


Err_Serialize:

rs.Close

Set rs = Nothing

End Function
Capture.jpg
0
Comment
Question by:Shooter54
[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
  • 3
  • 2
6 Comments
 
LVL 45

Expert Comment

by:aikimark
ID: 40606631
Is this for a form or a report or something else?

If for a report, the easiest may be to create the sequence numbers at the report/detail level, and not in the data source query.

* What do the tables look like?
* Are there unique ID/autonumber fields in the details table?
* How important is performance?
0
 

Author Comment

by:Shooter54
ID: 40606650
I am using the query to update a field in a table.

I have two tables that are relevant to the question, the Material list header table which contains accounting inf. etc., in which the material list number is unique and autonumber. The detail table contains the fields that I want to update/sequentially number, and they are joined by a similar NOT unique field named MaterialList. The unique/key field in my detail or line item table is simply an autonumber that I don't really use for anything. The field that I want to update resides in my detail table, and is named POLineNumber or something similar.
0
 
LVL 45

Expert Comment

by:aikimark
ID: 40606661
There are two methods that come to mind.  The first is to include the line number values when you populate the table (as the order is created).

Updating existing table data poses some challenges, since some of the easiest-to-use methods create a non-updatable recordset.

I think the reliable method will be to use a field expression or DCOUNT() domain function that counts the number of detail ID values less than the current detail ID value.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Shooter54
ID: 40606790
Not being well versed in VBA, how would I go about implementing this?
0
 
LVL 45

Accepted Solution

by:
aikimark earned 500 total points
ID: 40607120
Example:
Select D.*, (Select Count(*)+1 From Details As C Where C.ID < D.ID) As DetailNumber
From Details As D

Open in new window

0
 
LVL 37

Expert Comment

by:PatHartman
ID: 40608819
The items should not be numbered after the fact.  They should be numbered as the rows are entered.

Your question makes it sound like you are not using bound forms.  Is that the case?
0

Featured Post

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

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.
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…
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…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

728 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