Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Microsoft Office > Access and Access VBA > Access
Password Reminder
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old December 19th, 2005, 07:05 AM
Authorized User
Join Date: Apr 2004
Location: Cape Town, , South Africa.
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.

Reply With Quote
  #2 (permalink)  
Old December 19th, 2005, 08:38 AM
Friend of Wrox
Points: 9,611, Level: 42
Points: 9,611, Level: 42 Points: 9,611, Level: 42 Points: 9,611, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
Join Date: Mar 2004
Location: Washington, DC, USA.
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.)


Reply With Quote
  #3 (permalink)  
Old December 19th, 2005, 11:19 AM
Friend of Wrox
Join Date: Jun 2003
Location: , , USA.
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



Reply With Quote
  #4 (permalink)  
Old December 21st, 2005, 01:01 PM
Authorized User
Join Date: Apr 2004
Location: Cape Town, , South Africa.
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

Reply With Quote

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

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

All times are GMT -4. The time now is 08:21 PM.

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