DataBase - Code that worked in 2003 doesn't work in 2007
Asked By tkosel
07-Apr-10 06:04 PM

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
Application.hWndAccessApp
(1)
DoCmd.ShowToolbar
(1)
Office 2007
(1)
Access 2003
(1)
Access 2007
(1)
Office
(1)
WIDEnableItem
(1)
Debug.Print
(1)
Linq Adams via AccessMonster.com replied to tkosel
First thing that has to be asked in this situation is does any code run? Code
does not run in 2007 unless your database resides in a folder that has been
declared a ???trusted??? location.
To trust your folder, click:
Office Button (top left)
Access Options (bottom of dialog)
Trust Center (left)
Trust Center Settings (button)
Trusted Locations (left)
Add new location (button)
--
There is ALWAYS more than one way to skin a cat!
Answers/posts based on Access 2000/2003
Message posted via http://www.accessmonster.com
Jeanette Cunningham replied to tkosel

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
tkosel replied to Jeanette Cunningham
Thanks to both of you for responses. Found out that this code does not work
if you do not 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
does not 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 would not install.
Jeanette Cunningham replied to tkosel
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
tkosel replied to Jeanette Cunningham
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 cannot have both! Any ideas?
Jeanette Cunningham replied to tkosel
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

Roger Jennings on Access / Sharepoint DataBase http: / / www.quepublishing.com / articles / article.aspx?p = 1606238 I was very dismayed is it makes it pretty clear that in-house hosting of your Sharepoint server with Access Services to support browser-based Access apps is something only large companies will be able to afford, because the pricing and licensing for the Enterprise version is very, very steep. The alternative is hosted Sharepoint / Access Services, and the costs do not seem terribly high. It seems to me that the pricing is upside-down. Big enterprises do not want to deploy Access apps in the browser - - they will build their own .NET apps, browser-based or not 2010, it could vastly increase those costs for organizations with legacy hardware). The features of Access 2010 used in conjunction with Sharepoint 2010 seem to me to be most compelling for and small businesses. I do not know if I could sell clients on hosted Sharepoint / Access. I have been able to sell clients on hosted Exchange, and it is been very W. Fenton http: / / www.dfenton.com / contact via website only http: / / www.dfenton.com / DFA / Access Discussions SQL Express (1) SQL Server (1) MySQL (1) Exchange Server (1) Windows Server (1
Access 2010 for software development DataBase Hi, What do I need to use Access 2010 (or perhaps 2007) to port our 2003 application and create an executable (MDE in Access 2003)? Are there any issues with 2010? -paulw Access Discussions SQL Server (1) SharePoint (1) Albert D. Kallal (1 Office 2007 (1) Office 2003 (1) Access 2007 (1) Access 2010 (1) Access 2003 (1) You
Considerations migrating from Access 97 mdb to Access 2003 (or up)? DataBase What are the things I need to watch for? I know about sheet properties and Sandboxing mode and making sure to run Jet 4.0 SP8 for 2003 but what else should I look for? We're kind of at the end of the road (so it seems) with Access 97. It does not play well with Windows 7 Professional even in XP Mode, the is crashy / unsreponsive at times. I figure moving up to a more 'modern' version of Access can help alleviate some of these problems and allow it to run native in Windows XP mode. So I am all ears for recommendations on migration or ways to get Access 97 to work properly in Win 7. Thanks a bunch. Access Discussions Visual Studio .NET (1) Windows XP (1) Office 2007 (1) Access 2007 (1) Access
Upgrading to Office 2010 DataBase Hi everyone, I am about to upgrade my hardware and software, and would sell Office2010 solutions. Do I need Visual Studio 2010 or some special developer version of Office or anything like that? Most of the development I have done in Office so far is with either 97 or 03. Am I going to be able to am reasonably comfortable with .Net but do not know if I will need it for Office development in 2010. Thoughts and experience greatly appreciated. The more I read the more confused I get. Looking forward to getting a new toy though! Cheers The Frog Access Discussions Larry Linson Microsoft Office Access MVP Co author Microsoft Access (1) James A. Fortune CDMAPoster (1) Visual Studio 2010 (1) Entity Framework (1) Microsoft Excel
in the library and seeing how many forms there are. Any help gratefully received Phil Access Discussions David W. Fenton (1) Access 2007 (1) Office 2007 (1) Windows 7 (1) Distributed (1) Exception (1) Relative (1) Declare (1) Library is the means no MDEs, right? That would be a complete showstopper for a large population of Access developers, I think. It would not be for me in all my apps, but for to the Menu db that it does not reload it. I can remove the reference (Access.References.Remove RefName), but then I have to compact and repair the Db before adding I cannot find any way of removing the reference from the Project explorer after running Access.References.Remove RefName. Any help gratefully received as ever Thanks Phil Hi David Think one