Results 1 to 12 of 12
  1. #1
    DarkWolff is offline Novice
    Windows XP Access 2007
    Join Date
    Feb 2012
    Posts
    29

    Error Handling & Append Queries

    I'm trying to get an append query to throw an error when it encounters a key violation, but Access doesn't seem to be obliging.



    I have a form that allows a user to add items from one subform to another, which is done with an append query. I've disabled warnings for the operation (by request), so when a duplicate entry is run I want add a prompt. However the built-in error handling from Access doesn't seem to consider this an error as no errors are being thrown at all when this happens.

    Is this possible?

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,598
    Since errors are turned off, I suspect the record just won't be written even though the error message is not presented. Suggest you validate the user entry before running append.
    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
    DarkWolff is offline Novice
    Windows XP Access 2007
    Join Date
    Feb 2012
    Posts
    29
    You are correct, the record won't be written (which is correct). However I want to be able to create a message box that informs the user why it wasn't written. I just can't seem to get it to trap the error (because Access isn't seeing it as an error). This is true regardless if I hide the warnings or not.

  4. #4
    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,847
    A Form has an Error event.
    Here is info from Access 2003 Help.
    You could check your Acc2007 Help for Form Error Event for details.
    Error Event

    See AlsoApplies ToExampleSpecifics
    The Error event occurs when a run-time error is produced in Microsoft Access when a form or report has the focus.
    Private Sub object_Error(DataErr As Integer, Response As Integer)
    Object The name of a Form or Report.
    DataErr The error code returned by the Err object when an error occurs. You can use the DataErr argument with the Error function to map the number to the corresponding error message.
    Response The setting determines whether or not an error message is displayed. The Response argument can be one of the following intrinsic constants.
    Constant Description
    acDataErrContinue Ignore the error and continue without displaying the default Microsoft Access error message. You can supply a custom error message in place of the default error message.
    acDataErrDisplay (Default) Display the default Microsoft Access error message.
    Remarks

    This includes Microsoft Jet database engine errors, but not run-time errors in Visual Basic or errors from ADO.
    To run a macro or event procedure when this event occurs, set the OnError property to the name of the macro or to [Event Procedure].
    By running an event procedure or a macro when an Error event occurs, you can intercept a Microsoft Access error message and display a custom message that conveys a more specific meaning for your application.
    Example

    The following example shows how you can replace a default error message with a custom error message. When Microsoft Access returns an error message indicating it has found a duplicate key (error code 3022), this event procedure displays a message that gives more application-specific information to users.
    To try the example, add the following event procedure to a form that is based on a table with a unique employee ID number as the key for each record.

    Code:
    Private Sub Form_Error(DataErr As Integer, Response As Integer)
         Const conDuplicateKey = 3022
         Dim strMsg As String
          If DataErr = conDuplicateKey Then
             Response = acDataErrContinue
             strMsg = "Each employee record must have a unique " _
                 & "employee ID number. Please recheck your data."
             MsgBox strMsg
         End If
    End Sub


    I'm not sure what happens if you turn Warnings Off.
    Hope it's helpful.

  5. #5
    DarkWolff is offline Novice
    Windows XP Access 2007
    Join Date
    Feb 2012
    Posts
    29
    Thanks for the reply. The problem is the there is no error being thrown when the append query finds a duplicate record, regardless of whether or not the warnings are on or not. The query only adds appends one record based on the user's choice from another subform, so if the query fails it's fine if it just stops.

    I tried using both the main form and the subform's error events, but nothing happens as Access is not considering this an error. I also read up on the Execute method, as that seems to have more reportable error controls but I can't see to get it to work with my saved query.

  6. #6
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,640
    Just an untested shot in the dark, but you might try getting rid of the SetWarnings lines and use

    CurrentDb.Execute "YourQueryHere", dbFailOnError

    to run the query. The execute method doesn't throw the warnings, which is why you don't need the SetWarning lines, and it's possible that the dbFailOnError argument will tell you about the duplicates. Like I said, untested, so no warranty expressed or implied.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  7. #7
    DarkWolff is offline Novice
    Windows XP Access 2007
    Join Date
    Feb 2012
    Posts
    29
    Thanks for the post. When I try running the command with the Execute method (using a valid entry) I get:
    Run-Time error '3061":
    Too few parameters. Expected 4.

    The code I used was: CurrentDb.Execute "APP_NY_TS_Add_Monthly", dbFailOnError

    Using a DoCmd.OpenQuery causes the append query to work fine. The query pulls values a couple values from the subforms (textboxes), does that change anything?

  8. #8
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,640
    Yes, the Execute method can't resolve the form references. One work around is to wrap them in the Eval() function:

    Eval('Forms!FormName.ControlName')
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  9. #9
    DarkWolff is offline Novice
    Windows XP Access 2007
    Join Date
    Feb 2012
    Posts
    29
    I now have everything running and am able to trap the error. Thanks so much for your help!

  10. #10
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,640
    I'm curious, did the Execute method work out? I wasn't really sure if it would consider that an error, though I suspected it would.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  11. #11
    DarkWolff is offline Novice
    Windows XP Access 2007
    Join Date
    Feb 2012
    Posts
    29
    Yes, the Execute method worked when using the Eval workaround. If it tried to append with a duplicate key, Access threw error 3022, which is exactly what I wanted it to. Thanks so much!

  12. #12
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,640
    Excellent! Happy to help.
    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. Error 2501 displays with Error handling
    By agent- in forum Programming
    Replies: 13
    Last Post: 08-05-2011, 02:20 PM
  2. Error Handling
    By jgelpi16 in forum Programming
    Replies: 4
    Last Post: 09-14-2010, 12:17 PM
  3. Error Handling
    By trb5016 in forum Access
    Replies: 2
    Last Post: 08-10-2010, 08:37 AM
  4. #error handling
    By mws5872 in forum Access
    Replies: 4
    Last Post: 05-12-2010, 07:06 AM
  5. Handling dates in queries
    By mrk68 in forum Access
    Replies: 4
    Last Post: 03-23-2009, 06:35 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