Solved

Disabling Close Command

Posted on 2008-06-26
7
375 Views
Last Modified: 2012-06-27
I found a Microsoft article of how to disable the MS Access top close button and the close option in the system menu (when you right click on MS Access entry on the Windows task bar). I modified it to be very simple and it works great when only one form is open. However, when two forms open there is no longer a single MS Access entry on the WIndows task bar, there are individual entries for each form. So of course the close option in the system menu is enabled again as these are not the MS Access entry. So to get around this I figured I could just disable the close option in the new task bar. Easier said than done however.

For the function to disable the MS Access window's top close button and system menu close option I use Application.hWndAccessApp. When I attempted to disable the individual form window's closing stuff I used Me.hwnd for the argument from within the form. Using Me.Hwnd disabled the forms top close button but not the system menu close option.

What am I doing wrong?
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 DisableClose(hWnd As Long)
    Dim wFlags As Long
    Dim hMenu As Long
    
    hMenu = GetSystemMenu(hWnd, 0)
    wFlags = MF_BYCOMMAND Or MF_GRAYED
    
    EnableMenuItem hMenu, SC_CLOSE, wFlags
End Function

Open in new window

0
Comment
Question by:bejhan
  • 4
  • 2
7 Comments
 
LVL 27

Expert Comment

by:jjafferr
ID: 21876409
put the code in a module,

On the Main Form's load, have this call:
    'hide the close button
    HideAccessCloseButton

On the main Form's Close, have this call:
    'restore the max, min, close buttons
    UnHideAccessCloseButton

jaffer
Option Compare Database 

    Private Const GWL_EXSTYLE = (-20)
    Private Const GWL_STYLE = (-16) 
    Private Const WS_MAXIMIZEBOX = &H10000
    Private Const WS_MINIMIZEBOX = &H20000
    Private Const WS_SYSMENU = &H80000 
    Private Const HWND_TOP = 0
    Private Const SWP_NOMOVE = &H2
    Private Const SWP_NOSIZE = &H1
    Private Const SWP_FRAMECHANGED = &H20
    Private Const SWP_DRAWFRAME = SWP_FRAMECHANGED 
    Private Declare Function SetWindowLong Lib "user32" _
    Alias "SetWindowLongA" (ByVal hWnd As Long, _
    ByVal nIndex As Long, ByVal dwNewLong As Long) _
    As Long
    Private Declare Function GetWindowLong Lib "user32" _
    Alias "GetWindowLongA" (ByVal hWnd As Long, _
    ByVal nIndex As Long) As Long
    Private Declare Function SetWindowPos Lib "user32" _
    (ByVal hWnd As Long, ByVal hWndInsertAfter As Long, _
    ByVal x As Long, ByVal y As Long, ByVal cx As Long, _
    ByVal cy As Long, ByVal wFlags As Long) As Long 
    Sub HideAccessCloseButton() 
        Dim lngStyle As Long 
        lngStyle = GetWindowLong(hWndAccessApp, GWL_STYLE)
        lngStyle = lngStyle And Not WS_SYSMENU
        Call SetWindowLong(hWndAccessApp, GWL_STYLE, lngStyle)
        Call SetWindowPos(hWndAccessApp, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE Or SWP_DRAWFRAME) 
    End Sub 
    Sub UnHideAccessCloseButton() 
        Dim lngStyle As Long 
        lngStyle = GetWindowLong(hWndAccessApp, GWL_STYLE)
        lngStyle = lngStyle Or WS_SYSMENU
        Call SetWindowLong(hWndAccessApp, GWL_STYLE, lngStyle)
        Call SetWindowPos(hWndAccessApp, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE Or SWP_DRAWFRAME) 
    End Sub

Open in new window

0
 
LVL 1

Author Comment

by:bejhan
ID: 21876477
I don't think you understood my problem. I can disable and enable the access close button and system menu close option (SMCO). That is not problem.

For example:
I open form1, disable access close stuff. So now I can't exit the application unless I use the exit button provided in my form which is exactly what I want. Now I open form2. Form1 is still open. Now instead of the MS Access entry being the taskbar there is an entry for form1 and for form2. Since I only disabled the SMCO for the MS Access window, form1 and form2 can still be closed via their own SMCO. I want to disable form1's and form2's SMCO. So I try to use the same function I disabled the access top close button and SMCO except with form1.hwnd and form2.hwnd as arguments to the function. Now form1's and form2's top close button is disabled but their SMCOs are still enabled. I want to disable these SMCOs.
0
 
LVL 75

Accepted Solution

by:
DatabaseMX (Joe Anderson - Access MVP) earned 500 total points
ID: 21876500
0
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
LVL 1

Author Comment

by:bejhan
ID: 21876641
Wow, so simple... so simple. Thank you MX I have been grappling this problem for about a month now. Even had to close a question and ask again because I didn't find any answers. Incase anyone was wondering this is all I did (following the guidance of the link posted):

Declared Public pboolCloseAccess As Boolean in a module.

Created a hidden form, that loaded before any other forms.

In the hidden form I put these two events:

Private Sub Form_Load()
pboolCloseAccess = False
End Sub

Private Sub Form_Unload(Cancel As Integer)
Cancel = False

If Not pboolCloseAccess Then Cancel = True
End Sub

So this way, my exit command would set pboolCloseAccess to True so that the hidden form would unload, otherwise Cancel = True and it stops the whole process.
0
 
LVL 1

Author Comment

by:bejhan
ID: 21876843
I just realized that doesn't stop the user from closing a form while in the middle of work which is my main concern. I suppose I could use the same concept but for each form? Any better ideas?
0
 
LVL 75
ID: 21877177
Well, you can keep a form from close by using the UnLoad event like so:

Private Sub Form_UnLoad(Cancel As Integer)

    If <SomeCondition> =True Then
       Msgbox "Some message"
       Cancel = True
    End If

End Sub
0
 
LVL 1

Author Comment

by:bejhan
ID: 21877210
That's what I was thinking of doing but that is alot of work. I guess if its the only way I'll have to do it.
0

Featured Post

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article is a continuation or rather an extension from Cascading Combos (http://www.experts-exchange.com/A_5949.html) and builds on examples developed in detail there. It should be understandable alone, but I recommend reading the previous artic…
Most if not all databases provide tools to filter data; even simple mail-merge programs might offer basic filtering capabilities. This is so important that, although Access has many built-in features to help the user in this task, developers often n…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question