Hey everyone. I got the basic LoginForm in VB.net to work for me but I wanted to slightly modify it. I added a dropdownbox for the user to select whether they were a "User" or "Administrator".
I have an MS Access database called AITP.mdb with only one table in it called user . The user table has the following fields with attributes in parenthesis: user_id(autonumber), user_fname(text), user_lname(text), user_login_name(text), user_login_password(text), admin(Yes/No), user(Yes/No).
I want the prog to check what the user entered as an accesslevel, on the loginform, against what they have in the database. For example if a user has both admin and user then the form that opens depends on whether they selected administrator or user on the loginform.
Can someone please help me!!!!!!! My entire code for the form is listed below:
Public Class Login
' 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 Form1 ' 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 ' 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 loginbtn.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="";Data Source=""\AITP.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.Usernametxbx.Text Dim main_form As New Form1 Dim admin_form As New admin Dim strPassword As String = Me.Passwordtxbx.Text If g_login = "" Or strPassword = "" ThenMessageBox.Show(
"You are missing information. Please make sure that both the username and password fields are filled out.", "Missing Info") Me.Usernametxbx.Focus() Return End If 'If g_login = "admin" Or g_login = "Admin" Then 'If Passwordtxbx.Text = "aitp" Then 'Dim admin_form As New admin 'Me.Hide() 'admin_form.Show() 'Me.Close() 'Else 'MessageBox.Show("Incorrect login information. Please try again.", "Login Error.", MessageBoxButtons.OK, MessageBoxIcon.Error) 'Usernametxbx.Text = "" 'Passwordtxbx.Text = "" 'Usernametxbx.Focus() 'End If '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 [user_login_name], [user_login_password], [admin], [user] FROM [user] WHERE [user_login_name]='" & 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 Dim admin As Boolean = False Dim user As Boolean = False Dim accesslevel As String Dim admin_valid As Boolean = Falseaccesslevel = accesslvlcmbx.SelectedItem
TryDBCon.Open()
dr = cm.ExecuteReader
If dr.HasRows Then While dr.Read If strPassword = dr.Item("user_login_password") Thenvalid =
True If accesslevel = dr.Item("admin") Thenadmin_valid =
True Elseadmin_valid =
False End If
user = dr.Item(
"user") End If End WhileHasRows =
True End Ifdr.Close()
Catch exO As OleDb.OleDbExceptionMessageBox.Show(exO.Message)
Catch ex As ExceptionMessageBox.Show(ex.Message)
Finally If DBCon.State = ConnectionState.Open ThenDBCon.Close()
End Ifcm =
Nothingdr =
NothingDBCon.Dispose()
GC.Collect()
End TryiCount = iCount + 1
If valid = True Then If accesslevel = "" ThenMessageBox.Show(
"No Access Level Select. Please try again.", "Access Level", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)accesslvlcmbx.Focus()
ElseIf accesslevel = "Administrator" & admin = True Then Me.Hide()admin_form.Show()
Me.Close() ElseIf accesslevel = "User" & user = True Then Me.Hide()main_form.Show()
Me.Close() ElseMessageBox.Show(
"Invalid Access Level. Please try again.", "Invalid Access", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)accesslvlcmbx.Focus()
End If ElseIf iCount = 3 ThenMessageBox.Show(
"Contact AITP!", "Invalid Info") Me.Close() ElseIf HasRows = False ThenMessageBox.Show(
"Invalid user name, try again!", "Invalid Info") Me.Usernametxbx.Focus() Me.Usernametxbx.Text = "" Me.Passwordtxbx.Text = "" ElseMessageBox.Show(
"Invalid password, try again!", "Invalid Info") Me.Passwordtxbx.Focus() Me.Passwordtxbx.Text = "" 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 Login_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load End SubEnd
Class
VB.NET HELP!!!! Login Page HELP PLEASE!!!!!
shauntu