Results 1 to 14 of 14
  1. #1
    chemguy120 is offline Advanced Beginner
    Windows 10 Office 365
    Join Date
    Aug 2017
    Posts
    37

    Message Box Where Yes Advances Form To New Record

    I'm banging my head against the wall over something I thought would be simple! I have a form that that opens to the last record. I then want the "txt_ReferenceDate" field to be checked to see if it matches the current date. If it does the form should open normally to the last record. If it doesn't a yes/no message box should ask if the user wishes to create a new record (Yes) or continue of with the last record (No). I've tried using the following code:

    Code:
    Private Sub Form_Load()
        DoCmd.GoToRecord , , acLast
        
        If Me.txt_ReferenceDate <> Date Then
            If MsgBox("This record is not for the current date. Would you like to create a new record?", vbYesNo, "Warning!") = vbYes Then
                DoCmd.GoToRecord , , acNewRec
            Else
                Exit Sub
            End If
        End If
        
    End Sub
    If I answer no everything works as expected. If I answer yes I get the dreaded 2499 error stating I cannot use the GoToRecord in design view(??). I've tried the code in both the OnLoad and OnCurrent events. I get the same results using the OnCurrent event except the message box comes up twice before either opening the form or giving me the 2499 error depending on the selection.



    I know the user could just manually advance to a new record after opening the form without being prompted. The purpose of the message box is to force the user to think about what they are doing rather than just blindly charging ahead. Thanks for any and all help!

  2. #2
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,803
    The thing about design view is odd indeed. I'm wondering if that going to the last record makes it active, thus you can't go to new. I'm getting a bit rusty.
    I might use a DLookup or DMax (depends on what your data looks like) to get the last record date and compare that to the current date. If they're not the same, do one thing. If they are, do the other. Note that if your table date field values contain time values as well, then I doubt it will ever equal Date(). Default time portion of Date() is 00:00:00 although the function won't show that when it returns a date.
    Last edited by Micron; 04-11-2024 at 03:22 PM. Reason: added info
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  3. #3
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    Code works for me in Load event.

    Could provide db for analysis.
    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.

  4. #4
    chemguy120 is offline Advanced Beginner
    Windows 10 Office 365
    Join Date
    Aug 2017
    Posts
    37
    The DB is a bit complicated to try to remove stuff I would not have the authority to share. I used to just have the form open to the last record then automatically advance to a new record if it wasn't the current date using:

    Code:
    If Me.txt_ReferenceDate <> Date Then DoCmd.GoToRecord , , acNewRec
    This worked well for YEARS up until this Tuesday (patch Tuesday, so not sure if THAT may have played a part). Then suddenly it won't work. I re-created the form from scratch and it worked as it used to for a day, then that code just stopped working.

    If my idea as laid out in the original post won't work then I may just have a standard informational box warn the user the record is not for the current date then they would have to manually advance to a new record. I like the yes/no idea however as it seems more eloquent from an end user standpoint!

  5. #5
    davegri's Avatar
    davegri is online now Excess Access
    Windows 11 Access 2019
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,413
    Just a shot in the dark, but as an alternate you could try

    DoCmd.RunCommand acCmdRecordsGoToLast
    and
    DoCmd.RunCommand acCmdRecordsGoToNew

    And FWIW, you don't need the else exit sub. You're gonna exit sub regardless.

  6. #6
    chemguy120 is offline Advanced Beginner
    Windows 10 Office 365
    Join Date
    Aug 2017
    Posts
    37
    you don't need the else exit sub
    I actually meant to take that out. That was me experimenting trying to figure out what the issue is.

    I get the same results using your code as I do the original except the 2499 error is replaced with 2046 "The command or action 'RecordsGoToNew' isn't available now."

    When removing the message box code, both versions of the code to go to the last record as expected. Could there be an instant after clicking Yes that the form is momentarily not loaded and that is causing an issue?

  7. #7
    chemguy120 is offline Advanced Beginner
    Windows 10 Office 365
    Join Date
    Aug 2017
    Posts
    37
    Also, if it is worth mentioning, after the error is triggered, if I choose to end the dialog rather than go into the debugger, the form opens to the last record as expected for the No response.

  8. #8
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 11 Office 365
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,654
    Is the forms allow additions property set to yes?
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  9. #9
    chemguy120 is offline Advanced Beginner
    Windows 10 Office 365
    Join Date
    Aug 2017
    Posts
    37
    Sorry for responding late, it's been a busy week.

    Is the forms allow additions property set to yes?
    The additions property is set to yes.

    The "New Record" button on the form works to advance to a new record. The button uses the following code (in case it may be relevant):

    Code:
    Private Sub btn_NewRecord_Click()
        DoCmd.GoToRecord , , acNewRec
        Me.txt_ReferenceDate.SetFocus
        
    End Sub
    It would be nice if the Yes option in an initial message box would do that as well. For the time being I've reverted to a warning message if the date does not match the current date when the form is opened so the user knows to hit the new record button if they no longer have anything to add to the last record.

  10. #10
    Join Date
    May 2018
    Location
    Living in Scotland UK
    Posts
    1,566
    You can try the following:-

    Code:
    Dim LResponse As Integer
    LResponse = MsgBox("Do you wish to enter a new  record?", vbYesNo, "Continue")
    If LResponse = vbYes Then
       On Error Resume Next
    DoCmd.GoToRecord , , acNewRec
    Me.txt.Referencedate.SetFocus
    Else
       Me.txt.Referencedate.SetFocus
    End If
    You can PM me if you need further help.
    Good Reading https://docs.microsoft.com/en-gb/off...on-description

  11. #11
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,945
    That is bad coding.
    You are duplicating the setfocus for no reason?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  12. #12
    Join Date
    May 2018
    Location
    Living in Scotland UK
    Posts
    1,566
    Thanks for that
    You can PM me if you need further help.
    Good Reading https://docs.microsoft.com/en-gb/off...on-description

  13. #13
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,945
    Never mind the fact that the control is called txt_ReferenceDate and not txt.ReferenceDate
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  14. #14
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,803
    What's with the underscore between the control name and its type identifier? That just seems unnecessary to me.
    Ya know, you're only allowed so many key presses before you run out. No sense in using them when you don't have to (and probably we could count on one hand the number of people who would insert an underscore there).
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

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

Similar Threads

  1. MS Access Form message "No current record." box appears?
    By ramirezx@ddmfg.com in forum Access
    Replies: 1
    Last Post: 05-06-2021, 10:31 AM
  2. Replies: 13
    Last Post: 06-20-2018, 03:18 AM
  3. Pop Up message. Record already created.
    By Perfac in forum Programming
    Replies: 7
    Last Post: 01-28-2018, 07:03 PM
  4. Replies: 1
    Last Post: 02-25-2017, 09:24 PM
  5. Replies: 19
    Last Post: 06-11-2016, 01:35 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