How write record to table using code

Maybe this can't be done but I am trying to write a record to a table using a temporary table made from a make table query.  But I also want to write a couple of the fields from a forms fields.

I have this code in the after update event of a combobox on the form:

Dim RS As DAO.Recordset
    Set RS = CurrentDb.OpenRecordset("tblUnitLineItems")
    With RS
          .AddNew
          !JobID = Me.txtJobID
          !UnitID = Me.txtUnitID
          !Category = Me.cboCategory
          !LineItem = Me.cboLineItemDescription
          !Quantity = Me.txtQuantity
          !Price = Me.txtPrice
          !MasterItem = Me.chkbxMasterItem
          !LineItemID = Me.txtLineItemID
          .Update
          End With

 RS.Close
 Set RS = Nothing

Open in new window


But I want the record source for the following fields to come from the temporary table:

Category
Quantity
Price
MasterItem
LineItemID
LineItem

But then I want the following fields to come from the form fields:
JobID
UnitID

Can this be done?  How would I re-write the code if so?

--Steve
SteveL13Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

hieloCommented:
Try executing a INSERT INTO SELECT query.  The general syntax is:
INSERT INTO targetTable (targetColumn1, targetColumn2, ..., targetColumnX)
SELECT sourceColumn1, sourceColumn2, ..., sourceColumnX FROM sourceTable;



Also, in general you don't need an actual query to get the SELECT to return rows. To clafify if you execute:
SELECT 12 as age,'John' as firstName

You will get one row with column named age with value=12 as well as a column named firstName with value='John'

Knowing that, you should be able to include the JobId and UnitID to the SELECT clause (see example at the end).

So, if you are trying to insert into "tblUnitLineItems" from "tempTable", backup your db first then try:

CurrentDb.Execute "INSERT INTO tblUnitLineItems(Category,Quantity,Price,MasterItem,LineItemID,LineItem,JobID,UnitID) SELECT Category,Quantity,Price,MasterItem,LineItemID,LineItem," & Me.txtJobID& ","  & Me.txtUnitID & " FROM tempTable"

Open in new window

0
Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
You can also use DAO if you'd prefer. You'd have to get the data from your temporary table first, so you'd open a Recordset for that:

Dim rst As DAO.Recordset
Set rst = Currentdb.OpenRecordset("SELECT * FROM YourTemporaryTable")

Open in new window

Note you may need a WHERE clause on the SELECT statement to get the right record. From there, you'd refer to that "rst" object when needed:
Dim RS As DAO.Recordset
    '/ I changed the line below to not use a table-type recordset
    Set RS = CurrentDb.OpenRecordset("SELECT * FROM tblUnitLineItems WHERE 1=0")
    With RS
          .AddNew
          !JobID = Me.txtJobID
          !UnitID = Me.txtUnitID
          !Category = rst("Category")
          !LineItem = rst("LineItemDescription")
          !Quantity = rst("Quantity")
          !Price = rst("Price")
          !MasterItem = rst("MasterItem")
          !LineItemID = rst("LineItemID")
          .Update
          End With
 RS.Close
 Set RS = Nothing

Open in new window

I'm just guessing at the names of the columns in your Temporary Table, so you may have to change the items like "LineItemDescription" to match the actual names you're using.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.