Solved

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

Posted on 2015-02-12
6
150 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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 34

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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.

762 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now