Code that worked in 2003 doesn't work in 2007

T

tkosel

I have been using the code below in a bunch of applications for quite some
time. It prevents the user from closing the Access program (Access 2003 and
older) using the application close button. It works great, I make the user
exit using a control on my form. However, it does not work in Access 2007.
Can anyone suggest why? Any way that you can see to make it work like it
used to?

Option Explicit
Option Compare Database
Private Declare Function GetSystemMenu Lib "user32" (ByVal hwnd As Long,
ByVal wRevert As Long) As Long

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


Public Sub FMS_AppCloseEnabled(pfEnabled As Boolean)
' Comments: Control the Access close button.
' Disabling it forces the user to exit within the application
' In : pfEnabled TRUE enables the close button, FALSE disabled it
' Owner : Copyright (c) 2008 from FMS, Inc.
' Usage : Permission granted to subscribers of the FMS Newsletter

On Error Resume Next

Const clngMF_ByCommand As Long = &H0&
Const clngMF_Grayed As Long = &H1&
Const clngSC_Close As Long = &HF060&

Dim lngWindow As Long
Dim lngMenu As Long
Dim lngFlags As Long

lngWindow = Application.hWndAccessApp
lngMenu = GetSystemMenu(lngWindow, 0)
If pfEnabled Then
lngFlags = clngMF_ByCommand And Not clngMF_Grayed
Else
lngFlags = clngMF_ByCommand Or clngMF_Grayed
End If
Call EnableMenuItem(lngMenu, clngSC_Close, lngFlags)
End Sub
 
J

Jeanette Cunningham

The code below worked for me in A2007.
To test it, I created a new form and put 2 buttons on it.
Clicking Command1 greys out the main app close button.

'start code----
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

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




Public Function SetEnabledState(blnState As Boolean)
' Usage :Call SetEnabledState(True)

Call CloseButtonState(blnState)

End Function





Public Sub CloseButtonState(bolClose 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 bolClose Then
wFlags = MF_BYCOMMAND Or MF_GRAYED
Else
wFlags = MF_BYCOMMAND And Not MF_GRAYED
End If
Debug.Print wFlags

result = EnableMenuItem(hMenu, SC_CLOSE, wFlags)


End Sub


Private Sub Command0_Click()
Call SetEnabledState(True)
End Sub

Private Sub Command1_Click()
Call SetEnabledState(False)
End Sub

'end code------------


Jeanette Cunningham MS Access MVP -- Melbourne Victoria Australia
 
T

tkosel

Thanks to both of you for responses. Found out that this code doesn't work
if you don't have office sp2 installed. Installed office sp2 and everything
is fine. However, that does bring up another question. I want to deploy
this application using the access 2007 runtime, and this code runs, but
doesn't have the desired effect. As previously mentioned, in regular Access
2007, it has the desired effect if you install office sp2. Any ideas on how
I can get it to run in the runtime? I tried installing sp2 on the runtinme
installled machine, it wouldn't install.
 
J

Jeanette Cunningham

If you run office update on the machine with the runtime,
what do you get offered as updates?
Does the machine with the runtime have office 2007 on it?


Jeanette Cunningham MS Access MVP -- Melbourne Victoria Australia
 
T

tkosel

Jeanette and other,

Thanks for your responses. I have not had a chance to run Office Update
yet. I will test, my suspicion is that since no "Office" components are
installed, nothing will happen. (Unless the runtime is considered an Office
component!) I will test and respond. Office 2007 IS NOT on the runtime
machine.

Interesting discovery. If I call the code on a machine with Office SP2
installed it works. However, if I run this line first, it does not work.

DoCmd.ShowToolbar "Ribbon", acToolbarNo

Too bad, I would really like to use that combination to eliminate the Access
Office button and prevent the user from exiting using the close dialog!
Seems like I can't have both! Any ideas?
 
J

Jeanette Cunningham

First check that the database is in a trusted location, then see if the code
will run.


Jeanette Cunningham MS Access MVP -- Melbourne Victoria Australia
 

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