Results 1 to 3 of 3
  1. #1
    emihir0 is offline Novice
    Windows 8 Access 2013
    Join Date
    Apr 2015
    Posts
    22

    Returning multiple rows for each record.

    I have a table



    Orders
    ID_Order (autoNum) ID_Cust (int) amountOrdered (int) amountPerPackage (int)
    1 1 20 10
    2 1 10 5
    3 1 5 5


    And I want my output query to look like this:

    packageNo ID_Order ID_Cust
    1 1 1
    2 1 1
    3 2 1
    4 2 1
    5 3 1

    Here is a short pseudo-code (same result can be achieved in a few different ways using code).
    Code:
    counter: Int
    remainder: Int
    counter = 0
    remainder = 0
    
    For (OrderID, CustID) in Orders:
    
    While (remainder != 0):
    remainder = amountOrdered - amountPerPackage
    amountOrdered = amountOrdered - amountPerPackage
    counter = counter + 1 output: counter, OrderID, CustID
    Loop
    Loop
    My question, however, is, how do you achieve this using SQL?

    Basically I want the output query to be used in a report to print out the packaging labels.

  2. #2
    CJ_London is offline VIP
    Windows 8 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,845
    Which sql are you referring to? T-SQL (sql server) or Access SQL?

    If the latter you would need to use vba code and build it into the print process or create label records which can then be queried from your report

    Not sure about T-SQL from an access perspective

  3. #3
    InsuranceGuy is offline Competent Performer
    Windows 7 64bit Access 2013
    Join Date
    Aug 2015
    Location
    Colorado
    Posts
    126
    I haven't seen this accomplished in an Access query before. I believe it's possible in TSQL using recursive references; however, Access does not support those methods.

    If you are using Access, you are probably best suited to develop a VBA routine, using your pseudocode, that takes the source data and builds your output table. Your report will then refer to the output data that your VBA routine created.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 4
    Last Post: 07-16-2015, 04:49 PM
  2. IIF Function - Returning Extra Rows
    By accessnewbie352 in forum Queries
    Replies: 5
    Last Post: 01-02-2015, 12:27 PM
  3. Import Record with Multiple Rows
    By nerkalyn in forum Import/Export Data
    Replies: 6
    Last Post: 02-19-2014, 03:03 PM
  4. record set not returning rows
    By lvmoore in forum Forms
    Replies: 3
    Last Post: 09-25-2010, 10:48 AM
  5. Returning correct rows in a join/nested query
    By goneaccessing in forum Queries
    Replies: 5
    Last Post: 03-03-2010, 12:21 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums