Hello @edbanshee
Everything needed can be done with TableAdapter components. Full source code is in the following GitHub repository.
Notes
- I didn't match up your table and column names as I already had this code and believe you should be able to adapt to your code.
- I used one form for redirection but of course you can change that to two forms.
.xsd
Here the Users table has UserName and UserPassword columns for the login and UserRole which is either (in this case) user or admin
Login query
Login form
In this case the user gets one try even though the standard is to allow multiple attempts which would be easy to do but kept this simple on purpose. Also note the database is password protected where the password is stored in app.config which we could encrypted.
Public Class LoginForm
Private Sub cmdLogin_Click(sender As Object, e As EventArgs) Handles cmdLogin.Click
Dim getLoginDetails As AppDataSet.UsersDataTable =
UsersTableAdapter.GetDataByLogin(txtUserName.Text, txtPassword.Text)
Dim result As AppDataSet.UsersRow = getLoginDetails.FirstOrDefault()
If result IsNot Nothing Then
Hide()
Dim f As New Form1(result.UserRole)
f.ShowDialog()
Else
MessageBox.Show($"Contact an admin")
Application.ExitThread()
End If
End Sub
End Class
Main Form code
Public Class Form1
Private UserRole As String
Public Sub New(pUserRole As String)
InitializeComponent()
UserRole = pUserRole
End Sub
Private Sub Form1_FormClosed(sender As Object, e As FormClosedEventArgs) Handles Me.FormClosed
Application.ExitThread()
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
CustomersTableAdapter.Fill(Me.AppDataSet.Customers)
Text = $"User Role: {UserRole}"
End Sub
Private Sub cmdClose_Click(sender As Object, e As EventArgs) Handles cmdClose.Click
Close()
End Sub
Private Sub CustomersBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs) _
Handles CustomersBindingNavigatorSaveItem.Click
Validate()
CustomersBindingSource.EndEdit()
TableAdapterManager.UpdateAll(Me.AppDataSet)
End Sub
End Class
New code
I updated the repository code as follows. Note that rather than using a string for role I changed to an Enum. Let me know if you need more assistance.
Added the following enum
Namespace Enums
Public Enum UserRoles
Admin
User
End Enum
End Namespace
Then via project properties, application tab click on application events button and replace the code with the following.
Imports WindowsApplication1.Enums
Namespace My
Partial Friend Class MyApplication
Public Property UserRole() As UserRoles
End Class
End Namespace
Login form
Imports WindowsApplication1.Enums
Public Class LoginForm
Private Sub cmdLogin_Click(sender As Object, e As EventArgs) Handles cmdLogin.Click
Dim getLoginDetails As AppDataSet.UsersDataTable =
UsersTableAdapter.GetDataByLogin(txtUserName.Text, txtPassword.Text)
Dim result As AppDataSet.UsersRow = getLoginDetails.FirstOrDefault()
If result IsNot Nothing Then
Hide()
My.Application.UserRole = DirectCast([Enum].Parse(GetType(UserRoles),
result.UserRole), UserRoles)
If My.Application.UserRole = UserRoles.Admin Then
Dim f As New AdminForm()
f.ShowDialog()
Else
Dim f As New UserForm()
f.ShowDialog()
End If
Else
MessageBox.Show($"Contact an admin")
Application.ExitThread()
End If
End Sub
End Class
Form1 is now AdminForm
Public Class AdminForm
Public Sub New()
InitializeComponent()
End Sub
Private Sub Form1_FormClosed(sender As Object, e As FormClosedEventArgs) Handles Me.FormClosed
Application.ExitThread()
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
CustomersTableAdapter.Fill(Me.AppDataSet.Customers)
Text = $"User Role: {My.Application.UserRole}"
End Sub
Private Sub cmdClose_Click(sender As Object, e As EventArgs) Handles cmdClose.Click
Close()
End Sub
Private Sub CustomersBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs) _
Handles CustomersBindingNavigatorSaveItem.Click
Validate()
CustomersBindingSource.EndEdit()
TableAdapterManager.UpdateAll(Me.AppDataSet)
End Sub
End Class
Added a form named UserForm
Public Class UserForm
Private Sub UserForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Text = $"User Role: {My.Application.UserRole}"
End Sub
Private Sub UserForm_FormClosed(sender As Object, e As FormClosedEventArgs) Handles Me.FormClosed
Application.ExitThread()
End Sub
Private Sub cmdClose_Click(sender As Object, e As EventArgs) Handles cmdClose.Click
Close()
End Sub
End Class