Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    qzx999 is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2013
    Posts
    6

    Single Macro Error 3021


    Hi - I've built a form that has a closeWindow macro built into a button. When the button is clicked the form will alert when any required fields are missing, but then the "Macro Single Step" error box will appear with error 3021 - where the user must click "Stop all Macros" to continue. How can I disable this error box, but still have my button and required field checking still enabled??

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    51,500
    Sounds like you need an If Then Else structure.

    I don't use macros, only VBA.

    Provide code or the db for analysis. Follow instructions at bottom of my post.
    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
    qzx999 is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2013
    Posts
    6
    June7-

    Attached is database file. I used if/then statement, but problem is when error is not triggered it still won't execute the "Close Window" command in the Else statement. The record saves, but the window should close on button click if no errors are triggered. I don't know VBA, but can use VBA solution if you suggest. I am just slightly more familiar with macros!

    SP WORKING DEMO VERSION.zip

  4. #4
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    51,500
    Which button?
    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.

  5. #5
    qzx999 is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2013
    Posts
    6
    Sorry, it's the "Save and Close" button. The "Exit (No Save)" has an UndoRecord command, so it doesn't matter if required fields aren't included - the Save and Close button functionality is the only one that I am having problems with.

    Thank you!

  6. #6
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    51,500
    I get the message box with Save and Close even though all fields have data.

    I think you need error handler in the macro using OnError. Review http://office.microsoft.com/en-us/ac...001226457.aspx

    I think that article is showing a regular macro, not embedded. I tried this macro which runs but the GoTo ErrorHandler doesn't trap error. The form closes and record does not save, seems the 3021 error is not triggered by the incomplete record.

    Click image for larger version. 

Name:	Macro.png 
Views:	15 
Size:	9.4 KB 
ID:	12940

    Sorry, can't figure out macro.
    Last edited by June7; 07-03-2013 at 11:13 AM.
    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.

  7. #7
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,663
    Also, it doesn't make sense to have
    Code:
    If Error(3021) then
    This is the error function. "Error(3021)" will return the description (text) of the error number.
    The function ERROR(3021) will return the string "No Current Record".


    I would think the line should be something like:
    Code:
    If ERR.Number = 3021 Then

    I don't use macros, only VBA, so this is just a guess.

  8. #8
    qzx999 is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2013
    Posts
    6
    I think VBA is probably best course to go anyway - but I don't know it well. Is there a simple VBA code that can accomplish the "CloseWindow" command and provide error checking for required fields?

  9. #9
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    51,500
    How did you determine to use error number 3021? I find that number is associated with 'No current record' message. I now suspect there isn't an error that can be trapped. Access refusal to save an initiated record where required fields are not populated is legitimate behavior. This is evidenced by using the X close instead of the command button. (BTW, disable the X close if you don't want users to bypass the command buttons.) Warning that field must have data pops up but no error messages, followed by prompt to choose close without saving or to stay on form. I don't know any way to 'trap' warnings or alerts. Many can be indiscriminately suppressed with SetWarnings method or automatically responded to by code. This is done by placing code in procedure where a specific warning can be expected. As an example, the following will suppress popup warning that records will be deleted from table:

    DoCmd.SetWarnings = False
    DoCmd.RunSQL "DELETE FROM table1"
    DoCmd.SetWarnings = True

    The only approach I am familiar with for assuring required fields are populated is to specifically check the value of each.
    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.

  10. #10
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2013
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,923
    I believe you can catch and suppress all error messages in the OnError event of the form. That's not how I do it but I believe that could work.

  11. #11
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    51,500
    I just tested that.
    Private Sub Form_Error(DataErr As Integer, Response As Integer)
    Debug.Print Err.Number
    Debug.Print DataErr
    End Sub

    Both popups trigger the OnError event. For each Err.Number returns 0 - "No Error".
    However, the DataError variable of the event returns:
    3314 : You must enter a value in the '|' field.
    2169 : You can't save this record at this time @Microsoft Access may have encountered an error while trying to save a record.
    If you close this object now, the data changes you made will be lost.
    Do you want to close the database object anyway?

    Might find this interesting http://support.microsoft.com/kb/268721

    All my attempts to handle the warnings and suppress the first popup fail, however the second popup does not happen. When I use the command button code, the form just closes, the OnError event does not trigger.
    Code:
    Private Sub cmdExit_Click()
    DoCmd.Close
    End Sub
    
    Private Sub Form_Error(DataErr As Integer, Response As Integer)
    Select Case DataErr
        Case 3314
            Response = vbOK
        Case 2169
            Response = vbNo                  
    End Select
    End Sub
    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.

  12. #12
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2013
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,923
    You can use the Response variable to suppress any message if you want.

  13. #13
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    51,500
    This is looking more and more not possible.

    The response argument can have either of two values:
    acDataErrContinue: Ignore the error and continue without displaying the Microsoft Access error message.
    acDataErrDisplay: Display the Microsoft Access error message. This is the default setting.

    So my uses of vbOk and vbNo don't do anything.

    The acDataErrContinue will suppress the warning popups but the form closes and edits are lost instead of keeping user on the form to complete entries.

    Appears my earlier comment about automating response to warning popups was not quite on the mark. With only the two options available, there is no way to tailor the response to the specific warning.
    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.

  14. #14
    qzx999 is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2013
    Posts
    6
    Thanks so much for all your help and testing. This request seems like a pretty standard Event need.... is there another type of logic that you would suggest to accomplish this? I just want to save the record, but alert if there are required fields missing.

  15. #15
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2013
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,923
    Most developers do that in the BeforeUpdate event of the Form.

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Macro Single Step Error
    By cfspartan in forum Macros
    Replies: 6
    Last Post: 04-25-2015, 12:05 AM
  2. 3021 Error
    By RonL in forum Programming
    Replies: 6
    Last Post: 05-10-2013, 03:45 PM
  3. Error 3021
    By Marianna_Air in forum Forms
    Replies: 27
    Last Post: 08-20-2012, 01:13 PM
  4. Replies: 8
    Last Post: 05-16-2011, 06:01 PM
  5. Runtime Error 3021
    By paddon in forum Programming
    Replies: 12
    Last Post: 03-14-2011, 12:14 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