Login Form

This code allows a user to login to another form if that user has a profile within a database.  Since many newbies are often looking for login code, hopefully this simple Access example will be helpful.  You will need a login button called Login and two textboxes called username and password.

' Place this code under your Public Class.  If your form name is Login, then place it under Public Class Login
    Inherits System.Windows.Forms.Form
    Dim iCount As Integer ' this integer is declared to help count the number of imes a user tried to login.
    Dim frmMain As New MainScreen ' this line declares a variable that will point the user to the Main Screen upon a successful login.  The second form created is called MainScreen.vb
    Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogin.Click
        'The connection string is used to describe the type of database, the security information and the location to the database.
        Dim ConString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Password="""";User ID=Admin;Data Source=""databasename.mdb"";"
        'Create a new connection object and assign the ConString Connection String above
        Dim DBCon As New OleDb.OleDbConnection(ConString)
' g_login is a global variable defined in a Module that captures the login name and passes it from form to form.  To create this, just create a Module, say Module1.vb and in it put "Public g_login as String" {g meaning global and login to represent the global login}
        g_login = Me.txtUsername.Text

        Dim strPassword As String = Me.txtPassword.Text

        If g_login = "" Or strPassword = "" Then
            MessageBox.Show("You are missing information. Please make sure that both the username and password fields are filled out.", "Missing Info")
        End If
' The database has two fields in the Users table.  A UserID field, which is the primary key and declared as a text.  The other field is Password, which is a text as well.
        Dim strsql As String = "SELECT [UserID], [Password] FROM Users WHERE [UserID]='" & g_login & "' "

        Dim cm As New OleDb.OleDbCommand(strsql, DBCon)
        Dim dr As OleDb.OleDbDataReader
        Dim valid As Boolean = False
        Dim HasRows As Boolean = False
            dr = cm.ExecuteReader
            If dr.HasRows Then
                While dr.Read
                    If strPassword = dr.Item("Password") Then
                        valid = True
                    End If
                End While
                HasRows = True
            End If
        Catch exO As OleDb.OleDbException
        Catch ex As Exception
            If DBCon.State = ConnectionState.Open Then
            End If
            cm = Nothing
            dr = Nothing
        End Try
        iCount = iCount + 1
        If valid = True Then
        ElseIf iCount = 3 Then
            MessageBox.Show("Contact Safreak!", "Invalid Info")
        ElseIf HasRows = False Then
            MessageBox.Show("Invalid user name, try again!", "Invalid Info")
            Me.txtUsername.Text = ""
            Me.txtPassword.Text = ""
            MessageBox.Show("Invalid password, try again!", "Invalid Info")
            Me.txtPassword.Text = ""
        End If

    End Sub

You might also like...




Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“If debugging is the process of removing software bugs, then programming must be the process of putting them in.” - Edsger Dijkstra