Results 1 to 8 of 8
  1. #1
    Mazkot is offline Novice
    Windows 10 Access 2013 64bit
    Join Date
    Oct 2020
    Posts
    4

    Query help.

    I would like to know if it's possible to create duplicate records based on a field in original table.   Below is a simple look at what I want to do.  I would like to make duplicate records in my new query based on the number in the QTY field.



    Click image for larger version. 

Name:	CreateNewTableORIG.jpg 
Views:	23 
Size:	18.5 KB 
ID:	43155
    Would like to get this:
    Click image for larger version. 

Name:	desired.jpg 
Views:	22 
Size:	34.7 KB 
ID:	43156

    Thanks

  2. #2
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2016
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,722
    Can you tell us why you want to do this? There may be options/alternatives --once we understand the requirement and rationale.

  3. #3
    Mazkot is offline Novice
    Windows 10 Access 2013 64bit
    Join Date
    Oct 2020
    Posts
    4
    I'm looking to import the data into UPS Worldship and it cannot create the package details as they are kept in my database...they need a seperate line item for each package.


    Quote Originally Posted by orange View Post
    Can you tell us why you want to do this? There may be options/alternatives --once we understand the requirement and rationale.

  4. #4
    accesstos's Avatar
    accesstos is offline Expert
    Windows XP Access 2007
    Join Date
    Dec 2018
    Location
    Greece
    Posts
    551
    You need a new table (say tblDuplicates) to keep only the PNUMs.

    Copy the code below in a standard code module:
    Code:
    Sub DuplicateRows()
        Dim db As DAO.Database
        Dim rs As DAO.Recordset
        Dim i As Long
        
        Set db = CurrentDb
        db.Execute "DELETE * From tblDuplicates", dbFailOnError
        Set rs = db.OpenRecordset("SELECT PNUM, QTY FROM tblOriginal", dbOpenForwardOnly)
        With rs
            If Not (.BOF And .EOF) Then
                While Not .EOF
                    For i = 1 To Nz(!QTY, 0)
                        db.Execute "INSERT INTO tblDuplicates (PNUM) VALUES ('" & !PNUM & "')", dbFailOnError
                    Next i
                    .MoveNext
                Wend
            End If
            .Close
        End With
        Set rs = Nothing
        Set db = Nothing
    End Sub
    Create a query with that SQL statement:
    Code:
    SELECT tblOriginal.*
    FROM tblDuplicates INNER JOIN tblOriginal 
    ON tblDuplicates.PNUM = tblOriginal.PNUM;
    Run the procedure and open the query to check the results.

    Wait for a better solution.

    Cheers,
    John

  5. #5
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2016
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,722
    I agree with John re an approach that seems to work, has a specific purpose and is repeatable. However, I think it would help if you could tell us a little more about the requirement/spec for UPS Worldship and the set up you require.

  6. #6
    Mazkot is offline Novice
    Windows 10 Access 2013 64bit
    Join Date
    Oct 2020
    Posts
    4
    My data right now is consolidated by product number(PNUM). each line has a different PNUM and each PNUM could have more than one qty. UPS's software cannot handle it that way. Each package should have it own record so that it pulls the correct amount of packages into it program for shipping. If I pull the data in like I have it in my database in the above sample, I only get 3 cartons being shipped when I actually should have 6.

  7. #7
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,898
    Can be done in query without VBA. Requires a unique identifier field - autonumber should serve. However, be aware this can perform slowly with very large dataset and then VBA solution might be preferable.

    SELECT Data.*
    FROM Data, (SELECT DISTINCT [Tens]+[Ones] AS Factor, 10*Abs([Deca].[id] Mod 10) AS Tens, Abs([Uno].[id] Mod 10) AS Ones FROM MSysObjects AS Uno, MSysObjects AS Deca) AS F
    WHERE F.Factor<QTY
    ORDER BY ID;

    Although, this will not alter the BL value as shown in sample output. Seems to be a typo error in the BL values.

    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.

  8. #8
    Mazkot is offline Novice
    Windows 10 Access 2013 64bit
    Join Date
    Oct 2020
    Posts
    4
    Thanks. I'll let you know if I run into any issues.

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

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