Results 1 to 11 of 11
  1. #1
    mbenton's Avatar
    mbenton is offline Advanced Beginner
    Windows 7 32bit Access 2007
    Join Date
    Jul 2015
    Posts
    73

    Run-time error to trigger a Macro

    Hello, as users navigate through a particular form they often over look a very small detail dealing with time slots. Anyhow the over sight often generates a Run-time error '13' Type mismatch. This happens because they fail to enter a time correctly. I want the Run-time error to trigger a macro that I've created with a message of how to correct the time. I created the macro through the form's properties on "OnError". I thought to create the macro through the specific time field's properties however there is not "OnError" option for the time field, only the form has OnError option. What am I doing incorrectly? It doesn't work, the Run-time error still appears and my macro message is no where to be found. Anyone have suggestions? Is it possible to have Run-time errors trigger another message? Thanks.

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,524
    in the form's error trap, run the macro....

    Code:
    sub txtTime_AfterUpdate()
     on error goto ErrUpd
    
    exit sub
    
    errUpd:
    if err = 13 then docmd.runmacro "mErrorMacro"
    end sub

  3. #3
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 10 Access 2013 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    I would think you could also catch the issue in the control's BeforeUpdate event before the error occurs.

  4. #4
    mbenton's Avatar
    mbenton is offline Advanced Beginner
    Windows 7 32bit Access 2007
    Join Date
    Jul 2015
    Posts
    73
    I placed the code as an Afterupdate event procedure exactly how you wrote it. I also named my macro the same as the one in your code. The Run-time error still showed but not the message in my macro. What do you mean when you say in the form's error trap?

  5. #5
    mbenton's Avatar
    mbenton is offline Advanced Beginner
    Windows 7 32bit Access 2007
    Join Date
    Jul 2015
    Posts
    73
    Also, if I place it in the BeforeUpdate there would be no run-time error to trigger the macro. What would trigger the macro?

  6. #6
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 8 Access 2013
    Join Date
    Aug 2013
    Posts
    7,862
    Sounds like you need to trap the error on the correct event. My vote is OnExit of the control. Or use the before update and use VBA for validation instead of waiting for the err.

  7. #7
    mbenton's Avatar
    mbenton is offline Advanced Beginner
    Windows 7 32bit Access 2007
    Join Date
    Jul 2015
    Posts
    73
    My after update code looks like this:

    Private Sub Form_AfterUpdate()
    On Error GoTo errUpd
    Exit Sub

    errUpd:
    If Err = 13 then docmd.RunMacro "mErrorMacro"
    End Sub

  8. #8
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 8 Access 2013
    Join Date
    Aug 2013
    Posts
    7,862
    Quote Originally Posted by mbenton View Post
    My after update code looks like this:

    Private Sub Form_AfterUpdate()
    On Error GoTo errUpd
    Exit Sub

    errUpd:
    If Err = 13 then docmd.RunMacro "mErrorMacro"
    End Sub
    That's fine, but I am guessing your afterupdate will never see an err 13. This is why some people are suggesting a different event. All of these events for controls fire in a certain order. If the control is set up as a Date, you are depending on the intrinsic validation to through the error. When does the form validate? Maybe the onChange would do it. I think The OnChange would produce funny results. This is why I would try OnExit (it might work for you).

    Personally, I manage data validation using VBA. Maybe an unbound control would be better in this instance.

  9. #9
    mbenton's Avatar
    mbenton is offline Advanced Beginner
    Windows 7 32bit Access 2007
    Join Date
    Jul 2015
    Posts
    73
    The form validates when I click on a command button which runs a series of queries including a distance and time calculation. Are you suggesting that I place the code in the OnExit through that control?

  10. #10
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 8 Access 2013
    Join Date
    Aug 2013
    Posts
    7,862
    Quote Originally Posted by mbenton View Post
    The form validates when I click on a command button which runs a series of queries including a distance and time calculation. Are you suggesting that I place the code in the OnExit through that control?
    No, this information is the missing link. I was wondering how Access was throwing the error and you might want to take a different approach.

    Is it more than one control that is causing the error? Are the controls bound?

    Before running a query, you will want to validate data in each control (whether the control is bound or not). The type mismatch is likely a null or empty where a number or date is needed.

  11. #11
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 8 Access 2013
    Join Date
    Aug 2013
    Posts
    7,862
    @mbenton
    It's time for me to go have wine and tapas. I can check back in on this thread tomorrow.

    The bottom line is you need to make sure the correct data type is in each control before running the query(s).

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

Similar Threads

  1. Replies: 1
    Last Post: 01-13-2015, 01:33 PM
  2. Replies: 1
    Last Post: 10-03-2014, 05:14 PM
  3. Replies: 0
    Last Post: 07-16-2012, 05:42 AM
  4. Replies: 2
    Last Post: 05-05-2012, 02:34 AM
  5. Macro to trigger emailing an updateable table
    By KOIBOY in forum Import/Export Data
    Replies: 5
    Last Post: 09-10-2011, 05:46 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