Results 1 to 5 of 5
  1. #1
    lixolin is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Nov 2013
    Posts
    6

    Prevent form close depending on close method

    I am well aware of how to cancel a form from closing (set 'cancel = true' in unload event) but i would like this prevention to be based on how the cancel event was started. i know i have seen code for this somewhere online while looking for something else but now i can't seem to find it. What i want is to prevent the closing of the form in all other ways but when i call it from code with docmd.close.



    From what i remember about this is that you had some value that told you how the form close had been initiated (like 1 if it's called from close, 2 if its called by the system, 3 if you hit the clsoe button, etc etc)and that you could base your code on this. Does any one know what i'm talking of? I know i did try it when i found it the last time and I think it worked pretty well for the thing i need to use it for now!

    All help and ideas appreciated!

  2. #2
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,863
    I am not aware of this approach. I imagine it would be possible to use a Select Case statement to evaluate whether the intrinsic close button was clicked. I believe the conventional approach would be to evaluate the data in the form using "Before Update". In this case it would not matter how the form is closed and or you can use an If Then or Select Case to evaluate constraints and decide whether or not to call DoCmd.Close

    90% of my forms have the "Close" button disabled.

  3. #3
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2013
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,912
    The easiest way is to define a global public variable in the code module and set it with code. If it is not set in the UnLoad event, then cancel the close.
    (RG for short) aka Allan Bunch Previous MS Access MVP - WinXP Pro, Win7 Pro Win10 Pro - acXP, ac07, ac10, ac13
    How to mark the thread as Solved!
    Teaching is not filling a bucket but lighting a fire.
    Borrowed quote..."Docendo discimus"

  4. #4
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,863
    I am curious how Access would know (or how you could remind Access) if the intrinsic close button for the form was clicked vs. another control.

  5. #5
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2013
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,912
    Quote Originally Posted by ItsMe View Post
    I am curious how Access would know (or how you could remind Access) if the intrinsic close button for the form was clicked vs. another control.
    AFAIK it can't beyond the fact that only you know about the variable tested and only you can set it to bypass the cancel code.
    (RG for short) aka Allan Bunch Previous MS Access MVP - WinXP Pro, Win7 Pro Win10 Pro - acXP, ac07, ac10, ac13
    How to mark the thread as Solved!
    Teaching is not filling a bucket but lighting a fire.
    Borrowed quote..."Docendo discimus"

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

Similar Threads

  1. How to close a form
    By rescobar in forum Forms
    Replies: 6
    Last Post: 08-19-2013, 06:42 PM
  2. Replies: 2
    Last Post: 06-20-2011, 03:10 PM
  3. Open Form with instance method & wait to close?
    By GeorgeBakoyannis in forum Forms
    Replies: 1
    Last Post: 10-09-2010, 11:00 AM
  4. Close All but One Form
    By Bruce in forum Forms
    Replies: 4
    Last Post: 04-06-2010, 09:31 AM
  5. close form
    By taylorosso in forum Forms
    Replies: 5
    Last Post: 08-25-2009, 12:18 PM

Tags for this Thread

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 - Senior Forums