Solved

Populate Subform table

Posted on 2014-04-07
4
519 Views
Last Modified: 2014-04-07
Hi,

I have a form with a subform (table) on.  The main form is unbound.  

I am using the subform (table) to allow the user to enter data, however, I need to auto populate two fields on the table everytime a new record is created.  I have the following code on the subform but nothing is happening:

Private Sub Form_BeforeInsert(Cancel As Integer)
Dim rst As DAO.Recordset

Set rst = CurrentDb.OpenRecordset("tbl_reference_data", dbOpenTable)
rst.AddNew
rst!customer_id = Me.Parent!txt_customer_id
rst!batch_id = Me.Parent!txt_batch_id
rst!date_stamp = Now()
rst.Update
'rst.Close
Set rst = Nothing

End Sub

Open in new window

0
Comment
Question by:anthonytr
  • 2
  • 2
4 Comments
 
LVL 12

Expert Comment

by:pdebaets
ID: 39983974
Try setting the Link Child Fields subform property to "customer_id, batch_id" (without the quotes), and do the same for Link Parent Fields. Then you don't need the code you have posted above.

You may need to set the Link Parent Fields property to "txt_customer_id, txt_batch_id", but try "customer_id, batch_id" first.
0
 

Author Comment

by:anthonytr
ID: 39983982
The forms are unbound, so I can't use LinkChildFields
0
 
LVL 12

Accepted Solution

by:
pdebaets earned 500 total points
ID: 39984004
You may want to consider binding your parent form. It can make life much easier.

Another option is to set the default value of your subform customer_id and batch_id controls. You could set them to

=forms!MyParentFormName!txt_customer_id

and

=forms!MyParentFormName!txt_batch_id

... respectively.
0
 

Author Closing Comment

by:anthonytr
ID: 39984020
Thank you for your assistance - this has done the trick
0

Featured Post

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Suggested Solutions

When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

772 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