title: How to use VBA to add new record in MS Access?
body: <p>I'm using bound forms for the user to update information on new or existing customers. Right now I'm using a Add New Record macro on the submit button (because I'm not sure how to add or save a new record through VBA). </p>
  
  <p>I added a before update event (using VBA) to have the user confirm they want to save changes before exiting the form. For some reason this is overriding the add record button and now users cannot add new record until exiting the forms.</p>
  
  <p>How can I use VBA to add new customer information to the correct table? Is this something that should be done with macros instead?</p>
  
  <p>Form BeforeUpdate Code:</p>
  
  <pre><code>Private Sub Form_BeforeUpdate(Cancel As Integer) 
  Dim strmsg As String 
  strmsg = "Data has been changed." 
  strmsg = strmsg &amp; " Save this record?" 
  If MsgBox(strmsg, vbYesNo, "") = vbNo Then 
      DoCmd.RunCommand acCmdUndo 
  Else 
  End If 
  End Sub
  </code></pre>
  
  <p>Add Record Button:</p>
  
  <pre><code>Private Sub btnAddRecord_Click() 
  Dim tblCustomers As DAO.Recordset 
  
  Set tblCustomers = CurrentDb.OpenRecordset("SELECT * FROM [tblCustomers]") 
  tblCustomers.AddNew 
  tblCustomers![Customer_ID] = Me.txtCustomerID.Value 
  tblCustomers![CustomerName] = Me.txtCustomerName.Value 
  tblCustomers![CustomerAddressLine1] = Me.txtCustomerAddressLine1.Value 
  tblCustomers![City] = Me.txtCity.Value 
  tblCustomers![Zip] = Me.txtZip.Value 
  tblCustomers.Update 
  tblCustomers.Close 
  Set tblCustomers = Nothing 
  DoCmd.Close 
  End Sub
  </code></pre>