Results 1 to 6 of 6
  1. #1
    Join Date
    Jun 2005
    Posts
    102

    Unanswered: Disabling Database Application Control Box

    Is there anyway you can disable to Control box in Access, I'm not talking about a form/report control box, I'm talking about the control box on the very top, the one for the database/application itself. Because I don't want the user clicking that to exit Access, I want them to click the button on the Switchboard instead to exit Access.

  2. #2
    Join Date
    Nov 2003
    Posts
    1,487
    You don't really need to disable the entire control box (besides, it has a pretty Icon in it ). Sounds like all you want to do is simply disable the Close feature of the Access Parent Window. You can do this with some API calls in VBA code. Below is a function that will disable the Close button [X] located on the far right side of the Title Bar and it will also remove the Close menu item from within the Control Box. This can actually apply to any Form. Simply pass the Form's Handle to the provided DisableFormClose function:

    Simply place the code below into a Database Code Module:
    Code:
    Private Const FFBYPOSITION = &H400
    Private Const FFREMOVEWHAT = &H1000
    Private Declare Function DrawMenuBar Lib "user32" (ByVal hwnd As Long) As Long
    Private Declare Function GetMenuItemCount Lib "user32" (ByVal hMenu As Long) As Long
    Private Declare Function GetSystemMenu Lib "user32" (ByVal hwnd As Long, _
    				ByVal bRevert As Long) As Long
    				   
    Private Declare Function RemoveMenu Lib "user32" (ByVal hMenu As Long, _
    				ByVal nPosition As Long, ByVal wFlags As Long) As Long
    
    Public Function DisableFormClose(fhWnd)
       ' This Function will disable a Forms' Title Bar Close button [X]
       ' and it will hide the close menu item from within the Title Bar
       ' Control Box.
       Dim MItem As Long
       Dim MItemCount As Long
       ' Get the Handle to the form's system menu
       MItem = GetSystemMenu(fhWnd, 0)
       
       If MItem Then
    	   
    	  ' Obtain the number of items in the menu
    	   MItemCount = GetMenuItemCount(MItem)
    	 
    	  ' Get rid of the system menu Close menu item.
    	  ' The last item on the menu is MItemCount - 1
    	   Call RemoveMenu(MItem, MItemCount - 1, FFREMOVEWHAT Or FFBYPOSITION)
    	
    	  ' Get rid of the system menu separator line
    	   Call RemoveMenu(MItem, MItemCount - 2, FFREMOVEWHAT Or FFBYPOSITION)
    	 
    	  ' Force a redraw of the menu. This
    	  ' refreshes the titlebar, Dimming the X
    	  ' to indicate it is disabled.
    	  Call DrawMenuBar(fhWnd)
       End If
    End Function
    Now place this line into the OnOpen event of your Database Startup Form:

    Call DisableFormClose(Application.hWndAccessApp)

    There ya go...

    .
    Environment:
    Self Taught In ALL Environments.....And It Shows!


  3. #3
    Join Date
    Jun 2005
    Posts
    102
    Hey thanks Cyberlunx!

  4. #4
    Join Date
    Dec 2002
    Location
    Préverenges, Switzerland
    Posts
    3,740

    off-thread, sorry

    hey CyberLynx

    do you have a similar trick to intercept restore?

    izy
    currently using SS 2008R2

  5. #5
    Join Date
    Nov 2003
    Posts
    1,487
    Since there is a little interest in this...I have reworked and renamed the earlier provided function. The comments within the funtion pretty much explain things. with this reworked function, you can remove any Title Bar Control Box menu item you like. Either menu items 1, 2, 3... or all Control Box menu items. whichever ones you want. The new function is named: DisableCtrlBoxItems()

    Again....place the following into the declaration section of a database code module:
    Code:
    Private Const FFBYPOSITION = &H400
    Private Const FFREMOVEWHAT = &H1000
    Private Declare Function DrawMenuBar Lib "user32" (ByVal hwnd As Long) As Long
    Private Declare Function GetMenuItemCount Lib "user32" (ByVal hMenu As Long) As Long
    Private Declare Function GetSystemMenu Lib "user32" (ByVal hwnd As Long, _
    				ByVal bRevert As Long) As Long
    Private Declare Function RemoveMenu Lib "user32" (ByVal hMenu As Long, _
    				ByVal nPosition As Long, ByVal wFlags As Long) As Long
    Place this function code into the same module:
    Code:
    Public Function DisableCtrlBoxItems(fhWnd As Long, MenuItemToRemove As String)
       ' This Function will remove desired menu items from a Forms' Title Bar
       ' Control Box menu items and disable the Close button [X].
       '
       ' fHwnd:
       ' ======
       ' To contain the Handle of the Window or Form we want to control.
       '
       ' MenuItemToRemove:
       ' =================
       ' Remove menu items from the Title Bar Control Box simple supply
       ' the number (or numbers seperated by a comma, semi-colon, or colon)
       ' that corresponds to the menu item(s) you want removed. Here is a
       ' list of corresponding numbers:
       '
       ' 1 = The CLOSE Item
       ' 2 = The SEPERATOR LINE above the Close manu item
       ' 3 = The MAXIMIZE menu item
       ' 4 = The MINIMIZE menu item
       ' 5 = The SIZE menu item
       ' 6 = The MOVE menu item
       ' 7 = The RESTORE Menu Item.
       ' All = Supply the word "ALL" to remove ALL THE MENU ITEMS
       
       ' Declare Variables
       Dim MItemHwnd As Long, MItemCount As Long, X As Long
       Dim I As Integer
       Dim MM As String, Strg As String
     
       ' Get the Handle to the form's system menu
       MItemHwnd = GetSystemMenu(fhWnd, 0)
       ' If the Window has no System Menu then exit this function.
       ' No sense doing work if we don't have to.
       If MItemHwnd = 0 Then Exit Function
       
       If MItemHwnd Then
    	  ' Get the number of items located within the system menu
    	   MItemCount = GetMenuItemCount(MItemHwnd)
    	   ' If an menu item passed to this function does not exist.
    	   ' Again, no sense doing work if we don't have to.
    	   If (MItemCount < Val(MenuItemToRemove) Or Val(MenuItemToRemove) = 0) _
    		  And UCase(MenuItemToRemove) <> "ALL" Then Exit Function
    	   ' If "ALL" is passed in the 'MenuItemToRemove' parameter then
    	   ' build the new content for this parameter that will include
    	   ' all menu items within the control box. Some applications add
    	   ' items to the control box.
    	   If UCase(MenuItemToRemove) = "ALL" Then
    		  MenuItemToRemove = ""
    		  I = MItemCount
    		  Do Until I = 0
    			 MenuItemToRemove = MenuItemToRemove & I & ";"
    			 I = I - 1
    		  Loop
    		  If Right$(MenuItemToRemove, 1) = ";" Then
    			 MenuItemToRemove = Left$(MenuItemToRemove, Len(MenuItemToRemove) - 1)
    		  End If
    	   End If
    	   
    	   ' Enumerate through the Title Bar Control Box menu items
    	   ' and parse through the 'MenuItemToRemove' parameter so as
    	   ' to remove each desired menu item.
    	   For X = 1 To MItemCount
    		  Strg = ""
    		  For I = 1 To Len(MenuItemToRemove)
    			 MM = Mid$(MenuItemToRemove, I, 1)
    			 If MM = ";" Or MM = ":" Or MM = "," Then
    			   MenuItemToRemove = Right$(MenuItemToRemove, Len(MenuItemToRemove) - I)
    			   Exit For
    			 End If
    			 Strg = Strg & MM
    		  Next I
    		  If Strg > "" And CInt(Val(Strg)) = X Then
    			 ' API call to remove the menu item.
    			 Call RemoveMenu(MItemHwnd, MItemCount - X, FFREMOVEWHAT Or FFBYPOSITION)
    		  End If
    	   Next X
    	
    	  ' API call to force a redraw of the menu. This refreshes
    	  ' the titlebar. It Also Dims the Close button [X] to
    	  ' indicate it is disabled.
    	  Call DrawMenuBar(fhWnd)
       End If
    End Function
    Now in your Startup Forms' OnOpen event, enter something like this:

    Call DisableCtrlBoxItems(Application.hWndAccessApp, "1,2,7")

    This call will remove the CLOSE menu item, remove the SEPERATOR LINE located above the CLOSE menu item, and it will also remove the RESTORE menu item from the MS-Access Title Bar Control Box. Again, you can apply this to any Parent or child Form.

    Hope this helps...

    .
    Environment:
    Self Taught In ALL Environments.....And It Shows!


  6. #6
    Join Date
    Dec 2002
    Location
    Préverenges, Switzerland
    Posts
    3,740
    yum yum
    playing tomorrow morning

    tks

    izy
    currently using SS 2008R2

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •