Results 1 to 3 of 3
  1. #1
    rcerda is offline Novice
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    18

    VBA to add new record to a Table from a Form

    I have a Form which updates the Customer table and views the Invoicing done to each customer. In the same form I want to be able to add new records but using the vba as follows:

    Private Sub btnAddRecord_Click()
    Dim tblClientesausensi As DAO.Recordset

    Set rstClientesausensi = CurrentDb.OpenRecordset("SELECT * FROM [Clientesausensi]")
    rstClientesausensi.AddNew
    rstClientesausensi![IdCliente] = Me.IdCliente.Value
    rstClientesausensi![Empresa] = Me.Empresa.Value
    rstClientesausensi![Rut] = Me.Rut.Value
    rstClientesausensi![Giro] = Me.Giro.Value
    rstClientesausensi![Dirección] = Me.Dirección.Value
    rstClientesausensi![Comuna] = Me.Comuna.Value
    rstClientesausensi![Ciudad] = Me.Ciudad.Value
    rstClientesausensi![Telefono] = Me.Telefono.Value
    rstClientesausensi![Fax] = Me.Fax.Value
    rstClientesausensi![Email] = Me.Email.Value
    rstClientesausensi.Update
    rstClientesausensi.Close
    Set rstClientesausensi = Nothing
    DoCmd.Close
    End Sub



    However, it complaints about duplicate records(error 3022: it says that it would create duplicate records if allowed to process, so it advice to remove the key value from the table, which I do not think is the right approach), which I imagine is the code I have to put prior to the one that is her correct ??. Could anybody guide me on this ?.

    Thank you so much.

  2. #2
    ranman256's Avatar
    ranman256 is online now VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    Normally you don't copy the ID. That would cause a duplicate.
    none of this code is needed....just run an append query.

  3. #3
    ssanfu is offline Master of Nothing
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    It would appear that the field "Clientesausensi.IdCliente" is a primary key )PK) field.
    Is it also an Autonumber type field?

    If it is an Autonumber type field, don't have that line in the code.

    If it is the PK field, but NOT an Autonumber type field, you need to increment it yourself.


    Without knowing more about your table design, I cannot offer specific code changes.

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

Similar Threads

  1. Replies: 3
    Last Post: 11-01-2013, 10:16 AM
  2. Replies: 1
    Last Post: 06-24-2013, 11:31 AM
  3. Replies: 3
    Last Post: 03-09-2013, 10:39 AM
  4. Replies: 3
    Last Post: 02-06-2013, 07:23 PM
  5. Replies: 4
    Last Post: 08-14-2012, 07:14 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