From: Salad on 25 Mar 2010 19:30 John von Colditz wrote: > It happens that Salad formulated : > >> John von Colditz wrote: >> >>> I am modifying an existing application for a client. None of the >>> forms has a close or exit button, so users are in the habit of just >>> clicking on the X in the upper right of the forms. I was informed >>> today that users occasionally click the X for the application by >>> mistake, and the entire database closes. How do I trap that in code, >>> give a Yes/No option on closing, and then cancel if necessary? >>> >>> Thanks! >>> >>> John >>> >>> >> I did this with very little testing. It appears to work, but I'd test >> further. >> >> I created a module. The code is >> Public Function OpenApp() As Boolean >> DoCmd.OpenForm "Table1", , , , , acHidden >> DoCmd.OpenForm "Junk" >> OpenApp = True >> End Function >> >> That opens form Table1 invisible and form Junk as visible. >> >> Form Table1 is a bound form to table Table1 (a table with 2 fields; >> TestID and Test). I added the a checkbox field to the form called >> CloseIt. When the form is opened I set a value, "New Data" to the >> "Test" field simply to dirty the record. On the Unload I check for >> the value of the checkbox CloseIt. The default value of CloseIt is >> false. If closeit is true, the app shuts down. If false, the user is >> asked whether the app should be closed. >> Private Sub Form_Current() >> Me.Test = "New Data" >> End Sub >> Private Sub Form_Unload(Cancel As Integer) >> If Not Me.CloseIt Then >> If MsgBox("Close the app?", vbYesNo, "Confirm") = vbNo Then >> Cancel = True >> End If >> Me.CloseIt = False >> Else >> Me.Undo >> End If >> End Sub >> >> >> The form Junk opens up. If the op presses the Exit button, it sets >> the CloseIt checkbox to True in form Table1. Thus if I press the Exit >> button, the app quits. If I press the X button to close Access, I am >> prompted and asked whether or not I want to exit the app. If I answer >> Yes, CloseIt in form Table1 is set to true and the app quits. If I >> answer No, from Junk remains open like I did nothing. >> Private Sub CommandExit_Click() >> Forms!Table1!CloseIt = True >> Application.Quit >> End Sub >> >> I have an Autoexec macro that RunCode's "OpenApp" when the app loads. >> Personally, I'd add the Min/Max/Close buttons to the forms. > > > Too much going on with my application closing, and I couldn't make this > work! > > Thanks anyway! > > The example Rich gave looked clean and easier to implement.
From: The Frog on 26 Mar 2010 03:43 Have you tried just killing the title bar altogether? The code below is not mine, but I have found it most useful for controlling excatly this situation. User have no choce but to use your forms :-) Standard module: '********************************************************************************* '********************************************************************************* ' useage: ' AccessTitleBar(True) turns the title bar on ' AccessTitleBar(False) turns it off completely ' Buttons(True) turns on the control buttons for the title bar ' Buttons(False) turns them off 'Both of these are functions that return a long if you want it to 'otherwise use them like: Call FunctionName(True/False) '******************************************************************************** '******************************************************************************** Option Compare Database Option Explicit Private Const GWL_STYLE = (-16) Private Const WS_CAPTION = &HC00000 Private Const WS_MINIMIZEBOX = &H20000 Private Const WS_MAXIMIZEBOX = &H10000 Private Const WS_SYSMENU = &H80000 Private Const SWP_NOSIZE = &H1 Private Const SWP_NOMOVE = &H2 Private Const SWP_NOZORDER = &H4 Public Const SWP_FRAMECHANGED = &H20 Private Declare Function GetWindowLong _ Lib "user32" Alias "GetWindowLongA" ( _ ByVal hwnd As Long, _ ByVal nIndex As Long _ ) As Long 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 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 ' ************************************************** ' Function AccessTitleBar(Show As Boolean) As Long Dim hwnd As Long Dim nIndex As Long Dim dwNewLong As Long Dim dwLong As Long Dim wFlags As Long hwnd = hWndAccessApp nIndex = GWL_STYLE wFlags = SWP_NOSIZE + SWP_NOZORDER + SWP_FRAMECHANGED + SWP_NOMOVE dwLong = GetWindowLong(hwnd, nIndex) If Show Then dwNewLong = (dwLong Or WS_CAPTION) Else dwNewLong = (dwLong And Not WS_CAPTION) End If Call SetWindowLong(hwnd, nIndex, dwNewLong) Call SetWindowPos(hwnd, 0&, 0&, 0&, 0&, 0&, wFlags) End Function Function Buttons(Show As Boolean) As Long Dim hwnd As Long Dim nIndex As Long Dim dwNewLong As Long Dim dwLong As Long hwnd = hWndAccessApp nIndex = GWL_STYLE Const wFlags = SWP_NOSIZE + SWP_NOZORDER + SWP_FRAMECHANGED + SWP_NOMOVE Const FLAGS_COMBI = WS_MINIMIZEBOX Or WS_MAXIMIZEBOX Or WS_SYSMENU dwLong = GetWindowLong(hwnd, nIndex) If Show Then dwNewLong = (dwLong Or FLAGS_COMBI) Else dwNewLong = (dwLong And Not FLAGS_COMBI) End If Call SetWindowLong(hwnd, nIndex, dwNewLong) Call SetWindowPos(hwnd, 0&, 0&, 0&, 0&, 0&, wFlags) End Function Cheers The Frog
From: Salad on 26 Mar 2010 13:36 The Frog wrote: > Have you tried just killing the title bar altogether? The code below > is not mine, but I have found it most useful for controlling excatly > this situation. User have no choce but to use your forms :-) Hi Frog. No I haven't. Unlike the OP, I permit min/max/close buttons on my form. And my apps haven't had the need to hide the main close button of Access that I'm aware of. Perhaps if I was writing a Touch Screen app it would be beneficial. I try to keep things as expected as a Window's app. But it's nice to know there's a method to do so. > > Standard module: > '********************************************************************************* > '********************************************************************************* > ' useage: > ' AccessTitleBar(True) turns the title bar on > ' AccessTitleBar(False) turns it off completely > ' Buttons(True) turns on the control buttons for the title > bar > ' Buttons(False) turns them off > 'Both of these are functions that return a long if you want it to > 'otherwise use them like: Call FunctionName(True/False) > '******************************************************************************** > '******************************************************************************** > Option Compare Database > > Option Explicit > > > Private Const GWL_STYLE = (-16) > Private Const WS_CAPTION = &HC00000 > Private Const WS_MINIMIZEBOX = &H20000 > Private Const WS_MAXIMIZEBOX = &H10000 > Private Const WS_SYSMENU = &H80000 > > > Private Const SWP_NOSIZE = &H1 > Private Const SWP_NOMOVE = &H2 > Private Const SWP_NOZORDER = &H4 > Public Const SWP_FRAMECHANGED = &H20 > > > Private Declare Function GetWindowLong _ > Lib "user32" Alias "GetWindowLongA" ( _ > ByVal hwnd As Long, _ > ByVal nIndex As Long _ > ) As Long > > > 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 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 > ' ************************************************** > ' > > > Function AccessTitleBar(Show As Boolean) As Long > Dim hwnd As Long > Dim nIndex As Long > Dim dwNewLong As Long > Dim dwLong As Long > Dim wFlags As Long > > > hwnd = hWndAccessApp > nIndex = GWL_STYLE > wFlags = SWP_NOSIZE + SWP_NOZORDER + SWP_FRAMECHANGED + SWP_NOMOVE > > > dwLong = GetWindowLong(hwnd, nIndex) > > > If Show Then > dwNewLong = (dwLong Or WS_CAPTION) > Else > dwNewLong = (dwLong And Not WS_CAPTION) > End If > > > Call SetWindowLong(hwnd, nIndex, dwNewLong) > Call SetWindowPos(hwnd, 0&, 0&, 0&, 0&, 0&, wFlags) > End Function > > > Function Buttons(Show As Boolean) As Long > Dim hwnd As Long > Dim nIndex As Long > Dim dwNewLong As Long > Dim dwLong As Long > > > hwnd = hWndAccessApp > nIndex = GWL_STYLE > > > Const wFlags = SWP_NOSIZE + SWP_NOZORDER + SWP_FRAMECHANGED + > SWP_NOMOVE > Const FLAGS_COMBI = WS_MINIMIZEBOX Or WS_MAXIMIZEBOX Or WS_SYSMENU > > > dwLong = GetWindowLong(hwnd, nIndex) > > > If Show Then > dwNewLong = (dwLong Or FLAGS_COMBI) > Else > dwNewLong = (dwLong And Not FLAGS_COMBI) > End If > > > Call SetWindowLong(hwnd, nIndex, dwNewLong) > Call SetWindowPos(hwnd, 0&, 0&, 0&, 0&, 0&, wFlags) > End Function > > > Cheers > > The Frog
From: The Frog on 29 Mar 2010 03:29
Hi Salad, You have nailed it in one. Our external field sales group use touch screen laptops (not my idea). I tried to eliminate any inadvertant 'touches' that would cause operational problems. Since then I use it with almost any app, and I leave the forms with their controls for the users to operate. So far pretty clean. I am going to have a play with the hiden form method - it seems messy to me. I like clean and functional :-) Cheers The Frog |