Form for multi-user Login Credentials

skf786

Board Regular
Joined
Sep 26, 2010
Messages
156
hello,

im using the following code for a form for user login credentials. i would like to:

1) extend the form for multi users - so i can add a user 'abc' (password: 123), 'def' (password: 456) etc

and

2) allow a user 3 attempts to input correct password failing which the file should close.

many thanks,

skf
VBA Code:
Option Explicit


Private Sub cmdClear_Click()

    Me.txtUserID.Value = ""
    Me.txtPassword.Value = ""
    
    Me.txtUserID.SetFocus
    
    'Code to exit- Please remove the comments if you want to use it exit
'    Unload Me
'    ThisWorkbook.Close Savechanges:=False
'    Application.Visible = True
    

End Sub

Private Sub cmdLogin_Click()

    Dim user As String
    
    Dim password As String
    
    user = Me.txtUserID.Value
    
    password = Me.txtPassword.Value
    
    If (user = "admin" And password = "admin") Then
    
        Unload Me
        Application.Visible = True
        
               
        Else
        
            MsgBox "Invalid Credentials"
            Unload Me
            ThisWorkbook.Close Savechanges:=False
            Application.Visible = True
           
            
        End If
    
End Sub



Private Sub Image1_Click()

End Sub

Private Sub UserForm_Initialize()

    Me.txtUserID.Value = ""
    Me.txtPassword.Value = ""
    
    Me.txtUserID.SetFocus

End Sub




Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)

    If CloseMode = 0 Then Cancel = True

End Sub
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Hi Dave,

than you for the link. unfortunately im still very very new at VBA so would be able to figure it out :)

The article is a tutorial that broadly matches your requirement & gives step by step guide & for ease, also provides video & download of template.
All of this should, I would have thought, prove a little easier to work through as opposed to attempting (as a novice) to create your own.

If get stuck, you can always post back your workings to forum for further guidance. Plenty here to assist.

Dave
 
Upvote 0
The article is a tutorial that broadly matches your requirement & gives step by step guide & for ease, also provides video & download of template.
All of this should, I would have thought, prove a little easier to work through as opposed to attempting (as a novice) to create your own.

If get stuck, you can always post back your workings to forum for further guidance. Plenty here to assist.

Dave
Thank you Dave, i will give it a try.

skf
 
Upvote 0
so this is the code i am able to create (with lots of help!). i have a form with two users admin, and user (both with passwords admin and user respectively). If i add any more users, the form does not accept them.
1) is there a way of adding more users e.g. abc with a password 123 and def with pw 456?
2) Also i would like the file to store the username in cell a1 of sheet1 once the user logs in.

Thank you for your help.

skf


VBA Code:
Option Explicit


Private Sub cmdClear_Click()

    Me.txtUserID.Value = ""
    Me.txtPassword.Value = ""
    
    Me.txtUserID.SetFocus
    
    'Code to exit- Please remove the comments if you want to use it exit
'    Unload Me
'    ThisWorkbook.Close Savechanges:=False
'    Application.Visible = True
    

End Sub

Private Sub cmdLogin_Click()

    Dim user As String
    
    Dim password As String
    
    user = Me.txtUserID.Value
    
    password = Me.txtPassword.Value
    
    If (user = "admin" And password = "admin") Or (user = "user" And password = "user") Then
    
        Unload Me
        Application.Visible = True
        
    Else
    
        If LoginInstance < 3 Then
        
            MsgBox "Invalid login credentials. Please try again.", vbOKOnly + vbCritical, "Invalid Login Details"
            LoginInstance = LoginInstance + 1
            
        Else
        
            MsgBox "You have exceeded the maximum number of login attempts.", vbOKOnly + vbCritical, "Invalid Credentials"
            Unload Me
            ThisWorkbook.Close Savechanges:=False
            Application.Visible = True
            LoginInstance = 0
            
        End If
    
    End If
    
End Sub



Private Sub Image1_Click()

End Sub

Private Sub UserForm_Initialize()

    Me.txtUserID.Value = ""
    Me.txtPassword.Value = ""
    
    Me.txtUserID.SetFocus

End Sub




Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)

    If CloseMode = 0 Then Cancel = True

End Sub
 
Upvote 0

Forum statistics

Threads
1,215,360
Messages
6,124,493
Members
449,166
Latest member
hokjock

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