Solved

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

Posted on 2015-02-12
6
158 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
  • 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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 

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 35

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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.
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.
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…

860 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