473,503 Members | 1,647 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Forbid closing a Form

Yes, there is a way of forbidding a form being closed if you create a
boolean variable (blnCanClose = False) in the form's Unload event. I
didn't read all the responses, but I have just such a form that cannot
be closed using the close button. They must first select a button,
then the form closes. Here's the skeleton code:

Private Sub Form_Open(Cancel As Integer)
' bln is set to false when form is opened
' can't be closed until variable = true
blnCanClose = False
End Sub

Private Sub Form_Unload(Cancel As Integer)
If blnCanClose = False Then
MsgBox "Please Select a Date!"
' input person needs to select a date
' code then uses selected date to perform something
' call function or press a command button
Cancel = True
Else
Cancel = False
End If
End Sub

Private Sub cmdPerformFunction_Click()
' code for whatever you want performed from this form
' variable = true, form can now be closed after function executed
blnCanClose = True

' close the form after this button is pressed
DoCmd.Close acForm, Me.Name

End Sub

I downloaded this module and used it for my own use. The programmer's
form wouldn't close at all until another button was pressed. I hope
this helps!!
-- Michel
Nov 12 '05 #1
1 4400
The boolean variable is a good method of achieving the desired results, but
I'm not sure it would keep a user from closing the entire instance of
Access, versus just the form. I had once such instance where I wanted to be
absolutely certain to my ability that a user could not close the application
as a whole, versus just a form, other than by the provided means. Here's
what I did:

Create a class module called CloseCommand. Dump this code into it and save
it:

' --------------------------------------------------------------------------
------

Option Compare Database
Option Explicit

Private Declare Function GetSystemMenu Lib "user32" (ByVal hWnd As _
Long, ByVal bRevert As Long) As Long

Private Declare Function EnableMenuItem Lib "user32" (ByVal hMenu As _
Long, ByVal wIDEnableItem As Long, ByVal wEnable As Long) As Long

Private Declare Function GetMenuItemInfo Lib "user32" Alias _
"GetMenuItemInfoA" (ByVal hMenu As Long, ByVal un As Long, ByVal b
As Long, _
lpMenuItemInfo As mtypENUITEMINFO) As Long

Private Type mtypENUITEMINFO
cbSize As Long
fMask As Long
fType As Long
fState As Long
wID As Long
hSubMenu As Long
hbmpChecked As Long
hbmpUnchecked As Long
dwItemData As Long
dwTypeData As Long
cch As Long
End Type

Private Const mcF_GRAYED As String = &H1&
Private Const mcF_BYCOMMAND As String = &H0&
Private Const mcSC_CLOSE As String = &HF060&

Public Property Get Enabled() As Boolean

On Error GoTo Err_Enabled

Dim lngHWnd As Long
Dim lngHMenu As Long
Dim lngResult As Long
Dim MI As mtypENUITEMINFO

MI.cbSize = Len(MI)
MI.dwTypeData = String$(80, 0)
MI.cch = Len(MI.dwTypeData)
MI.fMask = mcF_GRAYED
MI.wID = mcSC_CLOSE
lngHWnd = Application.hWndAccessApp
lngHMenu = GetSystemMenu(lngHWnd, 0)
lngResult = GetMenuItemInfo(lngHMenu, MI.wID, 0, MI)
Enabled = (MI.fState And mcF_GRAYED) = 0
Exit_Enabled:
Exit Property

Err_Enabled:
Err.Raise Err.Number
End Property

Public Property Let Enabled(boolClose As Boolean)

On Error GoTo Err_Enabled

Dim lngHWnd As Long
Dim lngWFlags As Long
Dim lngHMenu As Long
Dim lngResult As Long

lngHWnd = Application.hWndAccessApp
lngHMenu = GetSystemMenu(lngHWnd, 0)

If Not boolClose Then
lngWFlags = mcF_BYCOMMAND Or mcF_GRAYED
Else
lngWFlags = mcF_BYCOMMAND And Not mcF_GRAYED
End If

