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