Results 1 to 3 of 3
  1. #1
    adams77 is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2013
    Posts
    16

    Update certain table records with VBA code based on query

    The below code is doing exactly what is coded, however, I would like to only update the records that the query is pulling by dates. Is this possible to only update certain records in a table?

    My Code:

    Private Sub Report_Close()
    On Error GoTo ErrorHandler
    Dim sql As String
    Dim rs As ADODB.Recordset

    sql = "SELECT * FROM tblPending WHERE Sent is null"
    Set rs = New ADODB.Recordset

    rs.Open sql, CurrentProject.Connection, adOpenDynamic, adLockOptimistic

    With rs
    If Not .BOF And Not .EOF Then
    .MoveLast
    .MoveFirst
    If .Supports(adUpdate) Then

    Do Until .EOF


    ![DuesStart] = Now() & ![DuesStart]
    .Update
    .MoveNext
    Loop

    End If
    End If

    .Close
    End With

    ExitSub:
    Set rs = Nothing
    Exit Sub

    ErrorHandler:
    Resume ExitSub

    End Sub

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Certainly. What would be the criteria to restrict records? Include that in the recordset filter criteria or a condition in the looping structure.

    Instead of looping a recordset, can do:

    CurrentDb.Execute "UPDATE tablename SET [DuesStart]=Now() & [DuesStart] WHERE Sent Is Null {AND other criteria as needed}"
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726

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

Similar Threads

  1. Replies: 5
    Last Post: 11-29-2014, 10:19 PM
  2. Replies: 13
    Last Post: 11-27-2014, 10:18 AM
  3. Replies: 1
    Last Post: 11-23-2014, 02:20 PM
  4. Replies: 14
    Last Post: 12-06-2012, 11:25 AM
  5. Replies: 6
    Last Post: 05-10-2012, 08:20 PM

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