Page 1 of 2 12 LastLast
Results 1 to 15 of 20
  1. #1
    VicM is offline Advanced Beginner
    Windows 7 64bit Access 2010 32bit
    Join Date
    Sep 2017
    Location
    US-PA & FL
    Posts
    55

    Breakpoint before DoCmd.GoToRec , , acNewRec throws run-time error 2046

    Hi,
    I'm running Access 2010 on a Win7 Pro box.



    On one of my forms I have a button to start a new record. When testing the code for the button, I set a breakpoint so I can step thru the lines and check some variables. When I reach the DoCmd.GoToRec , , acNewRec line I get a run-time 2046 error: The command or action 'GoToRecord' isn't available now.

    But if I remove the breakpoint and set one AFTER the DoCmd line, it works. I don't get an error and the form has moved to a blank New Record.

    Is this some sort of bug in the software?

    Thanks,
    Vic

  2. #2
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    I suspect your code is setting a focus somewhere, using screen.activeform or screen.activecontrol, by setting a breakpoint or using Stop the focus is moved to the vb editor.

  3. #3
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows 10 Access 2016
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,544
    Hi Vic

    I have tried but am unable to replicate the problem that you describe.

    Does the same thing happen on other forms?

    Can you post a copy of a db that behaves in this manner
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  4. #4
    VicM is offline Advanced Beginner
    Windows 7 64bit Access 2010 32bit
    Join Date
    Sep 2017
    Location
    US-PA & FL
    Posts
    55
    CJ_London--I don't have anything that's setting the focus. I isolated the form, its table and all the requisite code in an abbreviated DB. When I set a breakpoint somewhere before the DoCmd.GoToRecord , , acNewRec line in the subroutine for the 'New Author' button, I still get the error when stepping thru the code. But if I remove the breakpoint, or set it after the new line instruction, everything works.

    For the benefit of Bob Fitz I'm attaching the abbreviated DB. If you open the frmAuthors form, click on the New Author button, the added author will be posted to the Authors table. But if you navigate to the VBA code window and select the NewAuth_Click() subroutine, you can set a breakpoint anywhere before the DoCmd.GoToRecord line. Then step thru the code. You should get the run-time error when you reach the DoCmd line. At least I did in this abbreviated DB before I attached it here.
    Attached Files Attached Files

  5. #5
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    setting the focus was just a guess since no code had been provided, however still think it is due to the focus moving to the vba window.

    DoCmd.GotoRecord - where?

    Review the documentation https://learn.microsoft.com/en-us/of...cmd.gotorecord

    in particular

    If you leave the ObjectType and ObjectName arguments blank (the default constant, acActiveDataObject, is assumed for ObjectType), the active object is assumed.

    because of your breakpoint, the active data object is the vba window

    try

    DoCmd.GoToRecord acForm, Me.Name, acNewRec

    just as you have for the close code in you btnClose event

    edit: for information, the value of acActiveDataObject is 1, whilst the value for acForm is 2

  6. #6
    Edgar is online now Competent Performer
    Windows 8 Access 2016
    Join Date
    Dec 2022
    Posts
    274
    I don't know if it's a bug, what I do know is that the DoCmd object calls Macro actions and maybe they're are not available when you're stepping through code. If you need to go to a new record without having issues with the DoCmd object, then you could use Me.Recordset.AddNew

  7. #7
    VicM is offline Advanced Beginner
    Windows 7 64bit Access 2010 32bit
    Join Date
    Sep 2017
    Location
    US-PA & FL
    Posts
    55
    Thanks to all of you.
    CJ_London I replaced the code as you suggested and according to the Microsoft documentation on GoToRecord. However this still caused the run-time error.

    I then tried the suggestion by Edgar which is to use Me.Recordset.AddNew in place of the DoCmd method. And lo and behold that actually worked! So the possibility exists as Edgar suggested that macros are involved with the DoCmd method and it somehow negates the method when stepping thru the code.

    But as I've mentioned the DoCmd method works fine when there are no breakpoints in the code. So I'll have to make a note when stepping thru code, if there is a DoCmd.GoToRecord instruction, I'll replace it with the Me.Recordset.AddNew line for testing purposes.

    Thanks to all of you for helping me work thru this. I was beginning to think there was some issue with my copy of the Access application.

    Vic

  8. #8
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    I replaced the code as you suggested and according to the Microsoft documentation on GoToRecord. However this still caused the run-time error.
    I tested in the db you provided and it worked for me. Only change I had to make is turn popup off since it did not appear on my monitors when open. I guess that might make a difference, tho' I don't know why that should be

  9. #9
    VicM is offline Advanced Beginner
    Windows 7 64bit Access 2010 32bit
    Join Date
    Sep 2017
    Location
    US-PA & FL
    Posts
    55
    CJ_London
    WOW! I didn't realize the form was set to PopUp. When I set that to No, the DoCmd instruction worked while stepping thru the code and did not throw the run-time error.
    I agree, that is weird.
    I'll be making a note to myself about this situation.

    Once again, thanks for your input.
    Vic

  10. #10
    VicM is offline Advanced Beginner
    Windows 7 64bit Access 2010 32bit
    Join Date
    Sep 2017
    Location
    US-PA & FL
    Posts
    55
    And here's a PS.

    With the PopUp set to No, and a breakpoint before the DoCmd line again reading: DoCmd.GoToRec , , acNewRec, the step thru worked! So from this it would appear the PopUp property set to Yes somehow interferes with the DoCmd method during step thru of code.
    Go figure.

  11. #11
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    Agree - it appears the popup causes the issue, I can't find any mention in the documentation, but someone asked the same question 16 years ago here
    https://bytes.com/topic/access/answe...ecord-acnewrec

    But the post was moved and I can't find where to so no answer

    If it's a bug it clearly hasn't bothered people for a long time

    I'll ask around...

  12. #12
    VicM is offline Advanced Beginner
    Windows 7 64bit Access 2010 32bit
    Join Date
    Sep 2017
    Location
    US-PA & FL
    Posts
    55
    Amazing you remembered something from 16 yrs ago! I sometimes don't remember what I had for breakfast. LOL

  13. #13
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Office 365
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,977
    Many thanks to @CJ_London for alerting me to this thread

    I've seen variations on this issue over the years where adding breakpoints prevents code running, but until today had never really studied the issues.
    I've just alerted the Access team to this issue but I expect they are already aware as this particular problem has existed since at least A2003 (A2000 is fine / A2002 not tested)

    I created my own test databases to check that the issue wasn't specific to your own db.
    I then added various navigation buttons with code like this

    Code:
    Private Sub cmdLast_Click()    MsgBox "Last" 'add breakpoint
       ' DoCmd.OpenForm Me.Name, acNormal 'disable in testing
        DoCmd.GoToRecord , , acLast
    End Sub
    I then added a breakpoint before the GoToRecord code – in this case on the MsgBox line

    From A2003 onwards, the code errors on the GoToRecord line:
    A365: error 2499 - You can't use the GoToRecord or SearchForRecord action or method on an object in Design view.
    A2003/2007/2010: error 2046 - The command or action GoToRecord isn't available now.
    A2000: No error!

    The breakpoint causes the form to behave as though it is in 'design view'

    FIXES:
    1. Enable the line DoCmd.OpenForm Me.Name, acNormal. No error
    2. Set Popup = No. No error. No need to specify Form view
    3. Use A2000 (a bit extreme, methinks!)

    Repro databases (ACCDB & A2000 MDB) attached
    NOTE: the issue depends on the Access version NOT the file format
    Attached Files Attached Files
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

  14. #14
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Office 365
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,977
    In case the OP is still interested, I have extended my article https://www.isladogs.co.uk/popup-breakpoint-bug/ written in early April
    The updated article now fully explains this behaviour with popup forms.
    Last edited by isladogs; 06-14-2023 at 02:57 PM.
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

  15. #15
    VicM is offline Advanced Beginner
    Windows 7 64bit Access 2010 32bit
    Join Date
    Sep 2017
    Location
    US-PA & FL
    Posts
    55
    isladogs,

    The link for Popup forms with breakpoint before GoToRecord is broken.

    Vic

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

Similar Threads

  1. Replies: 5
    Last Post: 02-04-2023, 11:22 AM
  2. Replies: 3
    Last Post: 01-02-2015, 02:06 PM
  3. Run-time error 2046
    By Evans2 in forum Forms
    Replies: 8
    Last Post: 09-06-2014, 01:52 PM
  4. Replies: 3
    Last Post: 06-09-2012, 08:14 AM
  5. Replies: 5
    Last Post: 03-30-2010, 12:53 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