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

Prevent user to close access app from taskbar 3

Status
Not open for further replies.

borg2002

Programmer
Aug 7, 2002
84
US
Hello,

I am trying to force users to exit an application by using the close button on the form. I already managed to disable the default close button, but then I realized you can still quit the program using the right mouse button on the program's icon in the taskbar. Any ideas on how to prevent this?

Thanks in advance
Borg
 
Borg:

I use a technique that incorporates a hidded form with one check box control.

Form Name: frmExit
Control Name: chkExit
Default value of chkExit: False

In the form's On Unload event use this code:

Private Sub Form_Unload(Cancel As Integer)

If chkExit = False Then
Cancel = -1
End If

End Sub

You will need (if you don't already have it) a main menu/switchboard form with a Quit App button.

In the On Click event of that button put this code:

Forms!frmExit.chkExit = True

This will prevent the user from closing the app in any way other than the Quit App button.

Larry De Laruelle
ldelaruelle@familychildrenscenter.org

 
Thank you for your fast response Larry.
But in my application there is one form that must remain open until the close button is pressed. However, when I use your approach, this form is closed too and I only have an empty access screen left. I am probably doing something wrong, i guess.... Where can i define that the form frmExit is hidden? Do I have to open frmExit when the application starts?

Thank you
Borg
 
Here is a method that I use. It not only keeps a user from closing from the taskbar but also from the X on the menubar. Beaware that with this method, you need to provide (through code) a way for the user to close the application.

First Create a Class Module and paste the following code into it:
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 create another module with this code:
Public sub DisableClose()
'DISABLES THE CLOSE BUTTON AND MENU ITEM, SEE modDisableCloseButton
Dim c As CloseCommand
Set c = New CloseCommand
c.Enabled = False
end Sub


Then create a macro called AutoExec that runs the DisableClose function above.


Hope this makes sense to you.

Pat B
 
Thanks for your solution, but the people who requested this application explicitly asked me not to use the autoexec macro. I already managed to disable the X and the menus. The only thing left is to disable the option to close the application when clicking with the right mouse button on the icon in your taskbar. Larry provided a solution but I can't seem to use me.hide to hide the form...
Anyway, thanks for your help and quick reponse.

Kind regards
Borg
 
Interesting that they do not want you to use an AutoExec macro.

I am assuming that you have a form that opens on startup. Place the DisableClose Function as the first thing to happen during the Open event of that form.

It show accomplish the same results.
Pat B
 
A variation of Larry's solution would be to use that particular form. It doesn't need to be hidden, a visible one will do as well.

'============start=============
Option Compare Database
Option Explicit

Dim ExitButtonPressed As Boolean

'CloseButton should be the name of your button on the form
Sub CloseButton_Click()
ExitButtonPressed = True
'...the rest of the code
Quit 'or Close, whatever...
End Sub


Private Sub Form_Unload(Cancel As Integer)
If ExitButtonPressed Then Exit Sub
Cancel = True
MsgBox "Hey fellow, please click the button to close the app!"
End Sub
'============End===========================

Good luck
[pipe]
Daniel Vlas
Systems Consultant
danvlas@yahoo.com
 
Bustell and Danvlas, thank you very much for your responses. I tried to implement Daniels idea, but the boolean ExitButtonPressed always has the value false. Even though I assign it the value True when the close button is clicked. In the Form_Unload function the boolean then has the value False. I already changed the subs into public ones. Anyone a clue?
Here's my code...

Public ExitButtonPressed As Boolean

Public Sub Form_Unload(Cancel As Integer)
If ExitButtonPressed = True Then
Exit Sub
Else
Cancel = True
MsgBox "Hey fellow, please click the button to close the app!"
End If
End Sub

Public Sub cmdsluiten_Click() 'Function to close application
On Error GoTo Err_cmdSluiten_Click

ExitButtonPressed = True
Application.Quit acPrompt

Exit_Me:
Exit Sub

Err_cmdSluiten_Click:
MsgBox Err.Description
Set rstmat = Nothing
Resume Exit_Me
End Sub

Thanks guys...
 
Form_Unload is a private sub (form event procedure), so leave it in the form, don't move it to a module...

Actually, I don't know what you did and where you put the original code. So I'll explain it a little further:
Open the form in Design View
Go to View-Code
Paste the code
Save the form
Try it.

Good luck,

Dan



[pipe]
Daniel Vlas
Systems Consultant
danvlas@yahoo.com
 
Check out faq702-1870. I have used this to prevent ANY closing of the database except by my close button.... If we knew what it was we were doing, it would not be called research, would it? - Albert Einstein [atom]

Robert L. Johnson III, A+, Network+, MCP
Access Developer/Programmer
robert.l.johnson.iii@citigroup.com
 
Thanks for the help Daniel...
When I used boolean values it didn't seem to work. So I used integer values and this seems to do the trick.
I declared the ExitButtonPressed boolean as public in a module i am using, which seems to work. Except that when i change the value from false to true and the program gets into the Form_Unload procedure, the value is automatically set back to false. Resulting in the fact that I couldn't quit the application anymore... ;-))
With the corresponding integer values it works fine. Thanks for your solution. It really helped me a lot...
[thumbsup2]
Kind Regards
Borg
 
Thanks for your input Robert.
Everything works fine now...

Kind Regards
Borg
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top