lngResult = EnableMenuItem(lngHMenu, mcSC_CLOSE, lngWFlags)

Exit_Enabled:
Exit Property

Err_Enabled:
Err.Raise Err.Number

End Property

' -------------------------------------------------------

Then in my startup routine, I created a variable and set the attribute:

Dim c as CloseCommand
c.Enabled = False

When you're ready to allow them to exit gracefully, do the opposite:

Dim c as CloseCommand
c.Enabled = True
Note: This will disable the 'X' button in the upper right corner of
Access, disable the File - > 'Close' menu, and you cannot even right click
in the taskbar to close it. The only means is either to ctrl-alt-delete or
through your design procedures.
Good luck,

James

--
James Lankford, MCSE
"Michel" <m.********@att.net> wrote in message
news:13**************************@posting.google.c om...
Yes, there is a way of forbidding a form being closed if you create a
boolean variable (blnCanClose = False) in the form's Unload event. I
didn't read all the responses, but I have just such a form that cannot
be closed using the close button. They must first select a button,
then the form closes. Here's the skeleton code:

Private Sub Form_Open(Cancel As Integer)
' bln is set to false when form is opened
' can't be closed until variable = true
blnCanClose = False
End Sub

Private Sub Form_Unload(Cancel As Integer)
If blnCanClose = False Then
MsgBox "Please Select a Date!"
' input person needs to select a date
' code then uses selected date to perform something
' call function or press a command button
Cancel = True
Else
Cancel = False
End If
End Sub

Private Sub cmdPerformFunction_Click()
' code for whatever you want performed from this form
' variable = true, form can now be closed after function executed
blnCanClose = True

' close the form after this button is pressed
DoCmd.Close acForm, Me.Name

End Sub

I downloaded this module and used it for my own use. The programmer's
form wouldn't close at all until another button was pressed. I hope
this helps!!
-- Michel

Nov 12 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
2774
by: Peter yeshew | last post by:
Is it possible to forbid closing the form through the File- Close menu ? On my form i have a command button called CmdDeleteInvoice. When this command button is visible ,i want to forbid the user...
13
11354
by: Richard Hollenbeck | last post by:
To prevent future apostrophe bugs and errors, isn't it just simpler to forbid an apostrophe from being entered into a text field? For example, couldn't "Alice's Restaurant" be changed to "Alices...
1
5798
by: Chris Bruce | last post by:
In my application I need a way to distiguish between the following events: 1. When a user closes an MDI child window. 2. When the user closes the MDI parent window which subsequently closes the...
1
2189
by: **Developer** | last post by:
When I get a closing event in a MID Child form I don't know if the child form is closing or the main form is closing. Is there a way to tell? Thank
10
3983
by: Charles Law | last post by:
For some reason, when I click the X to close my MDI parent form, the action appears to be re-directed to one of the MDI child forms, and the parent remains open. I am then unable to close the...
2
3000
by: Tom | last post by:
How is the best way to avoid validation when closing a window? For instance, I have a Windows Forms window which has a validation event for a text box. However, if one enters invalid data in then...
4
2981
by: Academic | last post by:
Does it make sense to put this If e.Cancel Then Exit Sub at the beginning of form closing events so if the user cancels the app's exiting in one Closing routine he will not be asked again by...
14
3333
by: =?Utf-8?B?UHVjY2E=?= | last post by:
Hi, I'm using VS2005 and .net 2.0. I'm creating an application that has 3 forms. I want allow users to move forward and backward with the forms and retain the data users have entered. I thought...
2
4215
by: =?Utf-8?B?RXRoYW4gU3RyYXVzcw==?= | last post by:
I am (still) relatively new to Windows applications, most of my experience has been Web based, and I am confused about what exactly happens when the Main() method is called and how to manipulate...
0
7201
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7083
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
6988
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7456
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5578
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5011
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3166
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3153
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
379
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.