Results 1 to 2 of 2

Thread: login on vb;username password at ms access

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2010
    Posts
    12

    login on vb;username password at ms access

    Hi,
    Please help me create a vb 2008 code that requires a user to log in. His username and password are stored in an ms access table. Everytime he logs in, that table is searched for the corresponding username and password; if successful, form1 is opened; if not, the program displays a username and password misatch error message and allows the user up to 3 attempts before it closes the program.

    Also, please include a code that allows the user to change his password.

    Thanks and God bless!

  2. #2
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: login on vb;username password at ms access

    This example provides you have an access database with a table named USERS having fields USERID and PASSWORD in it. Also you must have a connection string to your database.

    In your main form you will need to call frmLogin:

    vb Code:
    1. Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal As System.EventArgs) Handles btnLogin.Click
    2.      Dim frm As New frmLogin
    3.      Dim ret As DialogResult = frm.ShowDialog()
    4.      If ret <> Windows.Forms.DialogResult.OK Then
    5.          MsgBox("Login failed", MsgBoxStyle.Exclamation)
    6.      Else
    7.          ' Logged in
    8.      End If
    9. End Sub

    Now you should create a frmLogin having txtLogin and txtPassword (textboxes) and btnLogin (a button):

    vb Code:
    1. Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal As System.EventArgs) Handles btnLogin.Click
    2.         ' Create a connection to your database:
    3.         Dim conn As New OleDbConnection("your connection string goes here")
    4.         ' Create a query string (with parameters):
    5.         Dim sql As String = "SELECT USERID FROM USERS WHERE USERID=@p_userid AND PASSWORD=@p_passw;"
    6.         Dim cmd As New OleDbCommand(sql, conn)
    7.  
    8.         ' Add parameters
    9.         With cmd
    10.             .Parameters.AddWithValue("p_userid", txtLogin.Text)
    11.             .Parameters.AddWithValue("p_passw", txtPassword.Text)
    12.         End With
    13.  
    14.         ' Open your connection and perform the query
    15.         Try
    16.             conn.Open()
    17.             Dim usr As String = cmd.ExecuteScalar.ToString
    18.             If Not (IsDBNull(usr)) AndAlso usr = txtLogin.Text Then
    19.                 Me.DialogResult = Windows.Forms.DialogResult.OK   ' Return OK (user is found password matches)
    20.             Else
    21.                 Me.DialogResult = Windows.Forms.DialogResult.Cancel ' Return Cancel (query returned nothing)
    22.             End If
    23.         Catch ex As Exception
    24.             ' Catch database connection or query errors:
    25.             MsgBox("Could not connect to database or database error." & Environment.NewLine &
    26.                     "Error message:" & ex.Message)
    27.             Me.DialogResult = Windows.Forms.DialogResult.Cancel
    28.         Finally
    29.             ' Close connection
    30.             conn.Close()
    31.             Me.Close()
    32.         End Try

    This is a very simple mechanism which provides almost no security at all since usernames and passwords are stored in unencrypted forms and the database itself can be compromised.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width