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

Prevent users closing application with X i.e must use switchboard 5

Status
Not open for further replies.

mwheads

Programmer
Apr 24, 2004
38
0
0
ZA
Hi Everyone

Is there a way with options (disable X) or VBA of preventing users from closing the application with the X. I want my users to only use the switchboard to enter and exit the database.

thanks

Paul
 
Hi!

Yes - I use the method described by mstrmage1768 in faq faq702-1870.

Roy-Vidar
 
Thanks RoyVidar, it does work well but I would like some extra tips if possible just to sort my out my erratic users.
My problem is, that it first softsaves the record before refusing to close and then the undo button is rendered ineffective , I would like if possible for the "Cancel = Not OK2Close" to have absolutely no effect on the record (like clicking on a button with no code) OR to first undo the entire record, then warn the user with a msgbox. I'm not sure if this is possible.
Thanks for the answer though, its a huge step in the right direction.
Regards,
Paul
 
The referenced FAQ is a good method of dealing with the close situation, but only after it has happened. If you to prevent it from happening in the first place, then you can use the following approach. As an added benefit, this code also allows you to change the text in the title bar of the main Access window if you choose.

First, in a code module, declare the following API's and Constants
Code:
Public Const WS_SYSMENU = &H80000
Public Const GWL_STYLE = (-16)

Public Type RECT
    tLng_Left                 As Long
    tLng_Top                  As Long
    tLng_Right                As Long
    tLng_Bottom               As Long
End Type

Public Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Public Declare Function SetWindowText Lib "user32" Alias "SetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String) As Long
Public Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
Public Declare Function MoveWindow Lib "user32" (ByVal hwnd As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal bRepaint As Long) As Long
In Application Start-Up form, drop in the following code in the two events indicated, and add the one subroutine:
Code:
Option Explicit

Dim fLng_OrigWindWord      As Long

'==============================================================

Private Sub Form_Load()
   
   Dim lLng_HideAccessWord    As Long
   
   fLng_OrigWindWord = GetWindowLong(hWndAccessApp, GWL_STYLE)
   lLng_HideAccessWord = fLng_OrigWindWord And (Not WS_SYSMENU)
   SetWindowLong hWndAccessApp, GWL_STYLE, lLng_HideAccessWord

[COLOR=green]   ' If you don't want to change the Title Bar text, 
   ' then send "" as the parameter, exactly as the call
   ' appears in the Form_Unload event.[/color]
   RedrawTitleBar "Place your Customized Access Title Bar Text Here"
   
End Sub

'==============================================================

Private Sub Form_Unload(Cancel As Integer)
   
   SetWindowLong hWndAccessApp, GWL_STYLE, fLng_OrigWindWord
   RedrawTitleBar ""
   
End Sub

'==============================================================

Private Sub RedrawTitleBar(rStr_TitleBarText As String)

   Dim lRct_WinPos         As RECT
   Dim lLng_WinWidth       As Long
   Dim lLng_WinHeight      As Long
   
   SetWindowText hWndAccessApp, IIf((Len(rStr_TitleBarText) < 1), "Microsoft Access", rStr_TitleBarText)
   GetWindowRect hWndAccessApp, lRct_WinPos
   With lRct_WinPos
      lLng_WinWidth = .tLng_Right - .tLng_Left + 1
      lLng_WinHeight = .tLng_Bottom - .tLng_Top + 1
[COLOR=green]      ' Not really moving the window, just repainting it[/color]
      MoveWindow hWndAccessApp, .tLng_Left, .tLng_Top, lLng_WinWidth, lLng_WinHeight, True
   End With

End Sub


Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
My goodness, its super hectic, I hope I get it right. I will give it a go. thanks,

Most of of my application is not critical. Its only 2 of my forms that I can't have my users quit before they balance their entries.
I will be content for the the first method to undo their record before it softsaves it. i.e all or nothing.

I will certainly try the above, but I hope its easier than it looks.
 
mwheads,

cajun'c code is good.....not too difficult to implement in my opinion. As for mine, we may be able to figure out a way to prevent the save. I have to do some testing, and maybe some of the other guys can help me out here, but if we check the OK2Close during the BeforeUpdate property of the form itself and it is False, we can cancel out. This will prevent any saving, retain the undo option, and give you the functionality you are looking for. I'll do some testing and get back to you.

=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)

Robert L. Johnson III
CCNA, CCDA, MCSA, CNA, Net+, A+
w: rljohnso@stewart.com
h: wildmage@tampabay.rr.com
 
Perhaps I'm not the best judge, but the code does look more hectic, to use your word, than it actually is.

If you have any questions on what is going on in that code, please feel free to ask and I'll do my best to explain it.

Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Hi CajunCenturion
How do I declare the API's and Constants? Is that in a class module or a code module. I'm not sure where that first step go's or how.
The second step (Application Start-Up form)....Would that be in Load and Unload of my Switchboard only.
Does it disable X across the entire application?
Does it change the functionality of commandbuttons in any way?

