I have a project that when opened automatically shows a userform for LOGIN/PASSWORD. Everything works great except that if the user clicks the Close(x) button included in the title bar of the userform, the userform is exited but the workbook is still open to its first page.

Is there any code snippet that will cause the project to close completely if the Close(X) Button is clicked to prevent entry into my program other than with proper LOGIN & USERNAME.

Unload the workbook form from the unload method in the password form.

As WaltP pointed out, unload ALL forms in the application OR you can disable the window "x" button as in -

'In a module, add the following code...
Option Explicit

Private Const MF_BYPOSITION = &H400
  Private Const MF_REMOVE = &H1000

  Private Declare Function DrawMenuBar Lib "User32" (ByVal hWnd As Long) As Long

  Private Declare Function GetMenuItemCount Lib "User32" (ByVal hMenu As Long) As Long
  Private Declare Function GetSystemMenu Lib "User32" (ByVal hWnd As Long, ByVal bRevert As Long) As Long
  Private Declare Function RemoveMenu Lib "User32" (ByVal hMenu As Long, ByVal nPosition As Long, ByVal wFlags As Long) As Long
Public Sub RemoveX(frm As Form)

  Dim hMenu As Long
  Dim menuItemCount As Long

  hMenu = GetSystemMenu(frm.hWnd, 0)
  
  If hMenu Then
      
      menuItemCount = GetMenuItemCount(hMenu)
    
      Call RemoveMenu(hMenu, menuItemCount - 1, MF_REMOVE Or MF_BYPOSITION)
   
      Call RemoveMenu(hMenu, menuItemCount - 2, MF_REMOVE Or MF_BYPOSITION)
    
      Call DrawMenuBar(frm.hWnd)
   End If
End Sub

Now just call the code on your form as in -

Call RemoveX (Me)

If you want to unload all forms, the following will apply -

'In a module the following...
Public Sub UnloadAllForms()

Dim Form As Form
   For Each Form In Forms
      Unload Form
      Set Form = Nothing
   Next Form
End Sub

'Call in your unload event of your form...

Call UnloadAllForms

Solved, Thank you

Pleasure. Please mark this thread as solved as well, thanks:)

Please mark this as solved, found at the bottom of this page, thanks.:)

actually this code is more complicated. You are manipulating API for a simple PROBLEM,
if you have just observed, there are no programs that uses this function. i.e it's very unprofessional.

set the Form to This:

Form1.ControlBox = False
Form1.BorderStyle = 1

if you don't put a caption in the form, the title bar wont appear
let's complicate this.

You can also add this code to alert a user before closing a (any) form.

Private Sub Form_Unload(Cancel As Integer)

Dim strMess
If LogOff = False Then

    strMess = "You are about to close the Automated Payroll System." & vbCrLf & vbCrLf & "Are you sure?"
    
    If MsgBox(strMess, vbQuestion + vbYesNo, "Exit Confirmation") = vbYes Then
        End
        
    Else
        
        Cancel = 1
        
    End If
    
ElseIf LogOff = True Then

    Unload Me
    Form1.Show
    
End If

End Sub

Please mark thread as solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.