Results 1 to 4 of 4
  1. #1
    jax1000rr is offline Novice
    Windows XP Access 2007
    Join Date
    Apr 2012
    Posts
    26

    Question Help with running an update query on current recordset

    Hello,



    I know just enough about VBA and queries to get done what I need to, I'm no expert so any help with the below is appreciated.

    I need to enter a record into the table, check for a duplicate record before update and if one is located; I need an update query to run on the new record I am entering via the form.

    The code checks for the duplicate and displays the warning message fine, my disconnect is with the update query. I can't seem to get it to select the current record set.

    VBA and query code as follows:

    VBA
    Code:
    Private Sub Form_BeforeUpdate(Cancel As Integer)
    Dim strMsg As String
    strMsg = strMsg & "Do you want to add loan to master list?"
    If MsgBox(strMsg, vbQuestion + vbYesNo, "Add New Loan") = vbYes Then
       Dim rs As Recordset
       Set rs = CurrentDb.OpenRecordset("MasterList", dbOpenSnapshot)
       rs.FindFirst "LoanNumber = """ & Me![LoanNumber] & """"
       If Not rs.NoMatch Then
          DoCmd.SetWarnings False
          If MsgBox("This loan already exists in the database. Do you want to continue?", vbYesNo, "Duplicate Loan Warning") = vbYes Then
             DoCmd.OpenQuery "Reinstate Query"
          End If
          DoCmd.SetWarnings True
       Else
          DoCmd.RunCommand acCmdUndo
          'For Access 95, use DoMenuItem instead
          'DoCmd.DoMenuItem acFormBar, acEditMenu, acUndo, , acMenuVer70
       End If
    End If
    End Sub
    QUERY

    UPDATE MasterList SET MasterList.ReinstatedLoanAlert = "[text]", MasterList.LoanReinstated = True
    WHERE (((MasterList.LoanNumber)=[Forms]![Add New Loans]![Loan Number]));
    Last edited by June7; 04-12-2012 at 02:55 AM. Reason: add code tags and indentation

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    What do you mean by 'select the current record set'? The query references form for the criteria so it is reading from the record that has focus.

    I always give controls name different from the field they are bound to, like tbxLoanNum. Then refer to the control instead of field name.

    Instead of using OpenQuery and running an Access query object, could build the sql in the VBA code, like:

    CurrentDb.Execute "UPDATE MasterList SET MasterList.ReinstatedLoanAlert = '[text]', MasterList.LoanReinstated = True WHERE MasterList.LoanNumber='" & Me.tbxLoanNum & "'"

    If LoanNumber is not text datatype, remove the apostrophe delimiters.

    You want field to be updated with the string '[text]' - the word 'text' and brackets?
    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
    jax1000rr is offline Novice
    Windows XP Access 2007
    Join Date
    Apr 2012
    Posts
    26
    Thanks much for the info.

    I need to add one more criteria to the WHERE in the SQL statement:

    DateOpen=[Todays Date]

    Can you assist with that?

    Thanks again.

  4. #4
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    [Todays Date] is the current real-time date? Try:

    ... & Me.tbxLoanNum & "' AND DateOpen=#" & Date() & "#"
    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.

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

Similar Threads

  1. Update Query?? or RecordSet??
    By bbrazeau in forum Queries
    Replies: 2
    Last Post: 01-13-2012, 08:44 AM
  2. Running an Update Query from a Macro
    By michaelb in forum Queries
    Replies: 7
    Last Post: 06-02-2011, 09:46 PM
  3. Running an update query
    By markod in forum Queries
    Replies: 3
    Last Post: 11-11-2010, 01:24 PM
  4. Run Update Query on Current Record in Form
    By c3pse in forum Queries
    Replies: 3
    Last Post: 08-14-2010, 05:40 AM
  5. Help Automatically running and Update Query
    By JohnRandolphSTL in forum Queries
    Replies: 8
    Last Post: 04-13-2010, 02:08 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