My Profile
Active Members
TodayLast 7 Days
more...
Awards & Gifts
Online Exams
Fresher Jobs
Our fresher job section is exclusively for fresh graduates! Find jobs for freshers in major Indian
cities including Bangalore, Chennai, Hyderabad, Pune or Kochi
Resources
Find educational articles, blogs, discussion threads and other resources.
Colleges
Find details about any college in India or search for courses.
|
Login code for Visual Basic 2005 Beginners
Posted Date: 23 Mar 2008 Resource Type: Articles/Knowledge Sharing Category: Computer & Technology
|
Posted By: Olufemi Member Level: Diamond Rating: Points: 5
|
|
|
|
This code below will avail the VB2005 the opportunity to user a login form as security for a simple program. This example is design with MS Access as the database machine at the back end. With this codes the developer can control access to the system using the access table as the store for authorized users of the newly designed software. With this simple blocks of code you can modify it to suit your purpose.
' 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") Me.txtUsername.Focus() Return 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 Try DBCon.Open() 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 dr.Close() Catch exO As OleDb.OleDbException MessageBox.Show(exO.Message) Catch ex As Exception MessageBox.Show(ex.Message) Finally If DBCon.State = ConnectionState.Open Then DBCon.Close() End If cm = Nothing dr = Nothing DBCon.Dispose() GC.Collect() End Try iCount = iCount + 1 If valid = True Then Me.Hide() frmMain.Show() ElseIf iCount = 3 Then MessageBox.Show("Contact Safreak!", "Invalid Info") Me.Close() ElseIf HasRows = False Then MessageBox.Show("Invalid user name, try again!", "Invalid Info") Me.txtUsername.Focus() Me.txtUsername.Text = "" Me.txtPassword.Text = "" Else MessageBox.Show("Invalid password, try again!", "Invalid Info") Me.txtPassword.Focus() Me.txtPassword.Text = "" End If
End Sub I have used it and it is perfectly running. In case you have any question don't hesitate to ask me.
Olufemi Nigeria
|
Responses
|
No responses found. Be the first to respond and make money from revenue sharing program.
|
|
Watch TV Channels
Watch Asianet TV onlineKairali TV in InternetSurya TV onlineAmritha TV Channel
|