Results 1 to 3 of 3
  1. #1
    N7925Y is offline Advanced Beginner
    Windows 10 Access 2013 32bit
    Join Date
    Apr 2016
    Posts
    32

    Insert Into from unbound form


    I have an unbound form with fields that math up to several tables. I am using this form to add new records. I have a button that will save the new data. I have tried to write a code to run of the button. Having major issues with the syntax. Any help on fixing this would be greatly appreciated.

    Private Sub save_Click()
    CurrentDb.Execute "INSERT INTO tbl1Contacts FirstName, LastName"
    SELECT FirstName, LastName
    From frmNewContacts
    INSERT INTO tbl1Company CompanyName
    SELECT CompanyName
    From frmNewContacts
    End Sub

  2. #2
    ssanfu is offline Master of Nothing
    Windows XP Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Because you are using an unbound form, the syntax is a little different.

    Table "tbl1Contacts" MUST have field names "FirstName" and "LastName".
    Table "tbl1Company" MUST have a field name "CompanyName".

    In the code, change the blue text to your control names that are on the form.
    Code:
    Private Sub save_Click()
        Dim sSQL As String
    
        'Append contact name
        sSQL = "INSERT INTO tbl1Contacts (FirstName, LastName)"
        sSQL = sSQL & " VALUES ('" & Me.txtFirstName & "', '" & Me.txtLastName & "');"
        CurrentDb.Execute sSQL, dbFailOnError
    
        'Append Company name
        sSQL = "INSERT INTO tbl1Company (CompanyName)"
        sSQL = sSQL & " VALUES ('" & Me.CompanyName & "');"
        CurrentDb.Execute sSQL, dbFailOnError
    
    End Sub

  3. #3
    N7925Y is offline Advanced Beginner
    Windows 10 Access 2013 32bit
    Join Date
    Apr 2016
    Posts
    32
    Steve,

    Your code worked perfect. Thank You!!!

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 9
    Last Post: 11-06-2014, 01:15 PM
  2. Replies: 4
    Last Post: 08-01-2014, 09:20 AM
  3. Replies: 14
    Last Post: 05-13-2013, 04:27 PM
  4. How to Insert an unbound Form into table
    By fekrinejat in forum Forms
    Replies: 4
    Last Post: 02-04-2013, 10:34 PM
  5. Replies: 2
    Last Post: 05-18-2012, 02:12 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums