Hiding the MS Access Application Window Control Box

  • Thread starter Thread starter Stoney
  • Start date Start date
S

Stoney

Guest
Does anyone know how to hide the MS Access 2000 application window control box when using toolbars on a form? I have already disabled them on the forms within my application, however, the control box for Minimize, Maximize, Restore and Close still appear in the upper right hand corner of the screen. No errors occur when exiting forms and subforms throughout my application using the Close button on the toolbars. However, when clicking the X in the control box MS Access attempts to quit and close resulting in errors. I have tried to use the logic provided in the Microsoft Knowledge Base article Q210299 ("Maximized form shows control box and Minimize, Maximize, and Restore buttons") but I receive the compile error - "Expected variable or procedure, not module" when executing the code in the Onload property of the forms. Any ideas?
 
This will allow you to disable the 'X' to prevent the user from closing your application...

'Copy this function into a new module...
'Courtesy of Calvin Smith
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:
ErrorMsg "dProgramFunctions", "EnableDisableControlBoxX", Err.Number, Err.Description
Resume Exit_EnableDisableControlBoxX

End Function

'I test if the current user is me (the programmer). Use this in a forms OnOpen event or a transparent command button...
If CurrentUser <> "programmer" Then
EnableDisableControlBoxX (False) 'Disable the programs X (close) function
Exit Sub
Else
EnableDisableControlBoxX (True)
End If

'HTH
 
Thanks ghudson but this isn't working either. When I execute this function I receive a Runtime error 2766 "The object does not contain the Automation Object 'apiEnableMenuItem'.". Do you think this may be library related? It looks like there is a reference to a "user32" library. I don't think I have this. It was also the same library in the declarations of the module provided by microsoft that I tried to run earlier. If so, do you know where I can obtain it? Please Advise...
 
Last edited:
Sounds like a reference problem. I checked my references in a db that the function works in and the only references I have are...

Visual Basic for Applications
Microsoft Access 8.0 Object Library
Microsoft DAO 3.51 Object library

I am using Access 97 with Windows XP.

HTH
 
It works!

After further review I went back and ran the function through a macro using the "false" parameter. I was also executing it in the On Load event which didn't work so I put it in the On Open event.

Thanks a million ghudson!
 
ghudson,
I am also having a bit of a problem with this code. I called it from my main menu form which opens after the user logs in, and the form now doesn't open at all (or it's hidden or something.)

any ideas..:confused: :confused:

dh
 
i can't get it to work!!!

I have as you said put the coding in a module. i am running the command from a comand button from a form. but vba doesn't even get pass

Public Function EnableDisableControlBoxX(bEnable As Boolean, Optional ByVal lhWndTarget As Long = 0) As Long

it gives me a compile error on that line telling me that the sub or function is not defined.!


Any suggestions???? :confused:
 
The code has to be placed in a stand alone module [from the modules tab], not a form module.

This is the code that you would put behind your command button...
Code:
If CurrentUser <> "programmer" Then
    EnableDisableControlBoxX (False) 'Disable the programs X (close) function
    Exit Sub
Else
    EnableDisableControlBoxX (True)
End If
 
That is what i have done. And i tried calling the code from an autoeec macro with the action set to runcode.
and i put the code as following : EnableDisableControlBoxX (False)
 
rk2002 said:
That is what i have done. And i tried calling the code from an autoeec macro with the action set to runcode.
and i put the code as following : EnableDisableControlBoxX (False)
I have never used it in a macro so it must not like what you are trying to do with it. I suggest that you create a public function and put all of the code that you are running in the AutoExec macro in that function. Then call that function from the AutoExec macro.
 
that is what i am doing

ghudson said:
I have never used it in a macro so it must not like what you are trying to do with it. I suggest that you create a public function and put all of the code that you are running in the AutoExec macro in that function. Then call that function from the AutoExec macro.

first i tried calling the function from a command button and it gave me an error. and when i try running it from a macro it also gives me an error and halts the process
 

Users who are viewing this thread

Back
Top Bottom