none
Cannot Step Through VBA Code (F8) in Access 2007 module RRS feed

  • Question

  • I created a new access database (accdb) in Access 2007.

    I created a table w/data and a form with a button on it whose onclick event was defined as:

    Private Sub Command4_Click()
     ID.SetFocus
     MsgBox ("test")
      Field1.SetFocus

    End Sub

    The code runs when the button clicked but when I go to the Form's code module and insert the cursor on the ID.Setfocus line of the click event and click F8, I get an error beep. Likewise if I try to use the "Step Into" option from the Debug window.

    Under Access Options | Current Database I have ensured that "Use special keys" is checked.

    How can I step through my VBA code in the form's code module?

    Saturday, January 14, 2017 10:43 PM

Answers

  • Hi,

    I think Tom's point is if there is parameter, we are unable to step into the code .

    Please visit Error Handling and Debugging Tips for Access 2007, VB, and VBA, you could find:

    Run the Current Procedure

    Highlight the procedure that you want to run and press F5 to run it. If you want to step into it line-by-line, press F8. More explanations on running code line-by-line is given later. Of course, running a procedure this way only works if you don’t have to pass parameters to it. If you need to, consider using the Immediate Window.

    In my opinion, we also could not step into the code for event handlers. The only way is to set a breakpoint. Sorry that I did not find any official documents mentioned now.

    Since the issue is related to debug in VBA, I would move this thread into VBA forum.

    Regards,

    Celeste


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Dave1289 Tuesday, January 17, 2017 2:35 AM
    Monday, January 16, 2017 7:14 AM
    Moderator

All replies

  • At the line ID.SetFocus click on the left vertical tab to get a brown dot - a break point

    run the code by click the Command4 button - it will stop at the brown dot with yellow highlight.

    From there you just step by F8

    Greg


    Saturday, January 14, 2017 11:21 PM
  • That is by design. Form_Open requires a Cancel argument and you're not supplying it.

    It also requires the form to be open, and maybe it wasn't.

    The solution is to set a breakpoint, then open the form as usual.


    -Tom. Microsoft Access MVP

    Sunday, January 15, 2017 2:23 AM
  • Thank you SysGreg and Tom.

    Please forgive my ignorance.  It has been a long time since I've coded.

    I can indeed step through the code when I set a breakpoint.  But has this always been required? 

    If I create a Code Module rather than a form code behind, the F8 and F5 keys work as expected; I step thru each line of code or execute the entire block.  However, in the code behind a form they do not: the F5 invokes the Macro dialog box and F8 just beeps at me.

    I am using Access 2007 with an accdb file on Windows 10.

    Has this always been the case, that the code behind the form object acts differently than that in a code module?

    And is it true statement that in order to step through the code behind my form I must set a breakpoint?

    Thanks for you help on this.

    Dave



    Dave1289

    Monday, January 16, 2017 2:10 AM
  • You did not understand my comment:
    Form_Open requires a Cancel argument and you're not supplying it.

    This has always been this way. You can test this yourself. In a standard module, write:
    public sub test1()
      MsgBox "Hi from test1"
    end sub

    If you put your cursor in this function and hit F5, you can run it, or with F8 you can debug it.

    Second test:
    public sub test2(Cancel as integer)
      MsgBox "Hi from test2"
    end sub

    This one will not work, because you're not supplying the Cancel argument.

    What you *can* do is in the Immediate window:
    test2 True


    -Tom. Microsoft Access MVP

    Monday, January 16, 2017 3:15 AM
  • No.  I don't understand.  

    I was concerned with stepping thru the code associated with the onclick event of a button.

    I don't know why you are referencing the cancel argument of the Form_Open event.

    The form is open in design view. I click on View Code to debug the code  associated with the click event of the button.

    From what I've gathered so far, the only way to do so is to first set a breakpoint for the Click event.

    Is this true?


    Dave1289

    Monday, January 16, 2017 5:47 AM
  • Hi,

    I think Tom's point is if there is parameter, we are unable to step into the code .

    Please visit Error Handling and Debugging Tips for Access 2007, VB, and VBA, you could find:

    Run the Current Procedure

    Highlight the procedure that you want to run and press F5 to run it. If you want to step into it line-by-line, press F8. More explanations on running code line-by-line is given later. Of course, running a procedure this way only works if you don’t have to pass parameters to it. If you need to, consider using the Immediate Window.

    In my opinion, we also could not step into the code for event handlers. The only way is to set a breakpoint. Sorry that I did not find any official documents mentioned now.

    Since the issue is related to debug in VBA, I would move this thread into VBA forum.

    Regards,

    Celeste


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Dave1289 Tuesday, January 17, 2017 2:35 AM
    Monday, January 16, 2017 7:14 AM
    Moderator
  • Thank you Celeste.

    That was the basic information I needed: code modules behave differently than event handler code and the only way to step through the event handler code is to set a breakpoint. 


    Dave1289

    Tuesday, January 17, 2017 2:32 AM