Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Access and Access VBA > Access
Access Discussion of Microsoft Access database design and programming. See also the forums for Access ASP and Access VBA.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old December 19th, 2005, 07:05 AM
Authorized User
Join Date: Apr 2004
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to lizhaskin
Default Preventing user from closing form

Can anyone tell me how I can prevent a user from closing a form unless certain criteria are met? To be more specific, I have a form called Journals. In order for the system to balance the sum of the journals entered must equal zero.
Any ideas would be most welcome.

Old December 19th, 2005, 08:38 AM
Friend of Wrox
Join Date: Mar 2004
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts

Go to the form's properties dialog box, select the Other tab (I think) and select Modal. This will prevent the user from doing ANYTHING in the application unless a condition is met on the form (Um, other than right clicking on the form, selecting Design View, and then closing the form that way. That will take additional code, but most users won't figure it out. If you have validation on your table fields, this will also prevent fruther action, or error or condition checking on your form fields.)


Old December 19th, 2005, 11:19 AM
Friend of Wrox
Join Date: Jun 2003
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts

Hi Liz,

The following will completely lock down a form for you. The user will not be able to close the form by using any of the following:

- Close box in upper right hand corner of form
- Control box in upper left hand corner of form
- Ctrl-F4

The only way to close the form will be by clicking a Close button with some data validation code behind it.

In my example, I just placed an unbound textbox (Text1) on the form, and a command button (cmdClose). The user can’t close the form unless 0 is entered in the textbox. The error handler traps for the “Close action cancelled” error and displays a message box instructing the user to enter 0 in Text1.

Option Compare Database
Option Explicit

Private blnCanClose As Boolean

Private Sub cmdClose_Click()
  On Error GoTo ErrHandler

  blnCanClose = False
  If Me.Text1.Text = CStr(0) Then
    blnCanClose = True
  End If


    Exit Sub

    Dim strMsg As String
    If Err.Number = 2501 Then 'Close action cancelled
        strMsg = "Cannot close form" & vbCrLf & vbCrLf
        strMsg = strMsg & "Text1 must show 0."

        MsgBox "Error Number: " & Err.Number & vbCrLf & vbCrLf & strMsg, _
            vbCritical + vbOKOnly, "Data validation error"
        MsgBox "Error No: " & Err.Number & "; Description: " & Err.Description
    End If

    Resume ExitProcedure

End Sub

Private Sub Form_Unload(Cancel As Integer)
  Cancel = Not blnCanClose
End Sub



Old December 21st, 2005, 01:01 PM
Authorized User
Join Date: Apr 2004
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to lizhaskin

Hi Bob & mmcdonal
Thanks for your reply's. I have used Bob's code and it works like a dream

Similar Threads
Thread Thread Starter Forum Replies Last Post
preventing download for un registered user amirquddus ASP.NET 2.0 Professional 3 November 8th, 2007 10:35 AM
How to refresh owner form on closing of child form akumarp2p C# 2005 0 December 22nd, 2006 10:27 AM
Closing a form after opening another form rosebushr Access 2 January 7th, 2006 04:34 AM
Preventing Windows Form Automatic Refresh? Ron Howerton VB.NET 2002/2003 Basics 9 June 17th, 2004 08:48 AM
Preventing form submission on refresh rathbird General .NET 2 February 25th, 2004 06:33 PM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.