error handling question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Alireza355
    New Member
    • Feb 2009
    • 86

    error handling question

    Dear all,

    I have an old (2003) access database with tons and tons of codes for different events and everything imaginable...

    I have not used this database for about 10 years now, and now that I went back to it, lots of errors keep popping up because of changes and updates, etc...

    For my error handling, I have:

    Code:
    errorhandler:
    MsgBox "Error: (" & Err.Number & ") " & Err.Description, vbCritical
    So, I get this pop up window, giving me information on the error.

    Is there any way I can have access take me to the exact place in the codes where the error has happened? I mean I want to know what was being done when the error happened.

    Just a heads up: I have a big form opened, maximized, lots and lots of things happening in the background that make it difficult to know what exactly was being done when the error happened.
  • mbizup
    New Member
    • Jun 2015
    • 80

    #2
    If you are getting 'lots of errors', I'd suggest debugging by temporarily disabling custom error handling.

    From the VBA editor, Tools-->Options --> General select the 'Break on all errors' option. With that setting you'll get error popups with a 'Debug' option. Clicking 'Debug' will take you directly to the offending line(s) of code.

    If these errors have been in response to Access/Office version changes (with no code changes), the fix may be as simple as updating your references...

    Once done debugging, be sure to set the error handling option back to 'Break on Unhandled Errors'.

    Comment

    • NeoPa
      Recognized Expert Moderator MVP
      • Oct 2006
      • 32655

      #3
      I would fully support M's advice for this. That is certainly your best course of action.

      However, for completeness, there are options that can be run from an error handler that return you either to the line of code that failed or the one immediately after.

      Check out the Resume statement. On its own it does the former and with the keyword Next it does the latter.

      Let me repeat though, for the situation described you're better off disabling the error handling until you've identified any and all errors in the code.

      Comment

      • Alireza355
        New Member
        • Feb 2009
        • 86

        #4
        Thank you mbizup and neopa.

        I will give it a try...

        Comment

        • Alireza355
          New Member
          • Feb 2009
          • 86

          #5
          ok. the debug is now working and it is taking me to the problem area.

          like I said before, I created this database about 10 years ago, with a lot of help from people like you, and now after 10 years of not doing anything "code!!!!" I have forgotten lots of my knowledge.

          so the error I am getting right now is "no current record" and debug takes me to:

          Code:
          Application.RunCommand acCmdSelectRecord
          I may not have the proper references enabled. I don't even know what references need to be enabled for this line of code to work!!!

          what I intended to do was that based on the current record that is currently highlighted or selected in the subform, a dropdown combobox which gets its items from a query would requery and itemdata(0) would be selected and shown.

          so the code is actually:
          Code:
          Application.RunCommand acCmdSelectRecord
          [Forms]![sanad]![Combo771].Requery
          [Forms]![sanad]![Combo771] = [Forms]![sanad]![Combo771].ItemData(0)
          but now (after all these years and lots of possible changes or updates) the Application.Run Command acCmdSelectReco rd isn't working.

          any ideas???

          Thanks a lot.

          Comment

          • NeoPa
            Recognized Expert Moderator MVP
            • Oct 2006
            • 32655

            #6
            Hi Ali.

            I can't find much on why to use acCmdSelectReco rd but it seems that it helps to select the whole record when attempting any record-level actions.

            If the error message say "No current record" then it seems that is exactly your problem. I've had similar problems when the recordset's been empty. If that is true in your situation then that should explain why you're getting that message. If there are no records available then selecting the current one is never goiung to work.

            Comment

            • Alireza355
              New Member
              • Feb 2009
              • 86

              #7
              Dear Neopa
              thank you for your reply. There ARE records and they are shown in the subform. but I get that error. even when I click on another record on the subform, since the code is on its "on current" event, I get the error again and again

              Comment

              • jforbes
                Recognized Expert Top Contributor
                • Aug 2014
                • 1107

                #8
                If the code is being executed in the OnCurrent event, you may want to wrap it in a test to see if you are actually on a record you can use. I use the following to test to see that there is a valid record number, https://msdn.microsoft.com/en-us/lib.../ff821182.aspx:
                Code:
                If Not Me.CurrentRecord = 0 Then
                    'Do the things that need to be done
                End If

                Comment

                • Alireza355
                  New Member
                  • Feb 2009
                  • 86

                  #9
                  ok. Today, I spend a couple of hours on my database, and I am beginning to remember what I did 10 years ago.

                  The reason why I had Application.Run Command acCmdSelectReco rd set on the "on current" event of the subform was that I wanted the whole record to be highlighted at all times.

                  The reason why it is now giving me errors, I don't know :(

                  I took it away, and the form and subform are working fine, and no errors are being generated.

                  I still appreciate your help.

                  Comment

                  Working...