Solved

Trying to copy a record with a subform

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

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Most if not all databases provide tools to filter data; even simple mail-merge programs might offer basic filtering capabilities. This is so important that, although Access has many built-in features to help the user in this task, developers often n…
Today's users almost expect this to happen in all search boxes. After all, if their favourite search engine juggles with tens of thousand keywords while they type, and suggests matching phrases on the fly, why shouldn't they expect the same from you…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

758 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now