• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 218
  • Last Modified:

closing a form

Sorry if this seems like a silly question but.....

I have a form which has various text input boxes. One of the inputs (along with one other field) makes up the primary key.  I've set up a message box that prompts the user if an invalid ID has been input.  This works fine.  The message box has RETRY and CANCEL buttons.  I've been able to get the retry to work without any problem - I just send the user back to the text box to have another go.  For the cancel button though, I would like to close the form completely (and go back to a main menu when I set one up).  When I try to do a

DoCmd.Close acForm, "Initial", acSaveNo

command I just get a

Run-time error '2585': "This action can't be carried out while processing a form or report event."

I've tried saving the record first, running the command from a module rather than inside the form code, saving the record then deleting it and lots more I can't remember.  I feel like I must be missing something really obvious.
I hope this all makes sense (and it's an easy one...)
Thanks in advance,
1 Solution

are you using

retvalue = MsgBox("Continue?", vbOKCancel)
    If retvalue = 2 Then
    End If

something like that?
As the error suggests, there are some event procedures in which you can't carry out a DoCmd.Close acForm action (for example the LostFocus and Exit event procedures).

You can try some alternative designs to get round this problem. One way might be to remove the cancel button on the message box and provide cancel and OK buttons on the form itself. You can disable the OK button until the user enters a valid ID and just enable it when appropriate.
Have you tried using the before-update event instead of lost focus??
Easily Design & Build Your Next Website

Squarespace’s all-in-one platform gives you everything you need to express yourself creatively online, whether it is with a domain, website, or online store. Get started with your free trial today, and when ready, take 10% off your first purchase with offer code 'EXPERTS'.

Give this a try, don't know if it will work but I've been using it and it's ok:

DoCmd.Close acForm, me.Name, acSaveNo

Otherwise, could you past the code which has the retry/cancel options? This may help us help you.
damien232Author Commented:
Thanks for the help.
I've sort of worked it out (with a dodgy workaround).  I added a CANCEL button to the form (as suggested by shanesuebsahakarn) and then made the cancel button on the dialog box clear the invalid input and send an ESCAPE character which is then effectively the same as clicking the cancel button.

damien232Author Commented:
I partially used your advice with a flash of insight and some more trial-and-error on my part.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get 10% Off Your First Squarespace Website

Ready to showcase your work, publish content or promote your business online? With Squarespace’s award-winning templates and 24/7 customer service, getting started is simple. Head to Squarespace.com and use offer code ‘EXPERTS’ to get 10% off your first purchase.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now