Copying record to a table several times

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jacc14
    New Member
    • Jun 2007
    • 116

    #1

    Copying record to a table several times

    Hi
    I am sure there is an easy way to do this but I have exhausted all avenues.

    I have some label data in a table which I want to copy to an identical table. However the first label I may only want one of that label data copying to the new table. The second label I may want seven of that label data copying to the new table. I have a count field called "nooflabels "! which has 1 in the first record and 7 in the second.

    I have created an append query and set up a macro to run then created a button in my form to run that macro

    Code:
    Dim thecount As Integer
        Dim stDocName As String
    thecount = Me.Nooflabels
    
    
    stDocName = "Create_duplicatedespatches"
    DoCmd.RunMacro stDocName, thecount
    Problem is that the query doesnt treat each record separately so I either end up with 1 of each record or 7 of each record.

    How can I split this out.

    Am feel I am going down the wrong track and wondering if I should be using loop although i have never used this before.

    Please could someone advice on this.

    Thanks

    jacc14
  • FishVal
    Recognized Expert Specialist
    • Jun 2007
    • 2656

    #2
    Hi, jacc14.

    It is normally done in VBA via recordset iteration. I'm sure you will receive numerous advices of how it could be done.

    So I would suggest you pure SQL solution. Just to show feasibility of this approach. See attachment.

    Regards,
    Fish
    Attached Files

    Comment

    • jacc14
      New Member
      • Jun 2007
      • 116

      #3
      Dear FishVal

      This is excellent. I have applied and it works a treat.

      Thank you

      Jacc14

      Comment

      • jacc14
        New Member
        • Jun 2007
        • 116

        #4
        Dear FishVal
        I hope you dont mind me directing this question to yourself but you already know where I am going with this and I am struggling again

        The table which I have now produced looks like this.
        Code:
         ID         Label        Value   Count                [U]Unique Number[/U] 
         1         Label1         50      7                           1
         1         Label1         50      7                           2
         1         Label1         50      7                           3 
         1         Label1         50      7                           4
         1         Label1         50      7                           5
         1         Label1         50      7                           6
         1         Label1         50      7                           7
         2         Label2        100      1                           1
        I now need to assign a unique number by each group of records. This is so that when I print the labels it will say 1 of 7, 2 of 7, 3 of 7, 4 of 7, 5 of 7, 6 of 7, 7 of 7 and then for label2 it will say 1 of 1. This means that each batch of labels will have there own group of numbers. See above under Unique Number.

        I have used DMAX in a form before and know that this is the answer but I really want to put it in the sql query that you have helped me with as this is the part that creates the duplicated records. Is this possible. Sorry to be a pain.

        Many thanks

        Jacc14

        Comment

        • jacc14
          New Member
          • Jun 2007
          • 116

          #5
          sorry FishVal. Table doesnt look so clear now I have submitted it. Hope it makes sense.

          Best regards
          Jacc14

          Comment

          • FishVal
            Recognized Expert Specialist
            • Jun 2007
            • 2656

            #6
            Hello, jacc14.

            Below is a query (in context of the attachment tables):
            [code=sql]
            SELECT tblItems.keyID, tblItems.txtTex t, tblMultipliers. lngMultiplier & ' of ' & tblItems.lngQua ntity AS txtCopyNumber
            FROM tblItems INNER JOIN tblMultipliers ON tblItems.lngQua ntity >= tblMultipliers. lngMultiplier;
            [/code]

            Regards,
            Fish

            Comment

            • NeoPa
              Recognized Expert Moderator MVP
              • Oct 2006
              • 32669

              #7
              I may be way off base here Fish, but shouldn't that be something more like (I'm working with no meta-data) :
              Code:
              ...
              FROM tblItems INNER JOIN tblMultipliers
                ON tblItems.keyID = tblMultipliers.[SomeIDField]

              Comment

              • FishVal
                Recognized Expert Specialist
                • Jun 2007
                • 2656

                #8
                Ok. I would like to clarify the basics od the proposed solution.
                • [tblMultipliers] is an auxiliary table storing sequential natural numbers {1,2,3,4 ... N}.
                  metadata:
                  Code:
                  Field           Type
                  [lngMultiplier] Number(Long)
                • Table join is used to combine records from [tblMultipliers] where [lngMultip[lier] <= [ ... tblItems field storing required number of copies ... ] with a record from [tblItems].
                  [code=sql]
                  SELECT tblItems.keyID, tblItems.txtTex t, tblMultipliers. lngMultiplier & ' of ' & tblItems.lngQua ntity AS txtCopyNumber
                  FROM tblItems INNER JOIN tblMultipliers ON tblItems.lngQua ntity >= tblMultipliers. lngMultiplier;
                  [/code]


                Regards,
                Fish

                Comment

                • jacc14
                  New Member
                  • Jun 2007
                  • 116

                  #9
                  Dear FishVal

                  I have applied the code and its spot on. Thanks for your help. Much appreciated.

                  Best regards

                  Jacc14

                  Comment

                  • FishVal
                    Recognized Expert Specialist
                    • Jun 2007
                    • 2656

                    #10
                    You are welcome.

                    Best regards,
                    Fish

                    Comment

                    • NeoPa
                      Recognized Expert Moderator MVP
                      • Oct 2006
                      • 32669

                      #11
                      Originally posted by FishVal
                      ...
                      [tblMultipliers] is an auxiliary table storing sequential natural numbers {1,2,3,4 ... N}.
                      metadata:
                      Code:
                      Field           Type
                      [lngMultiplier] Number(Long)
                      ...
                      Ah, that makes more sense now.

                      Comment

                      Working...