Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Trying to copy a record with a subform

Posted on 2016-10-27
2
Medium Priority
?
54 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 40

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

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

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

Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
Microsoft Access has a limit of 255 columns in a single table; SQL Server allows tables with over 255 columns, but reading that data is not necessarily simple.  The final solution for this task involved creating a custom text parser and then reading…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
Suggested Courses

824 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