Results 1 to 4 of 4
  1. #1
    mp3909 is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Feb 2018
    Posts
    154

    Adding a new record to a recordset

    I am trying to add a new record to a recordset using the code below:



    Code:
    Public Sub formatExcelReports()
    Dim xlApp As Object
    Set xlApp = CreateObject("Excel.Application")
    xlApp.Visible = True
    Dim xlWbk As Object
    Dim ws As Object
    Dim lCol As String
    Dim rs As DAO.Recordset
    Dim sSQL As String
    sSQL = ""
    sSQL = "SELECT DISTINCT * FROM tblFilesToSend"
    Set rs = CurrentDb.OpenRecordset(sSQL)
    th:
    On Error GoTo th
    rs.AddNew           'it always errors out on this line, why?
    rs![File Name] = "Guaranteed_All.xlsx"
    rs![Relationship Office] = "All"
    rs.Update
    but it always errors out on the line rs.addNew.
    I don't understand why?

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,642
    The DISTINCT predicate makes the query read-only.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    mp3909 is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Feb 2018
    Posts
    154
    arrrhhh I see, I was totally unaware of that.
    Thank you for pointing that out.

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,642
    No problem. By the way, if that table is large it can be a performance problem to open it to all records when all you want to do is append. Either use a criteria that returns no records or the append only option.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

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

Similar Threads

  1. Replies: 1
    Last Post: 11-19-2018, 07:57 PM
  2. Replies: 1
    Last Post: 03-07-2014, 11:37 AM
  3. adding new column to recordset
    By pradeep.sands in forum Forms
    Replies: 10
    Last Post: 07-09-2013, 11:53 AM
  4. Replies: 4
    Last Post: 08-14-2012, 07:14 AM
  5. Replies: 14
    Last Post: 05-25-2012, 02:40 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