Solved

Trying to copy a record with a subform

Posted on 2016-10-27
2
35 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

792 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