Controlling Exits From Access

J

JFK

Hi,

Anyone any ideas how to override the exit application X that seems to
be present in all Access databases?

I need to force people to exit the application through an exit app
button and not via the outer quit option?

If this can't be done, is there an easy way to prompt users are they
sure they want to exit before the DB actually shuts down? At the
moment clicking on X closes the DB down with no prompts.
 
R

Rick Brandt

JFK said:
Hi,

Anyone any ideas how to override the exit application X that seems to
be present in all Access databases?

I need to force people to exit the application through an exit app
button and not via the outer quit option?

You only *think* you need this. Open a hidden form at startup and leave it
open the whole time the app is in use. Put whatever code you have in your
"exit app button" in the unload or close event of the hidden form. It will
run when they close Access in any proper manner.
If this can't be done, is there an easy way to prompt users are they
sure they want to exit before the DB actually shuts down? At the
moment clicking on X closes the DB down with no prompts.

That could be put into the unload of the aforementioned form. If they
answer no you can set the Cancel event of that argument to True which will
prevent Access closing.
 
A

ashg657

Create a new class module and paste the following:

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 MENUITEMINFO) As Long

Private Type MENUITEMINFO
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 String
cch As Long
End Type

Const MF_GRAYED = &H1&
Const MF_BYCOMMAND = &H0&
Const SC_CLOSE = &HF060&

Public Property Get Enabled() As Boolean
Dim hwnd As Long
Dim hMenu As Long
Dim result As Long
Dim MI As MENUITEMINFO

MI.cbSize = Len(MI)
MI.dwTypeData = String(80, 0)
MI.cch = Len(MI.dwTypeData)
MI.fMask = MF_GRAYED
MI.wID = SC_CLOSE
hwnd = Application.hWndAccessApp
hMenu = GetSystemMenu(hwnd, 0)
result = GetMenuItemInfo(hMenu, MI.wID, 0, MI)
Enabled = (MI.fState And MF_GRAYED) = 0
End Property

Public Property Let Enabled(boolClose As Boolean)
Dim hwnd As Long
Dim wFlags As Long
Dim hMenu As Long
Dim result As Long

hwnd = Application.hWndAccessApp
hMenu = GetSystemMenu(hwnd, 0)
If Not boolClose Then
wFlags = MF_BYCOMMAND Or MF_GRAYED
Else
wFlags = MF_BYCOMMAND And Not MF_GRAYED
End If
result = EnableMenuItem(hMenu, SC_CLOSE, wFlags)
End Property

......and then
Create a new module (or paste into an existing one) the following:

Function InitApplication()
Dim c As CloseCommand
Set c = New CloseCommand
Dim f As Boolean
On Error Resume Next
f = False
'Application.c = "Pheonix"
f = DLookup("[dbaccess]", "info", "id=1")
On Error GoTo 0
Err.Clear
'Disable Close menu.
c.Enabled = f
End Function

.....Finally, make a call to that function InitApplication somewhere when
you're app is loaded.
 
A

ashg657

Please note my previous post is simply a copy and paste jobby from my own
app. You will need to remove/edit some lines of code to work with your app.

Good luck.
--
Ash
Don''t forget to rate as helpful if I have helped you,Thanks!
(e-mail address removed)


ashg657 said:
Create a new class module and paste the following:

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 MENUITEMINFO) As Long

Private Type MENUITEMINFO
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 String
cch As Long
End Type

Const MF_GRAYED = &H1&
Const MF_BYCOMMAND = &H0&
Const SC_CLOSE = &HF060&

Public Property Get Enabled() As Boolean
Dim hwnd As Long
Dim hMenu As Long
Dim result As Long
Dim MI As MENUITEMINFO

MI.cbSize = Len(MI)
MI.dwTypeData = String(80, 0)
MI.cch = Len(MI.dwTypeData)
MI.fMask = MF_GRAYED
MI.wID = SC_CLOSE
hwnd = Application.hWndAccessApp
hMenu = GetSystemMenu(hwnd, 0)
result = GetMenuItemInfo(hMenu, MI.wID, 0, MI)
Enabled = (MI.fState And MF_GRAYED) = 0
End Property

Public Property Let Enabled(boolClose As Boolean)
Dim hwnd As Long
Dim wFlags As Long
Dim hMenu As Long
Dim result As Long

hwnd = Application.hWndAccessApp
hMenu = GetSystemMenu(hwnd, 0)
If Not boolClose Then
wFlags = MF_BYCOMMAND Or MF_GRAYED
Else
wFlags = MF_BYCOMMAND And Not MF_GRAYED
End If
result = EnableMenuItem(hMenu, SC_CLOSE, wFlags)
End Property

.....and then
Create a new module (or paste into an existing one) the following:

Function InitApplication()
Dim c As CloseCommand
Set c = New CloseCommand
Dim f As Boolean
On Error Resume Next
f = False
'Application.c = "Pheonix"
f = DLookup("[dbaccess]", "info", "id=1")
On Error GoTo 0
Err.Clear
'Disable Close menu.
c.Enabled = f
End Function

....Finally, make a call to that function InitApplication somewhere when
you're app is loaded.
--
Ash
Don''t forget to rate as helpful if I have helped you,Thanks!
(e-mail address removed)


JFK said:
Hi,

Anyone any ideas how to override the exit application X that seems to
be present in all Access databases?

I need to force people to exit the application through an exit app
button and not via the outer quit option?

If this can't be done, is there an easy way to prompt users are they
sure they want to exit before the DB actually shuts down? At the
moment clicking on X closes the DB down with no prompts.
 
K

Keith Wilby

ashg657 said:
Create a new class module and paste the following:
<snip>

With respect, I've no idea what that code does but it looks way too
complicated. The OP's objective can be achieved simply by using the hidden
form method suggested by Rick. I also use this method and it works
perfectly.

Regards,
Keith.
www.keithwilby.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top