Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

disable the close button in a window 1

Status
Not open for further replies.

easycode

Programmer
Jan 28, 2005
195
US
Hi all,
i have a customized application, and i would like to disable the close button of the main window (at the top right of the window) , I know how to do it in a form but i dont in the access window, in this way i am forcing the user to use the close button in the application. Before the application is closed i do some process to kepp the integrity of the application, but if user clicks on the close icon, i can not do the process.
If anyone can help, i thank you much.
 
I don't remember where I got this but it works....

Put the following in a Class Modual named CloseCommand

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

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

Then put this in another modual and call it fromt he autoexec macro or attatch it to a button or something...

Code:
Function InitApplication()
   Dim c As CloseCommand
   Set c = New CloseCommand

   'Disable Close menu.
   c.Enabled = False
'   c.Enabled = True
End Function

HTH


 
thanks for replying. I did what you said
but i am getting the error message "a module is not a valid type" and stops at

Dim c as CloseCommand

looks like access is not recognizing CloseCommand as a Data Type
 
This is what I use

Place this code in a module not associated with any forms

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)
Call CloseButtonState(blnState)
Call ExitMenuState(blnState)
End Function

Sub ExitMenuState(blnExitState As Boolean)
Application.CommandBars("File").Controls("Exit").Enabled = blnExitState
End Sub

Sub CloseButtonState(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 Sub

And then in your Main form's open event

Call SetEnabledState(False)

And in your Main form's close event

Call SetEnabledState(True)

This should work for you

lwells
 
Were you able to get the problem fixed microsoftaccess2000? Make sure the big chunk of code is in a class modual and not a regular modual. Make usre the modual is name CloseCommand. HTH
 
yes thank you,

that was the problem, that the first portion of the code should be in a class module.

Thanks again to all who replied.
 
Now i am dealing with something else, but i already posted the question on the forum about how to display a message to the user saying ""Are sure you want to navigate away from this page" with buttons Yes and Cancel, THis suppose to be in the close button of the Access Window Application.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top