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?
 
DDBConnect With a Mentor Commented:
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
 
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform)Commented:
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
GRayLCommented:
BTW, you do not need any code at this point.
0
 
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 MVP, Access and Data Platform)Commented:
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
All Courses

From novice to tech pro — start learning today.