Insert into to append an access table from a form

I am a novice attempting to appendl a table in access based on values in an open form.  I created a simple table with three fields and a supporting form.  I also created a target table that has the same field names: firstname, lastname and city.  I created a button on a form that shows data from the source table and used the following code in an 'onclick' event procedure:

Private Sub Detail_Click()
Dim strSql As String
strSql = "INSERT INTO Customers([firstname],[lastname],[city]) " SELECT ("'& Forms![Append_Form]![firstname] &'", "'& Forms![Append_Form]![lastname] & '", "' & Forms![Append_Form]![city] & '");"
End Sub

This consistantly fails syntax with entire srtSql line highlighted.  Any help would be greatly appreciated.

Thanks  
AJ
andrewjvaAsked:
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.

DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Systems AnalystCommented:
Try this:

Dim strSql As String
strSql = "INSERT INTO Customers([firstname],[lastname],[city])   SELECT (" & Chr(34) & Forms![Append_Form]![FirstName] & Chr(34) & ", " & Chr(34) & Forms![Append_Form]![LastName] & Chr(34) & ", " & Chr(34) & Forms![Append_Form]![City] & Chr(34) & ");"


mx
0
GRayLCommented:
Why not just bind your form to the table by setting the record source property of the form to your table name.  Then make the control source of each control the appropriate field name for that control.  Give that a try.
0
GRayLCommented:
BTW, you do not need any code at this point.
0
Powerful Yet Easy-to-Use Network Monitoring

Identify excessive bandwidth utilization or unexpected application traffic with SolarWinds Bandwidth Analyzer Pack.

andrewjvaAuthor Commented:
I still get the syntax error.  Here is the procedure as pasted from above:

Private Sub Detail_Click()
Dim strSql As String
strSql = "INSERT INTO Customers([firstname],[lastname],[city]) " SELECT ("'& Forms![Append_Form]![firstname] &'", "'& Forms![Append_Form]![lastname] & '", "' & Forms![Append_Form]![city] & '");"
End Sub

As far as binding to the target table, can I set the record source to the target while loading the form from the source table?
0
andrewjvaAuthor Commented:
BTW this is Access 2007, does that make a difference?
0
andrewjvaAuthor Commented:
OK, I cleaned up the cut and paste for the above code and now there's no error but there is no new record added either?  Help
0
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Systems AnalystCommented:
andrewjva:
Did you try my first post?

mx
0
andrewjvaAuthor Commented:
MX,

I did.  Now there is no error but the new record is not created. I click on the control and nothing is appended to the target table.  Any ideas?

AJ
0
GRayLCommented:
Just so we're clear, please give the name of both tables and state which table is bound to the form.
0
DDBCommented:
I understand that you have form with 3 unbound text boxes. let's say they rae named as tctFirstName, txtLastName, txtCity and you also have Command button (add record)

then:

Private Sub cmdAdd_Click()
On Error GoTo Err_cmdAdd_Click


   Dim strSql As String
   strSql = "INSERT INTO Customers([firstname],[lastname],[city]) SELECT '" & txtFirstName & "','" & txtLastName & "','" & txtCity & "';"
   
   DoCmd.RunSQL strSql
   

Exit_cmdAdd_Click:
    Exit Sub

Err_cmdAdd_Click:
    MsgBox Err.Description
    Resume Exit_cmdAdd_Click
   
End Sub
 

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.