if username & password on userform are error then close file totally after three times

Amer Omar

New Member
Joined
Jan 27, 2024
Messages
20
Office Version
  1. 2019
Platform
  1. Windows
Hi,
I search for project when username & password in TB1,TB2 are error then should close the file totally after three tries without close others files are open , I would when run the form should not show the sheets if username & password in TB1,TB2 are corrct then just show one sheet is the first contains button to show the form and hide the others .
I see some codes if username & password are error and try to open the file again then will show sheets I don't want this way .
always show the form without show contents file when try to write username & password in TB1,TB2 until will be correct the entiries .
thanks in advanced
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
The following code will prompt the user to enter the username and password, and close the workbook when the user fails three times to enter the correct credentials. Note that you'll need to make the necessary changes where specified in the code, and change the name of the controls accordingly.

Control NamePurpose
usernameTextboxTextbox to enter the username
passwordTextboxTextbox to enter the password
loginButtonButton to login
loginFormUserform to login

[ThisWorkbook module]

VBA Code:
Option Explicit

Private Sub Workbook_Open()

    If Not loginForm.isLoginSuccessful() Then
        ThisWorkbook.Saved = True
        ThisWorkbook.Close SaveChanges:=False
        Exit Sub
    End If
   
End Sub

[UserForm module]

VBA Code:
Option Explicit

Const USERNAME As String = "Username" 'change the username as desired
Const PASSWORD As String = "Password" 'change the password as desired
Const LOGIN_ATTEMPTS_ALLOWED As Long = 3 'change as desired

Dim m_success As Boolean

Public Function isLoginSuccessful() As Boolean

    m_success = False
   
    loginForm.Show vbModal
   
    isLoginSuccessful = m_success
   
End Function

Private Sub loginButton_Click()

    Static loginAttempts As Long
   
    With Me.usernameTextbox
        If Len(.Value) = 0 Then
            .SetFocus
            MsgBox "Please enter the username", vbExclamation, "Username"
            Exit Sub
        End If
    End With
   
    With Me.passwordTextbox
        If Len(.Value) = 0 Then
            .SetFocus
            MsgBox "Please enter the password", vbExclamation, "Password"
            Exit Sub
        End If
    End With
   
    If Me.usernameTextbox.Value = USERNAME And Me.passwordTextbox.Value = PASSWORD Then
        m_success = True
        Unload Me
        Exit Sub
    End If
   
    loginAttempts = loginAttempts + 1
   
    If loginAttempts >= LOGIN_ATTEMPTS_ALLOWED Then
        MsgBox "Too many login attempts", vbCritical, "Login"
        m_success = False
        Unload Me
        Exit Sub
    End If
       
    MsgBox "Username and/or password is invalid", vbExclamation, "Invalid"
   
End Sub

Hope this helps!
 
Upvote 0
Hi
thanks for this project, but the question is when open file should just the form without show the file( contents sheets) .
I see the file is visible and I should enable the macro , but I see some project in the internet hide workbook when run the form if the user and password are correct then the workbook is visible otherwise just the form keep visible until the user and password are correct or close the file .
is it possible?
 
Upvote 0
In that case, try the following instead...

[ThisWorkbook module]

VBA Code:
Option Explicit


Private Sub Workbook_Open()
    
    Dim uf As loginForm
    Set uf = New loginForm
    
    uf.Show vbModal
    
End Sub

[UserForm module]

VBA Code:
Option Explicit

Const USERNAME As String = "Username" 'change the username as desired
Const PASSWORD As String = "Password" 'change the password as desired
Const LOGIN_ATTEMPTS_ALLOWED As Long = 3 'change as desired

Dim m_success As Boolean

Private Sub cancelButton_Click()

    m_success = False
    
    Unload Me
    
End Sub

Private Sub loginButton_Click()

    Static loginAttempts As Long
    
    With Me.usernameTextbox
        If Len(.Value) = 0 Then
            .SetFocus
            MsgBox "Please enter the username", vbExclamation, "Username"
            Exit Sub
        End If
    End With
    
    With Me.passwordTextbox
        If Len(.Value) = 0 Then
            .SetFocus
            MsgBox "Please enter the password", vbExclamation, "Password"
            Exit Sub
        End If
    End With
    
    If Me.usernameTextbox.Value = USERNAME And Me.passwordTextbox.Value = PASSWORD Then
        m_success = True
        Unload Me
        Exit Sub
    End If
    
    loginAttempts = loginAttempts + 1
    
    If loginAttempts >= LOGIN_ATTEMPTS_ALLOWED Then
        MsgBox "Too many login attempts", vbCritical, "Login"
        m_success = False
        Unload Me
        Exit Sub
    End If
        
    MsgBox "Username and/or password is invalid", vbExclamation, "Invalid"
    
End Sub

Private Sub UserForm_Initialize()
    ThisWorkbook.Windows(1).Visible = False
End Sub

Private Sub UserForm_Terminate()
    If m_success Then
        ThisWorkbook.Windows(1).Visible = True
        ThisWorkbook.Saved = True
    Else
        ThisWorkbook.Close savechanges:=False
    End If
End Sub

Hope this helps!
 
Upvote 0
Solution

Forum statistics

Threads
1,223,162
Messages
6,170,432
Members
452,326
Latest member
johnshaji

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top