Solved

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

Posted on 2015-02-12
6
161 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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 

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 36

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

740 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