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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Disabling application close button 3

Status
Not open for further replies.

policechiefwiggum

Programmer
Mar 19, 2006
48
GB
Hi All,

I want to disable to access main window close button.

i have password validation on my own "Exit" button, which is not invoked with the X.

I found this on the web -
Code:
Option Compare Database
Option Explicit


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

which works so well it just removes all the buttons!

what i'd like is for either my password validation to work when the close button is clicked, or for just the close button to be diabled/removed. with the above code everything disapears meaning users are unable to minimize the appication

Thanks as always
Policechiefwiggum
 
well you can put this code behind a button to Minimize

DoCmd.RunCommand acCmdAppMinimize
 
Have A star for you nice peice of code perhaps you also have the sister coder to this to turn the the close button back on
 
Here's the code I use to do what you require

Code:
Option Compare Database

Private Declare Function IsWindowVisible Lib "user32" (ByVal hwnd As Long) As Long
Dim dwReturn As Long

Const SW_HIDE = 0
Const SW_SHOWNORMAL = 1
Const SW_SHOWMINIMIZED = 2
Const SW_SHOWMAXIMIZED = 3

Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, _
     ByVal nCmdShow As Long) As Long
     
Public Function fAccessWindow(Optional Procedure As String, Optional SwitchStatus As Boolean, Optional StatusCheck As Boolean) As Boolean
If Procedure = "Hide" Then
    dwReturn = ShowWindow(Application.hWndAccessApp, SW_HIDE)
End If
If Procedure = "Show" Then
    dwReturn = ShowWindow(Application.hWndAccessApp, SW_SHOWMAXIMIZED)
End If
If Procedure = "Minimize" Then
    dwReturn = ShowWindow(Application.hWndAccessApp, SW_SHOWMINIMIZED)
End If
If SwitchStatus = True Then
    If IsWindowVisible(hWndAccessApp) = 1 Then
        dwReturn = ShowWindow(Application.hWndAccessApp, SW_HIDE)
    Else
        dwReturn = ShowWindow(Application.hWndAccessApp, SW_SHOWMAXIMIZED)
    End If
End If
If StatusCheck = True Then
    If IsWindowVisible(hWndAccessApp) = 0 Then
        fAccessWindow = False
    End If
    If IsWindowVisible(hWndAccessApp) = 1 Then
        fAccessWindow = True
    End If
End If
End Function

Then in the Load event of the Databases main form add

Code:
EnableDisableControlBoxX False

This will make the X button go but the Min and Max buttons will remain.

Hope this helps

 
Sorry wrond module pasted use the following

Code:
Option Compare Database

Option Explicit
Public Declare Function apiEnableMenuItem Lib "user32" Alias "EnableMenuItem" (ByVal hMenu As Long, ByVal wIDEnableMenuItem As Long, ByVal wEnable As Long) As Long
Public Declare Function apiGetSystemMenu Lib "user32" Alias "GetSystemMenu" (ByVal hwnd As Long, ByVal flag As Long) As Long

Public Function EnableDisableControlBoxX(bEnable As Boolean, Optional ByVal lhWndTarget As Long = 0) As Long
On Error GoTo Err_EnableDisableControlBoxX

Const MF_BYCOMMAND = &H0&
Const MF_DISABLED = &H2&
Const MF_ENABLED = &H0&
Const MF_GRAYED = &H1&
Const SC_CLOSE = &HF060&

Dim lhWndMenu As Long
Dim lReturnVal As Long
Dim lAction As Long

lhWndMenu = apiGetSystemMenu(IIf(lhWndTarget = 0, Application.hWndAccessApp, lhWndTarget), False)

If lhWndMenu <> 0 Then
If bEnable Then
lAction = MF_BYCOMMAND Or MF_ENABLED
Else
lAction = MF_BYCOMMAND Or MF_DISABLED Or MF_GRAYED
End If
lReturnVal = apiEnableMenuItem(lhWndMenu, SC_CLOSE, lAction)
End If

EnableDisableControlBoxX = lReturnVal

Exit_EnableDisableControlBoxX:
Exit Function

Err_EnableDisableControlBoxX:
    MsgBox "Error:" & Err.Number & vbCrLf & "Description: " & Err.Description
Resume Exit_EnableDisableControlBoxX

End Function

The first one hides the main access window so only shows the forms on the screen if they are set to Pop Up Yes

Sorry.
 
Mike:

What is the code for
Code:
EnableDisableControlBoxX False
 
mike:
Sorry posts must have crosed in cyber space

Have a Star

BTW
what does fAccessWindowdo

Pwise
 
There are a couple of Macros,

One that is fired when the database opens with fAccessWindow("Minimize",False,False)

and the other when it closes with fAccessWindow ("Show", False, False)

In the database all form Pop Up properties should be set to yes.

When the database opens the horrible grey access window is minimized in the tool bar and all the user sees is the actual form, that way the database doesn't really look like an access database as it is much neater.

 
Thanks for the above code, i've tried dumping it in a few places and almost completley broken my DB!!!

where do i need to put the bulk of the code?

i've tried it in a new module, and I've pasted it under the "option compare database" text on the form. i've even tried pasting in the Onload in vba (this really did nearly break it!!)

i'm happy with sticking
Code:
EnableDisableControlBoxX False
in the onload

Thanks
Policechiefwiggum
 
I just have it running in it's own module.

What version of access are you using?

I have it running in 2000/03 and it's fine.

When you say it almost broke your DB what issues where you having?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top