Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Trying to copy a record with a subform

Posted on 2016-10-27
2
Medium Priority
?
52 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 39

Accepted Solution

by:
PatHartman earned 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

718 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