Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2015-02-12
6
Medium Priority
?
175 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 46

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 46

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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 

Author Comment

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

Accepted Solution

by:
aikimark earned 2000 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 39

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

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…
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

636 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