Add a new record to a subform automatically

G

Guest

I have a main form "Projects" and a subform "WorkOrders".
When I add a new project I need for (3) WorkOrders to be created
automatically.

The WorkOrders subform has a record "WorkOrderType" that queries a table
"WorkOrderTypes" for a list of types. I need to have this record filled in
with a specific entry from this table as well for each "WorkOrder".

How can this be acheived?
 
T

tina

you can append three records to the work orders table when the subform is
entered, in the subform control's Enter event. you have to check to see if
there are any records in the subform before running the append - something
along the lines of

Private Sub SubformControlName_Enter()

If Me!SubformControlName.Form.RecordsetClone.RecordCount < 1 Then
CurrentDb.Execute "INSERT INTO tblWorkOrders " _
& "( ForeignKeyFieldName, WorkOrderType ) " _
& "SELECT " & Me!PrimaryKeyFieldName & ", " _
& "WorkOrderTypeFieldName FROM " _
& "tblWorkOrderTypes", dbFailOnError
Me!SubformControlName.Requery
End If

End Sub

in the code above, you'll have to substitute the correct name of the subform
control *within the main form*, and the correct names of the work order
types table, the work order table, the fields in the work order table and
the work order types table, and the primary key field of the projects table.

note that if the work order types table has more than 3 records in it,
you'll need to add a WHERE clause to the above SQL statement, to specify
which three work order types you want to add to the work orders table.

hth
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top