Share via

Duplicating sub form records

Anonymous
2019-05-16T09:33:21+00:00

Hi,

I am trying to make a database for my dress designing business. There is a main order form and a few subforms linked with Order ID. It sometimes become very hard for me to record similar order details multiple times. I was thinking if there is an option which could create a duplicate of record on sub-forms (3) and assign them to a new order.

I have some understanding of VBA and macros. Any help will really be appreciable. 

Thanks

Microsoft 365 and Office | Access | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Anonymous
2019-05-17T16:06:01+00:00

You've done much as I expected, and approached the project in a spreadsheet manner, rather than as a relational database.  In the latter each table models an entity type, and each column in the table models an attribute which is functionally determined solely by the while of the key. Each table will generally have relatively few columns, but many rows.

I suspect you'll probably feel that you are in too deep for the radical redesign which would be involved in changing the database to one which conforms to the principles of the database relational model, but essentially what you'd have would be a table of orders, a table of garment types, and a table of garment features, for want of a better word, as values in columns, not as column headings.  The order specification would be a many-to-many relationship type between these tables, which would be modelled by a further table which resolves the relationship type into three one-to-many relationship types.  Non-key columns in this table represent the attributes of the relationship type, e.g. with the example of collars which you've posted, a value such as 'semi-spread collar' would be an attribute value of the ternary relationship type between garment type shirt, feature collar and the order in question.

Moreover, the above is a simplification of the model.  It would also contain other relationship types modelled by further tables to enforce constraints between the entity types, e.g. trousers don't have collars, so a further table would model the relationship type between trousers and features and would define those features pertinent to trousers, and similarly between coats/shirts/vests, and other features.  The nearest example I can give you to this is Relationships.zip in the same OneDrive folder to which gave you a link.  This uses a simplified medical prescriptions model as its example, to illustrate how relationships are built up across a database to achieve a robust model.

Recasting the database into a correct model would be a major undertaking, and my guess is that you won't want to go down that road.  You can of course still do the same as in my CopyTree demo, but you'd have to assign values for each column in the tables.  While the code would therefore be 'bulky', writing it is a one-off exercise, and its execution should still be efficient.

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

Answer accepted by question author

Anonymous
2019-05-16T16:34:22+00:00

A sales or purchase order is normally modelled by a many-to-many relationship type between Orders and Products tables or similar, with the relationship type being modelled by an OrderDetails table or similar.  To duplicate the order therefore requires a new row to be inserted into Orders, followed by the insertion of new rows into OrderDetails with the OrderID foreign key column now referencing the primary key of the newly inserted row in Orders.

You'll find an example of this in CopyTree.zip in my public databases folder at:

https://onedrive.live.com/?cid=44CC60D7FEA42912&id=44CC60D7FEA42912!169

Note that if you are using an earlier version of Access you might find that the colour of some form objects such as buttons shows incorrectly and you will need to  amend the form design accordingly.

This little demo file illustrates the copying of a tree structured hierarchy, and of a many-to-many relationship type.  The latter is appropriate to what you are attempting.  The code is in the following CopyMTMRel function:

Public Function CopyMTMRel(lngOrderID As Long, lngCustomerID As Long)

    Dim rst As DAO.Recordset

    Dim strSQL As String

    Dim lngNextOrderID As Long

    Dim lngItemID As Long

    Dim intQuantity As Integer

    Dim curCurrentUnitPrice As Currency

    lngNextOrderID = DMax("OrderID", "Orders") + 1

    ' insert new row into orders with today's date as order date

    strSQL = "INSERT INTO Orders(OrderID,CustomerID,OrderDate) " & _

        "VALUES(" & lngNextOrderID & "," & lngCustomerID & ",#" & _

        Format(VBA.Date, "yyyy-mm-dd") & "#)"

    CurrentDb.Execute strSQL, dbFailOnError

    'return recordset of current order's rows in OrderDetails

    ' and loop through recordset, inserting a new row

    ' at each iteration of the loop, with current unit price of item

    strSQL = "SELECT * FROM OrderDetails" & _

        " WHERE OrderID = " & lngOrderID

    Set rst = CurrentDb.OpenRecordset(strSQL)

    With rst

        If Not (.BOF And .EOF) Then

            .MoveLast

            .MoveFirst

            Do While Not .EOF

                lngItemID = .Fields("ItemID")

                intQuantity = .Fields("Quantity")

                ' get current unit price of item

                curCurrentUnitPrice = DLookup("UnitPrice", "Items", "ItemID = " & .Fields("ItemID"))

                strSQL = "INSERT INTO OrderDetails(OrderID,ItemID,UnitPrice,Quantity) " & _

                    "VALUES(" & lngNextOrderID & "," & lngItemID & "," & curCurrentUnitPrice & "," & intQuantity & ")"

                CurrentDb.Execute strSQL, dbFailOnError

                .MoveNext

            Loop

        End If

    End With

End Function

In the demo the orders form has only one subform, bound to OrderDetails.  As you have three subforms, you would need to repeat the code which follows the insertion of the new row into Orders three times, changing it each time to loop through a recordset of the relevant rows from the relevant subform's table and insert new rows into the table.

Note how, despite the fact that the primary key of Orders is an autonumber, the next number in sequence is inserted by first calling the DMax function and adding 1 to get the next number.  The same number is then inserted into OrderDetails as the foreign key value.

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

6 additional answers

Sort by: Most helpful
  1. Anonymous
    2019-05-17T11:03:38+00:00

    But unfortunately, in My Database, there are about a hundred fields in the subform. Which will make the code really bulky and another problem is that there are multiple subforms (single form format not continuous). I would really appreciate your help. 

    That makes me very sceptical about the structure of the database.  If you post an image of the database's relationships window, clearly set out so that all tables, the relationships between them, and all columns in each table are clearly visible, then we can comment further.

    Why would you consider 'bulky' code a problem.  If it reflects the valid complexity of the model, then the code would be equally valid.  As I said above, I have my doubts on that score.

    The fact that there are three subforms and that they are in single form view is immaterial.  You'd simply apply the same methodology for each subform's table.

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2019-05-17T10:36:49+00:00

    It's a fantastic solution. And I understood its working as well. But unfortunately, in My Database, there are about a hundred fields in the subform. Which will make the code really bulky and another problem is that there are multiple subforms (single form format not continuous). I would really appreciate your help.

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2019-05-16T12:53:30+00:00

    You can copy a record with VBA code fairly easily. This article should show you how:

    https://support.office.com/en-gb/article/Add-records-to-a-table-by-using-an-append-query-98a5bd66-2190-4243-9638-8ef649cf3596

    Do your dresses all have similar attributes? If so, you might consider making a "default template" for a dress (or for distinct types of dresses). For example, you might have 3 common items for DressA, and 5 for DressB, and so on. If you create templates for these, you could use append queries (defined above) to create your basic template, after which you could enter specific details for each dress.

    Was this answer helpful?

    0 comments No comments