Solved

Trying to copy a record with a subform

Posted on 2016-10-27
2
31 Views
Last Modified: 2016-10-27
I have a form, recordsource = tblPricingGrid,  that also has a sub-form, recordsource = tblPricingGridDetail.  It looks like this:

Form - Subform
The top part (light blue) is the main form and the bottom part is a continuous form that the user can enter prices into.  There are always going to be 42 rows in the subform.

My challenge is to use the [Add Record] button to add a new record to the table tblPricingGrid, AND copy just the left hand column in the sub-form to 42 new records in tblPricingGridDetail so that the new record will then appear to the user to enter prices.

The link between the main form and the subform is PricingGridID so the main table gets that next number because it is an autonumber PK field.  But then that autogenerated number needs to be populated into the tblPricingGridDetail in 42 records.

I sure hope this makes sense.  Its a little difficult to explain.
0
Comment
Question by:SteveL13
2 Comments
 
LVL 35

Accepted Solution

by:
PatHartman earned 500 total points
ID: 41862503
Here's an example from one of my apps.
This is the code behind "Add New Month To All" button.  It takes various arguments from the form and passes those to an append query that selects the  last forecast row from each utility and adds 1 month to create the next forecast month.  The forecast amount is carried forward from the previous month and can then be updated manually if needed.
CopyData.JPG
Private Sub cmdAddToAll_Click()
    Dim LastForecastDate    As Date
    Dim NewForecastDate     As Date
    Dim NewForecastMonth    As String
    Dim db                  As DAO.Database
    Dim qd                  As DAO.QueryDef
    Dim CountAffected       As Long
    
    If Me.cboRateGroup & "" = "" Then
        LastForecastDate = Nz(DMax("ForecastDate", "tblNewMeterForecastByMonth"), #12/31/1899#)
    Else
        LastForecastDate = Nz(DMax("ForecastDate", "tblNewMeterForecastByMonth", "RateGroup = '" & Me.cboRateGroup & "'"), #12/31/1899#)
    End If
    NewForecastDate = DateAdd("m", 1, LastForecastDate)
    NewForecastDate = LstDayNextMnth(LastForecastDate)
    NewForecastMonth = Year(NewForecastDate) & "/" & Format(Month(NewForecastDate), "00")
     
    Set db = CurrentDb
    Set qd = db.QueryDefs!qAddNewMonthForEveryUtilityForecast
        qd.Parameters!EnterForecastDate = LastForecastDate
        qd.Parameters!EnterNewForecastDate = NewForecastDate
        qd.Parameters!EnterNewForecastMonth = NewForecastMonth
        qd.Parameters!EnterRateGroup = Me.cboRateGroup
    qd.Execute
    CountAffected = qd.RecordsAffected
    MsgBox CountAffected & " records were added.", vbOKOnly
    Me.sfrmNewMeterForecastByMonth.Requery
End Sub

Open in new window

Query
INSERT INTO tblNewMeterForecastByMonth ( FPNAForecastUtilityAbrev, RateGroup, ForecastMonth, MeterCount, MeterCountPerDay, ForecastDate )
SELECT tblNewMeterForecastByMonth.[FPNAForecastUtilityAbrev], tblNewMeterForecastByMonth.RateGroup, [EnterNewForecastMonth] AS Expr2, tblNewMeterForecastByMonth.MeterCount, [MeterCount]/Day([EnterNewForecastDate]) AS Expr3, [EnterNewForecastDate] AS Expr1
FROM tblNewMeterForecastByMonth
WHERE (tblNewMeterForecastByMonth.RateGroup =[EnterRateGroup] OR [EnterRateGroup] Is Null)  AND (tblNewMeterForecastByMonth.ForecastDate =[EnterForecastDate]);

Open in new window

0
 

Author Closing Comment

by:SteveL13
ID: 41862949
Very helpful.  Thanks.
0

Featured Post

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.

778 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question