It does look very promising, Thanks Cajun and Mstrmage1768 and for all the help.

 
The API declarations go in a standard code module, not a class, not a form, and not the Switchboard.

If your application starts with a Switchboard, then yes, the code should go in the Load and Unload events of the Switchboard, and the additional stand alone subroutine also in the Switchboard code.

This code will remove the X from the Main Access Window. You can easily control the X button in individual forms by using the standard form properties.

It will not affect any control's functionality.

Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
This link from the microsoft website, may be of use to someone...

TITLE
How to disable the Close button on the Application window and the Exit command on the File menu

SUMMARY
Microsoft Access has no built-in method for disabling the Close button (X) on the application window, the Close command on the System menu of the application window, or the Exit command on the File menu. This article describes how to programmatically disable all of these.

...it does work!!


Regards,


Aubs
 
Excellent Code... better than the Cancel = True solution I had in place with a global variable...

Steve Medvid
&quot;IT Consultant & Web Master&quot;

Chester County, PA Residents
Please Show Your Support...
 
How does it work for users having a familiarity with ALT+F4, CTRL+F4, CTRL+W...?

Roy-Vidar
 
I have a hidden form that was already in use so I attatched the above code from CajunCenturion and placed it in that form. The code works great but when the user exits the database it "appears" access is still open becuase the screen does not refresh. I tried to close the Hidden form before the docmd.quit but I got a type mismatch error. Any ideas or suggestions??
 
Just a quick post to say thanks to Cajun~! I have been looking for this solution forever. VERY easy to implement.

A star and many more for ya!
 
CajunCenturion,

I used your code to remove the window control buttons on my titlebar as well as change the caption.
However, is it still possible to have a (custom) icon in the titlebar, cause the standard MS Access icon gets removed with this code ?
Also, is it possible to hide the text after my custom caption text ? It says something like [myformname:form].
 
Yes, you can replace the standard icon with a customized icon. I have the code on a CD somewhere and I'll post it later on today, if work permits. Otherwise, I'll post it this evening.

I'm not sure about the text after your custom caption because I've not experienced that situation.

--------------
Good Luck
To get the most from your Tek-Tips experience, please read FAQ181-2886
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Ok, here it is.

The following code allows you to set the icon for any window, provided that you have the handle to window. I would suggest that you place the code in a module so that it would available throughout the application.

First, in the declarations section of the module, place the following declarations:
Code:
Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Integer, ByVal lparam As Long) As Long
Public Declare Function ExtractIcon Lib "shell32.dll" Alias "ExtractIconA" (ByVal hInst As Long, ByVal lpszExeFileName As String, ByVal nIconIndex As Long) As Long

Public Const WM_GETICON = &H7F
Public Const WM_SETICON = &H80
Public Const ICON_BIG = 1
Then add in the following function
Code:
Function WindowSetIcon(rLng_WindowHandle As Long, rStr_IconPath As String) As Boolean
    
   On Error GoTo ErrorHandler
   
   Dim lLng_RetVal               As Long
   Dim lLng_IconHandle           As Long
   Dim lBol_ProcOK               As Boolean
   
   lBol_ProcOK = False
   lLng_IconHandle = ExtractIcon(0, rStr_IconPath, 0)
   If (lLng_IconHandle > 0) Then
      lLng_RetVal = SendMessage(rLng_WindowHandle, WM_SETICON, False, lLng_IconHandle)
      lBol_ProcOK = True
   Else
      MsgBox "Unable to Extract Icon from Filename:  " & rStr_IconPath
   End If
   
Exit_WindowSetIcon:

   WindowSetIcon = lBol_ProcOK

Exit Function
   
ErrorHandler:

   MsgBox "Error Encountered:  " & Err.Number & " -- " & Err.Description
   lBol_ProcOK = False
   Resume Exit_WindowSetIcon
   
End Function
For each form, then I would add the following call in the Form_Load event
Code:
Dim lBol_SetIcon As Boolean

lBol_SetIcon = WindowSetIcon(Me.hWnd, "C:\...\...\<IconFileName>.ICO")

--------------
Good Luck
To get the most from your Tek-Tips experience, please read FAQ181-2886
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Hmm, this might work but the problem is I already used your code to hide the X button and set a custom caption. And because of this my standard Access icon has disappeared as well.
I'm not getting any error with this last bit of code, but I do not see my custom icon either. Probably because it replaces the standard icon which is hidden as well. :(
 
I don't know why you're having that difficulty, but it could be from any number of reasons.

I would remove all three functions (hide system menu, set custom caption, and replace icon) and then add them back one at a time. I would do the icon first, the caption second, and the menu third. This should give us some idea of which step if causing the problem.

It could also be in your code, in the version of Access that you're using, or in the version of your OS.

--------------
Good Luck
To get the most from your Tek-Tips experience, please read FAQ181-2886
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top