how to create a login form in VB (vs 2008) with database

Answered how to create a login form in VB (vs 2008) with database

  • Wednesday, July 16, 2008 6:35 PM
     
     
    Hi,
    I am new with VB. I got now the Visual Studio 2008. I newer used it before.
    I need the following:
        I need a login form, which is using an MS Access 2003 database, in which I stored usernames and passwords.
    The DB name is users.mdb and is stored on the C drive. It contains just 1 table, the name of the table is UserList. In the table I have 3 colums, first is ID, second is Username, the third is Password. It contains just 4 datas, in the username the first entry is John, the second is Mary, in the password column the passwords are john and mary.

    And now to the code.

    I can add to the main form a "LoginForm" (from the VS2008 templates). The name is LoginForm1.vb
    After this I am creating a connection to the database, as following:
           Solution Explorer --- click on Data Sources --- Add new Data Source. On the windo, which appeares, I choos Database, then click on next, next page --- New Connection --- DataSource is seted to Microsoft Access Database File (OLEDB) --- and Database filename is C:\users.mdb --- no authentication is needed --- Test Connection succeeded ---click on OK --- in the data source config wizzard click on next --- by the question "Save the connection string to the Application configuration file" I choose yes, and it will be saved as "usersConnectionString" --- I click on next --- by choosing my database objects I choose the "Username" and "Password" --- The DataSet name is "usersDataSet" --- and I click on Finish.
    And here I am. I don't know, how to check if the value/text of the Username textbox is in the database, or not.

    Here is the code of the login form, wich I got by adding the template LoginForm1.vb to the project:


    Public Class LoginForm1 
     
        ' TODO: Insert code to perform custom authentication using the provided username and password  
        ' (See http://go.microsoft.com/fwlink/?LinkId=35339).   
        ' The custom principal can then be attached to the current thread's principal as follows:  
        '     My.User.CurrentPrincipal = CustomPrincipal 
        ' where CustomPrincipal is the IPrincipal implementation used to perform authentication.  
        ' Subsequently, My.User will return identity information encapsulated in the CustomPrincipal object 
        ' such as the username, display name, etc. 
     
        Private Sub OK_Click(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles OK.Click 
            Me.Close() 
        End Sub 
     
        Private Sub Cancel_Click(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles Cancel.Click 
            Me.Close() 
        End Sub 
     
        Private Sub LoginForm1_Load(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles MyBase.Load 
     
        End Sub 
    End Class 

    Thank you for your help in advance.
    IceFace

All Replies

  • Saturday, July 19, 2008 1:55 PM
     
     Answered Has Code
    Hi IceFace,

    Walkthrough: How to make a Login Verification routine?

    e.g. Access Database file myDB.mdb contains a table Users.

    Users table has the following two fields:
    Field Name   Data Type
    userid           Text
    password      Text

    Then you can add a template login form like this:
    Project -> Add Windows Form -> Select template  "Login Form"

    Login verification:
    Prerequisites: usernametextbox TextBox, passwordtextbox TextBox, OK and Cancel buttons on Login Form.

    Imports System.Data.OleDb  
     
    Public Class LoginForm1  
     
        ' OK button  
        Private Sub OK_Click(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles OK.Click  
            Dim con As New OleDbConnection("Provider=Microsoft.jet.oledb.4.0;data source=E:\VBproject\myDB.mdb")  
            Dim cmd As OleDbCommand = New OleDbCommand("SELECT * FROM Users WHERE userid = '" & usernametextbox.Text & "' AND password = '" & passwordtextbox.Text & "' ", con)  
            con.Open()  
            Dim sdr As OleDbDataReader = cmd.ExecuteReader()  
            ' If the record can be queried, Pass verification and open another form.  
            If (sdr.Read() = TrueThen 
                MessageBox.Show("The user is valid!")  
                Dim mainForm As New MainForm  
                mainForm.Show()  
                Me.Hide()  
            Else 
                MessageBox.Show("Invalid username or password!")  
            End If 
        End Sub 
     
        ' Cancel button  
        Private Sub Cancel_Click(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles Cancel.Click  
            Me.Close()  
       End Sub 
     
    End Class 


    Trackback:
    http://forums.msdn.microsoft.com/en-US/vbgeneral/thread/ab00db69-8f87-4456-b87e-d515c80e22db/
    http://forums.msdn.microsoft.com/en-US/vblanguage/thread/de43bba7-5228-4d20-8b5c-9389029f4dae/


    Best regards,
    Martin Xie

  • Wednesday, February 24, 2010 4:31 AM
     
     
    Good tutorial thanks
  • Monday, September 20, 2010 9:17 PM
     
     

    Hi Martin,

    Very clear and self explanatory!

    Thx!

  • Thursday, June 09, 2011 8:23 PM
     
     

    thanks 

    i am student and now a days learning vb.net

    your example for login form is great

  • Wednesday, September 21, 2011 2:37 AM
     
     

    Hi Martin,

     

    Do you know of a way to use a datatable inside a dataset instead of using Access?

     

    Thanks,

    David

  • Tuesday, November 29, 2011 7:38 AM
     
     

    your post is a good tutorial but i have a problem when i try the LOGIN form i receieved an error

    this my code

    Imports System.Data.OleDb
    Public Class LoginForm1

        ' TODO: Insert code to perform custom authentication using the provided username and password
        ' (See http://go.microsoft.com/fwlink/?LinkId=35339). 
        ' The custom principal can then be attached to the current thread's principal as follows:
        '     My.User.CurrentPrincipal = CustomPrincipal
        ' where CustomPrincipal is the IPrincipal implementation used to perform authentication.
        ' Subsequently, My.User will return identity information encapsulated in the CustomPrincipal object
        ' such as the username, display name, etc.

        Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click
            Dim con As New OleDbConnection("Provider=Microsoft.jet.oledb.4.0;data source=c:\db\user.mdb")
            Dim cmd As OleDbCommand = New OleDbCommand("SELECT * FROM Users WHERE userid = '" & UsernameTextBox.Text & "' AND password = '" & PasswordTextBox.Text & "' ", con)
            con.Open()
            Dim sdr As OleDbDataReader = cmd.ExecuteReader()
            ' If the record can be queried, Pass verification and open another form. 
            If (sdr.Read() = True) Then
                MessageBox.Show("The user is valid!")
                Dim mainForm As New Form3
                mainForm.Show()
                Me.Hide()
            Else
                MessageBox.Show("Invalid username or password!")
            End If
        End Sub

        Private Sub Cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel.Click
            Me.Close()
        End Sub

        Private Sub LoginForm1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load


        End Sub
    End Class

     

    I have an error in these Line

    Dim sdr As OleDbDataReader = cmd.ExecuteReader()

    and the error statement is below

    Oledbexception was unhandled

    The Microsoft Jet database engine cannot find the input table or query 'Users'.  Make sure it exists and that its name is spelled correctly.

     

    please help me
  • Monday, September 10, 2012 3:37 PM
     
     

    Eish! me too

    i think the problem might be the path but i dont knw where should i locate my database

    Your help will be appriciated

    Ooh-Bee

  • Monday, September 10, 2012 4:13 PM
     
     
    Unless you are the OP I would recommend posting a new question to the forum. It isn't likely that you will receive help in a thread that's several years old.

    Paul ~~~~ Microsoft MVP (Visual Basic)

  • Tuesday, November 27, 2012 8:00 PM
     
     
    Hi when using access as a database you need to know that Password is a protected word putting [] around password will help and just for good measure I put [] around all Example:  "SELECT * FROM [UserDetails] WHERE [Username]= ('" + LoginUsername.Text + "') AND [Password] = ('" + LoginPassword.Text + "')"
  • Saturday, March 09, 2013 9:02 AM
     
     

    it shows "No value given for one or more required parameters."