Learn to program
Twitter
LinkedIn
YouTube
RSS
Facebook

How To Make A MS Access Login Form

Watch and find out how to make a MS Access login form. Login forms are useful to collect needed information from the user. In this video, you will find out how to make one with MS Access VBA.

Option Compare Database

'=================
'frmMainForm code:
'=================

Private Sub Form_Close()

    DoCmd.Close acForm, "frmLogin"

End Sub



Private Sub Form_Open(Cancel As Integer)
    
    DoCmd.OpenForm "frmLogin", , , , , acDialog

End Sub


Option Compare Database

'=================
'frmLogin code:
'=================

Private Sub btnOK_Click()

    Dim strUser As String

    Dim strSQL As String
    Dim rst As Object
    Dim intCount As Integer
    
    strUser = Nz(Me.txtUser, 0)

    strSQL = "SELECT UserName FROM tblUsers WHERE UserName='" & strUser & "'"
    
    Set rst = CurrentDb.OpenRecordset(strSQL)
    
    intCount = rst.RecordCount
    
    Select Case intCount
        Case 0
            MsgBox "incorrect credentials, try again"
        Case 1
            MsgBox "correct"
            Me.Visible = False
    End Select
    
End Sub

Private Sub lblRegister_Click()

    Dim strNewUser As String
    Dim strSQLInsert As String
    
    strNewUser = InputBox("Enter your name to register.")
    
    strSQLInsert = "INSERT INTO tblUsers (Username) VALUES ('" & strNewUser & "')"
    
    CurrentDb.Execute strSQLInsert
    
    MsgBox "Great! Now enter '" & strNewUser & "' in the user name box above."
    

End Sub

You can download the database file, by clicking here.