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

Turn off exit button on user form 2

Status
Not open for further replies.

ninash

Technical User
Jul 6, 2001
163
GB
Hi Again,

Is there anyway to turn off the exit button on a user form as it is creating a hole in my security that needs to be plugged.

Thanks in advance
 
call the following routine with
HideCloseButton <formObject>

insert into a module:
'>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
'Find the Dialog's Window
Private Declare Function FindWindow Lib &quot;user32&quot; Alias &quot;FindWindowA&quot; (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

'Get the current window style
Private Declare Function GetWindowLong Lib &quot;user32&quot; Alias &quot;GetWindowLongA&quot; (ByVal hWnd As Long, ByVal nIndex As Long) As Long

'Set the new window style
Private Declare Function SetWindowLong Lib &quot;user32&quot; Alias &quot;SetWindowLongA&quot; (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long

Const WS_SYSMENU = &H80000
Const GWL_STYLE = (-16)

'Routine to hide the close button on a userform or dialogsheet
' oDialog is either the Userform or Dialog object

Sub HideCloseButton(oDialog As Object)

Dim hWnd As Long, lStyle As Long

'Were we given a userform or a dialog sheet
If TypeName(oDialog) = &quot;DialogSheet&quot; Then

'We had a dialog sheet. Note that pressing Escape still closes the dialog

Select Case Int(Val(Application.Version))
Case 5 'Doesn't work in Excel 5 - we only have 32-bit DLL calls here
Case 7 'Excel 95
hWnd = FindWindow(&quot;bosa_sdm_XL&quot;, oDialog.DialogFrame.Caption) 'DialogSheet
Case 8 'Excel 97
hWnd = FindWindow(&quot;bosa_sdm_XL8&quot;, oDialog.DialogFrame.Caption) 'DialogSheet
Case 9 'Excel 2000
hWnd = FindWindow(&quot;bosa_sdm_XL9&quot;, oDialog.DialogFrame.Caption) 'DialogSheet
End Select
Else
'We had a userform
Select Case Int(Val(Application.Version))
Case 8 'Excel 97
hWnd = FindWindow(&quot;ThunderXFrame&quot;, oDialog.Caption) 'UserForm
Case 9 'Excel 2000
hWnd = FindWindow(&quot;ThunderDFrame&quot;, oDialog.Caption) 'UserForm
End Select
End If

'Get the current window style
lStyle = GetWindowLong(hWnd, GWL_STYLE)

'Turn off the System Menu bit
SetWindowLong hWnd, GWL_STYLE, lStyle And Not WS_SYSMENU

End Sub

'<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< ide
 
The following code if place in teh UserForm codepane will disable the &quot;x&quot; button :
Code:
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
    If CloseMode = 0 Then
        Cancel = True
    End If
End Sub
AC
 
Ninash,
...
but: you need to disable the ALT+F4 (close application) combination, too. ide
 
Ide,

Setting Cancel = True in the QueryClose event will disable the &quot;x&quot;, Alt-F4 an the menu Close command.

It does leave them intact, just ignores them.

AC
 
acron,
ohh, yees !
i didn' t read it..

[blush]
ide ide
 
Thats Guys
You have got me out of a very tight spot
 
Hi, another way, sort of similar. Don't know what it does different to that one up there ^^^

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If Cancel <> 1 then
Cancel = 1
End If
End Sub


 
Setting

If Cancel <> 1 then
Cancel = 1
End If

means you never close the form , not even with a Cancel or Close button. The Unload Me statement would be intercepted and canceled.

The code posted above does not disable the form from being closed by code, wheras yous does.

Setting Cancel = 1 is the same as Cancel = True, but in your code it happens regardless. Maybe that is what you might want, but could prove cumbersome if you need a tidy shutdown.

AC
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top