Page 1 of 2 12 LastLast
Results 1 to 15 of 19
  1. #1
    acenumber5 is offline Novice
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2012
    Posts
    19

    Help generating line item number in order table

    Hey Guys! (and girls!),

    I am writing a query in Access 2010 using the query designer to export order data from my orders table to a csv file that I will then upload to a distributor.

    The problem I am having is that the distributor requires the csv file to contain the line item of the order and my storefront / database does not have this.
    For eg:
    My (simplified) order table looks this

    Order_number, SKU, quantity, Shipping Address
    1001, 1234, 1, 2039 abc drive dallas tx
    1002, 9876, 1, 2040 abc drive dallas tx
    1003, 8765, 1, 2041 abc drive Dallas tx
    1003, 1234, 1, 2041 abc drive Dallas tx
    1003, 1235, 1, 2041 abc drive Dallas tx


    So, my problem is with order number 1003 - it has multiple items so I need to make a field that counts the number of duplicates.. Ideally so my table would end up like this:

    Order_number, SKU, quantity, Shipping Address, line item number
    1001, 1234, 1, 2039 abc drive dallas tx, 1
    1002, 9876, 1, 2040 abc drive dallas tx, 1
    1003, 8765, 1, 2041 abc drive Dallas tx, 1
    1003, 1234, 1, 2041 abc drive Dallas tx, 2
    1003, 1235, 1, 2041 abc drive Dallas tx, 3

    ...but I can't for the life of me figure out how to do it... any suggestions?


    Thanks!

  2. #2
    orange's Avatar
    orange is online now Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    I think your problem revolves around your design.
    You have multiple Orders ans Multiple Items/Products.
    look at the data model at this site
    http://www.databaseanswers.org/data_...ders/index.htm

    Customer_Order_Product is equivalent to OrderLineItem

    This model shows the OrderItems
    http://www.databaseanswers.org/data_...arts/index.htm

    These free video tutorials describe the processes of data modelling, normalization and entity relationship diagramming. There are other videos, but this group is by the same presenter and covers an example situation. The example relates Customers, Orders and Items.

    http://www.youtube.com/watch?v=IiVq8M5DBkk Logical data modeling

    http://www.youtube.com/watch?v=BGMwuOtRfqU Candidate key

    http://www.youtube.com/watch?v=ZiB-BKCzS_I Normalization

    http://www.youtube.com/watch?v=pJ47btpjAhA Normalization example

    http://www.youtube.com/watch?v=q3Wg2fZENK0 1st Normal form

    http://www.youtube.com/watch?v=vji0pfliHZI 2nd Normal form

    http://www.youtube.com/watch?v=HH-QR7t-kMo 3rd Normal form

    http://www.youtube.com/watch?v=q1GaaGHHAqM E_R Diagramming

    http://www.youtube.com/watch?v=lXAGQ8vmhCY ERD Part 2

    The first few topics at this site are well worth the read.

    http://www.rogersaccesslibrary.com/forum/topic238.html

    Good luck with your project.

  3. #3
    acenumber5 is offline Novice
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2012
    Posts
    19
    Thanks for the info. I don't have a lot of control over how the storefront (aspdotnetstorefront) sets this up, but this is great info. Thanks again!
    AC


    Quote Originally Posted by orange View Post
    I think your problem revolves around your design.
    You have multiple Orders ans Multiple Items/Products.
    look at the data model at this site
    http://www.databaseanswers.org/data_...ders/index.htm

    Customer_Order_Product is equivalent to OrderLineItem

    This model shows the OrderItems
    http://www.databaseanswers.org/data_...arts/index.htm

    These free video tutorials describe the processes of data modelling, normalization and entity relationship diagramming. There are other videos, but this group is by the same presenter and covers an example situation. The example relates Customers, Orders and Items.

    http://www.youtube.com/watch?v=IiVq8M5DBkk Logical data modeling

    http://www.youtube.com/watch?v=BGMwuOtRfqU Candidate key

    http://www.youtube.com/watch?v=ZiB-BKCzS_I Normalization

    http://www.youtube.com/watch?v=pJ47btpjAhA Normalization example

    http://www.youtube.com/watch?v=q3Wg2fZENK0 1st Normal form

    http://www.youtube.com/watch?v=vji0pfliHZI 2nd Normal form

    http://www.youtube.com/watch?v=HH-QR7t-kMo 3rd Normal form

    http://www.youtube.com/watch?v=q1GaaGHHAqM E_R Diagramming

    http://www.youtube.com/watch?v=lXAGQ8vmhCY ERD Part 2

    The first few topics at this site are well worth the read.

    http://www.rogersaccesslibrary.com/forum/topic238.html

    Good luck with your project.

  4. #4
    acenumber5 is offline Novice
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2012
    Posts
    19
    I'll check it out, thanks!

  5. #5
    acenumber5 is offline Novice
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2012
    Posts
    19
    I figured out how to do it in Excel via:
    COUNTIF($A$1:A1,A1)


    http://stackoverflow.com/questions/7...a-number-after


    ...but I'm still trying to figure out how to do it in access....

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  7. #7
    acenumber5 is offline Novice
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2012
    Posts
    19
    You can say that again... (not easy in query form.)... Reviewing.

  8. #8
    orange's Avatar
    orange is online now Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    Great,solution in Excel. But I thought you were using Access.
    see http://allenbrowne.com/ranking.html

    I still think it may be a table design/normalization issue.

  9. #9
    acenumber5 is offline Novice
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2012
    Posts
    19
    Correct, I am trying to do it all in Access. Having to export in from Access, then do it in excel, then upload it to the distributor will be a very bad plan B...

  10. #10
    orange's Avatar
    orange is online now Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    see the article at link below -- specifically the OrderLines Table

    I couldn't find a better example on short notice, but the videos I suggested earlier
    show exactly the table LineItems that I think you may be missing.

    http://www.tomjewett.com/dbdesign/dbdesign.php?page=manymany.php




  11. #11
    acenumber5 is offline Novice
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2012
    Posts
    19
    ... Please excuse my lack of knowledge. I am new to this... I might have misspoke earlier. After looking at this diagram I realized that I do have an order table, and an "order lines table". Here is a screenshot.

    http://www.gamesandmovies.com/orderexport.jpg

    Still in my "orderlines table" (which is dbo_Orders_ShoppingCart) I have multiple rows with the same order number, representing a single order with more than one item, but no field withe the line item of the order...

    Thanks,
    AC

  12. #12
    orange's Avatar
    orange is online now Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    Ok, i have tried various things, including Lebans RowNumber, but the best I can do is a small vba proc as below.

    I took your sample data and imported it into Access in a table called OrdersAndSKU

    My proc writes out the fields in the Order record and the LineItem.
    You could write this to a table. I wasn't able to do it with a query.

    Here's the output
    1001,1234,1,2039 abc drive dallas tx,1
    1002,9876,1,2040 abc drive dallas tx,1
    1003,1234,1,2041 abc drive Dallas tx,1
    1003,1235,1,2041 abc drive Dallas tx,2
    1003,8765,1,2041 abc drive Dallas tx,3


    Here's the procedure

    Code:
    '---------------------------------------------------------------------------------------
    ' Procedure : OrderLineItems
    ' Author    : Jack
    ' Date      : 23/08/2012
    ' Purpose   : To add a sequnce number (called LineItem) to records. The objective is to
    ' show sequential numbers for multiple records with the same Order_Number
    '---------------------------------------------------------------------------------------
    '
    Sub OrderLineItems()
        Dim db As DAO.Database
        Dim rs As DAO.Recordset
        Dim LineItem As Integer
    10  On Error GoTo OrderLineItems_Error
    
    20  LineItem = 0
        Dim HOrder As String
    30  Set db = CurrentDb
    40  Set rs = db.OpenRecordset("Select * from OrdersandSKU order by 1,2", , dbReadOnly)
    50  HOrder = rs!order_number
    60  Do While Not rs.EOF
    70      If HOrder = rs!order_number Then
    80          LineItem = LineItem + 1
    90      Else
    100         HOrder = rs!order_number
    110         LineItem = 1
    120     End If
    130     Debug.Print rs!order_number & "," & rs!SKU & "," & rs!quantity & "," & rs![Shipping Address] & "," & LineItem
    
    140     rs.MoveNext
    150 Loop
    
    160 On Error GoTo 0
    170 Exit Sub
    
    OrderLineItems_Error:
    
    180 MsgBox "Error " & Err.number & " (" & Err.Description & ") in procedure OrderLineItems of Module AWF_Related"
    
    End Sub
    Last edited by orange; 08-23-2012 at 06:11 AM. Reason: spelling

  13. #13
    acenumber5 is offline Novice
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2012
    Posts
    19
    Orange - Thank you! Thank you! I can't believe that you went to all that trouble for this. I really appreciate it. This is going to be a tremendous help!

    Thanks!
    AC




    Quote Originally Posted by orange View Post
    Ok, i have tried various things, including Lebans RowNumber, but the best I can do is a small vba proc as below.

    I took your sample data and imported it into Access in a table called OrdersAndSKU

    My proc writes out the fields in the Order record and the LineItem.
    You could write this to a table. I wasn't able to do it with a query.

    Here's the output
    1001,1234,1,2039 abc drive dallas tx,1
    1002,9876,1,2040 abc drive dallas tx,1
    1003,1234,1,2041 abc drive Dallas tx,1
    1003,1235,1,2041 abc drive Dallas tx,2
    1003,8765,1,2041 abc drive Dallas tx,3


    Here's the procedure

    Code:
    '---------------------------------------------------------------------------------------
    ' Procedure : OrderLineItems
    ' Author    : Jack
    ' Date      : 23/08/2012
    ' Purpose   : To add a sequnce number (called LineItem) to records. The objective is to
    ' show sequential numbers for multiple records with the same Order_Number
    '---------------------------------------------------------------------------------------
    '
    Sub OrderLineItems()
        Dim db As DAO.Database
        Dim rs As DAO.Recordset
        Dim LineItem As Integer
    10  On Error GoTo OrderLineItems_Error
    
    20  LineItem = 0
        Dim HOrder As String
    30  Set db = CurrentDb
    40  Set rs = db.OpenRecordset("Select * from OrdersandSKU order by 1,2", , dbReadOnly)
    50  HOrder = rs!order_number
    60  Do While Not rs.EOF
    70      If HOrder = rs!order_number Then
    80          LineItem = LineItem + 1
    90      Else
    100         HOrder = rs!order_number
    110         LineItem = 1
    120     End If
    130     Debug.Print rs!order_number & "," & rs!SKU & "," & rs!quantity & "," & rs![Shipping Address] & "," & LineItem
    
    140     rs.MoveNext
    150 Loop
    
    160 On Error GoTo 0
    170 Exit Sub
    
    OrderLineItems_Error:
    
    180 MsgBox "Error " & Err.number & " (" & Err.Description & ") in procedure OrderLineItems of Module AWF_Related"
    
    End Sub

  14. #14
    orange's Avatar
    orange is online now Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    No problem. A question for you --Why exactly do you need to identify a sequential number for the item on an order?
    Why couldn't the person you supply the csv file to put a number on as he/she wishes?

  15. #15
    acenumber5 is offline Novice
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2012
    Posts
    19
    For whatever reason, the distributor that I am sending this order file to requires it. I have to export these orders and getting them in the format that they require. One of those requirements is this sequential number on the orders. Everything else was pretty standard. (UPC, Shipping address, etc). They are then drop shipping the items for me. The distributor wouldn't want to spend time manually adjusting these order files. I assume the program they use to import my order file somehow uses the line item field to figure out what items go on the same order....

    . Until this site grows enough to justify using "real" EDI, I have to create what I call PMEDI, (Poor Man's EDI) Mainly using access queries to export relevant data... I'm doing the same thing for a lot of the shopping engine feeds. (Google shopping, Bing shopping, The Find, shopping.com, etc)

    Thanks again!

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. New Line (New Order) on same form
    By Gaccesses in forum Access
    Replies: 2
    Last Post: 07-11-2012, 12:44 PM
  2. Replies: 5
    Last Post: 05-05-2012, 10:11 AM
  3. Line item query
    By michaeltorpedo in forum Queries
    Replies: 9
    Last Post: 05-02-2012, 01:17 PM
  4. Replies: 4
    Last Post: 07-27-2011, 12:42 PM
  5. Adding line numbers to a purchase order report
    By jordanturner in forum Reports
    Replies: 1
    Last Post: 12-08-2010, 08:25 AM

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