Results 1 to 11 of 11
  1. #1
    markarmer is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Mar 2011
    Posts
    14

    Need help with multiple duplicate records from a form button

    I have two tables, one named tlkpBatch, the other named Animals.



    When I buy new chickens I use a form with two text boxes called txtBatchName and txtQTY - In the batch name I type in a value such as 201110419-WhiteLayers (the numbers are the date, then a simple description), and in the txtQTY box I type in the number of birds that are in this batch.

    I also have a form for the animal table where I type in information about the individual birds, this has multiple fields such as the hatch date, breed & vendor etc... and use a combobox which pulls from the tlkpBatch table so I can assign the specific animal to that batch.

    Finally, at the bottom of this page I created a command button named cmdDuplicate so when I have entered the information for one of the new animals I can click that and it duplicates the record using a macro from the button wizard in Access 2010.

    This all works perfectly and does exactly what I need HOWEVER, often my batches are of 100 to 200 birds, so sitting there clicking the button 200 times gets a little tedious.

    Is there any way to automate this so clicking the button once will automatically create the right number of duplicates based on the QTY in my tlkpBatch table?

    A real life example being I received 75 chickens yesterday, created my batch name & put 75 in the QTY field, saved it then opened up my animals form, created a new record with all of the information about one of the specific animals in then sat and clicked the duplicate button 74 times to get my birds entered, it would be great to have the button take my QTY of 75, know I just created one record and duplicate it 74 times.

    Thank You very much in advance for anyone willing to help with this.

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    One option is this type of thing:

    http://www.baldyweb.com/MultiselectAppend.htm

    but instead of looping a listbox you use a For/Next loop that takes the upper number from your quantity textbox.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    jzwp11 is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2010
    Location
    Dayton, OH
    Posts
    2,901
    If within a batch there are many birds that have the same parameters (hatch date, breed & vendor ) then you should have groups within a batch and have 1 record in that table that defines that group. Then from there have your specific birds in a related table. How you define a "group" depends on your application. You might choose to define a group by breed or by hatch date or by vendor; you'll have to decide.


    tblBatch
    -pkBatchID primary key, autonumber
    -BatchNo
    -dteBatch (I would have a separate date field rather than combining it in the batch no, you can always combine the date with whatever you have in the batchno field to display something like this 201110419-WhiteLayers; having a separate date field will allow for easier searches)

    I assumed, just for illustration purposes, to separate a batch by breed since that seem logical to me.

    tblBatchBreeds
    -pkBatchBreedID primary key, autonumber
    -fkBatchID foreign key to tblBatch
    -fkBreedID foreign key to a table that holds all possible breeds

    tblIndividuals
    -pkIndID primary key, autonumber
    -fkBatchBreedID foreign key to tblBatchBreeds
    -IndvIDNo
    -dteHatch (hatch date)

    As to the vendor, does 1 vendor supply the entire batch? If, so then you should have a vendor field in the tblBatch. If multiple vendors are involved, then it will depend on how the vendors typically supply the batch (by breed?).

  4. #4
    markarmer is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Mar 2011
    Posts
    14
    That does look like what I need to do - do you have a minute to show how I would edit the code? I just tried hacking it up to apply to my form but made no progress

  5. #5
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    What have you got so far?
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  6. #6
    markarmer is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Mar 2011
    Posts
    14
    Well, as you will see I am a complete newbie and am still getting my head around this, in fact, this is the first database I have ever built... here is what I have so far based on your link...

    Dim strSQL As String
    Dim db As DAO.Database
    Dim rs As DAO.Recordset
    Dim ctl As Control
    Dim varItem As Variant
    On Error GoTo ErrorHandler
    Set db = CurrentDb()
    Set rs = db.OpenRecordset("Animals", dbOpenDynaset, dbAppendOnly)
    'add selected value(s) to table
    Set ctl = Me.QTY
    For Each varItem In ctl.txtQTY
    rs.AddNew
    rs!Animals_ID = ctl.ItemData(varItem)
    rs!Animal_Batch = Me.txtBatch
    rs.Update
    Next varItem
    ExitHandler:
    Set rs = Nothing
    Set db = Nothing
    Exit Sub
    ErrorHandler:
    Select Case Err
    Case Else
    MsgBox Err.Description
    DoCmd.Hourglass False
    Resume ExitHandler
    End Select

  7. #7
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Well, you're treating the textbox like a listbox. You would replace the parts related to the listbox and use a For/Next loop based on the textbox. Relevant changes include:

    Dim x As Integer

    For x = 1 To Me.txtQTY
    'code to add a record here
    Next x
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  8. #8
    markarmer is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Mar 2011
    Posts
    14
    Ahh.. Almost there now - It is working in that now clicking the button creates the correct number of records, the only problem is that they are all empty records not duplicates. Here is what I have for the button code now:

    Private Sub addbatch_Click()
    Dim strSQL As String
    Dim db As DAO.Database
    Dim rs As DAO.Recordset
    Dim ctl As Control
    Dim varItem As Variant
    Dim x As Integer
    On Error GoTo ErrorHandler
    Set db = CurrentDb()
    Set rs = db.OpenRecordset("Animals", dbOpenDynaset, dbAppendOnly)
    'add selected value(s) to table
    Set ctl = Me.txtQTY
    For x = 1 To Me.txtQTY
    rs.AddNew
    rs.Update
    Next x
    ExitHandler:
    Set rs = Nothing
    Set db = Nothing
    Exit Sub
    ErrorHandler:
    Select Case Err
    Case Else
    MsgBox Err.Description
    DoCmd.Hourglass False
    Resume ExitHandler
    End Select
    End Sub

  9. #9
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    In between these lines:

    rs.AddNew
    rs.Update

    you need lines that add values to fields, like you had before:

    rs!Animal_Batch = Me.txtBatch
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  10. #10
    markarmer is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Mar 2011
    Posts
    14

    Solved!

    Quote Originally Posted by pbaldy View Post
    In between these lines:

    rs.AddNew
    rs.Update

    you need lines that add values to fields, like you had before:

    rs!Animal_Batch = Me.txtBatch

    That Was It! - Thank you so VERY much, you have save me hours of tedious work.

    Mark

  11. #11
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Happy to help Mark!
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

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

Similar Threads

  1. Replies: 2
    Last Post: 02-12-2011, 09:54 PM
  2. Replies: 4
    Last Post: 12-13-2010, 05:33 PM
  3. Replies: 0
    Last Post: 07-26-2010, 04:36 PM
  4. Button on form to delete all records
    By bbylls in forum Forms
    Replies: 2
    Last Post: 12-08-2009, 12:38 PM
  5. Duplicate command button
    By brettg in forum Database Design
    Replies: 1
    Last Post: 08-04-2008, 04:16 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