Results 1 to 9 of 9

Preventing the closing of a form

  1. #1
    S2000magician is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Jul 2019
    Posts
    31

    Preventing the closing of a form


    I have a form that pops up when a user wants to create a new portfolio. To create the portfolio, the user has some tasks to perform, then has to click a "Create the Portfolio" button.

    I want to prevent the user from closing the form until that button's been clicked.

    How can I do that?

    I tried writing code for the Form_Close event, and exiting that subroutine when the button hasn't been clicked, but the form still closes.

    Help!

  2. #2
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,440
    Disable the close button property
    disable your 'close' button on your form (enabled property to false)
    in the ON CLICK event of the create portfolio button, if the addition of the record is successful then enable your 'close' button.

    I'd just be cautious here because if they've started the process (I'm assuming we're talking about a bound form, not an unbound form) you have to have some way of resetting the form so they can clear everything out and reset the 'close' button without adding a record.

  3. #3
    S2000magician is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Jul 2019
    Posts
    31
    Perfect!

    I didn't know that I could do that. It's just what I needed.

    Thanks so much!

    As a follow-on, when they do click the "Create the Portfolio" button and everything is OK, I want to close the form. Is there a Form Close command?

    I know: I should already know this junk.

  4. #4
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,440
    yes

    docmd.close

    by default that'll close the form you're on but the docmd.close may also include parameters like closing a form that's not the active form.

  5. #5
    S2000magician is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Jul 2019
    Posts
    31
    Thanks, again!

    You've been a great help.

  6. #6
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,440
    mark it solved!

  7. #7
    Ajax is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    7,359
    depends on how your app is constructed, but be aware that even if you have disabled the close button property it is likely the user can still close the form by either right clicking on the form window titlebar if using overlapping windows or popups, tab if using tabbed documents or the access window titlebar. Or by closing the app from the windows taskbar or task manager.

    So you also need to include some additional code and a form scoped variable. Something like

    put this at the top of your form module before your first sub or function

    Code:
    Dim OKToClose as Boolean
    in your close button event use

    Code:
    OKToClose=True
    docmd.close
    and in the form unload event put

    Code:
    if not OKToClose then msgbox "Please click the close button to close this form"
    Cancel=not OKToClose
    however not sure if that will work for taskbar or task manager

  8. #8
    S2000magician is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Jul 2019
    Posts
    31
    Quote Originally Posted by rpeare View Post
    mark it solved!
    How do I do that?

  9. #9
    S2000magician is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Jul 2019
    Posts
    31
    Quote Originally Posted by Ajax View Post
    depends on how your app is constructed, but be aware that even if you have disabled the close button property it is likely the user can still close the form by either right clicking on the form window titlebar if using overlapping windows or popups, tab if using tabbed documents or the access window titlebar. Or by closing the app from the windows taskbar or task manager.

    So you also need to include some additional code and a form scoped variable. Something like

    put this at the top of your form module before your first sub or function

    Code:
    Dim OKToClose as Boolean
    in your close button event use

    Code:
    OKToClose=True
    docmd.close
    and in the form unload event put

    Code:
    if not OKToClose then msgbox "Please click the close button to close this form"
    Cancel=not OKToClose
    however not sure if that will work for taskbar or task manager
    Thanks for the advice.

    I'll see if I can implement that.

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

Similar Threads

  1. Replies: 15
    Last Post: 04-02-2019, 02:41 PM
  2. Replies: 4
    Last Post: 03-16-2015, 10:31 AM
  3. Replies: 1
    Last Post: 01-09-2013, 01:33 PM
  4. Preventing Form Close
    By ramhanuman in forum Forms
    Replies: 1
    Last Post: 05-02-2012, 03:43 PM
  5. Replies: 1
    Last Post: 04-25-2012, 01:57 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
  •  
Tech Forums: Microsoft Office Forums