I have created a system and im new to vb.net. It is working fine. The problem is, I need to restrict the main page from 1.) Users and 2.) Admin.
For example, After logging in, Admin will proceed to the main page
And if users logs in, they will be directed to a different page
I'm still new to vb.net (like 6 weeks ago) and i am using microsoft access as my database. A lot of code I find online is very complex and technical. I just need simple vb.net code
Any help will be appreciated, thank you!!
i have posted my code for my login form, so you guys can further understand my coding:
Public Class Form1
Dim loginerror As String
Public Function login()
Dim DBconn As New ADODB.Connection
Dim user As New ADODB.Recordset
Dim Username As String
Dim userDB As String
Dim passDB As String
Dim UserFound As Boolean
DBconn.Open("Provider = Microsoft.Jet.OLEDB.4.0;" & _
"Data Source = '" & Application.StartupPath & "\LoginDB.mdb'")
user.Open("UserTable", DBconn, ADODB.CursorTypeEnum.adOpenStatic, ADODB.LockTypeEnum.adLockOptimistic)
UserFound = False
login = False
Username = "Username = '" & txtuser.Text & "'" '
Do
user.Find(Username)
If user.BOF = False And user.EOF = False Then
userDB = user.Fields("Username").Value.ToString
passDB = user.Fields("Password").Value.ToString
If userDB <> txtuser.Text Then
user.MoveNext()
Else
UserFound = True
If passDB = txtpass.Text Then
user.Close()
DBconn.Close()
Return True
Else
loginerror = "Invalid Password"
user.Close()
DBconn.Close()
Return False
End If
End If
Else
loginerror = "Invalid Username"
user.Close()
DBconn.Close()
Return False
End If
Loop Until UserFound = True
user.Close()
DBconn.Close()
Return False
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If login() = True Then
Welcome.Show()
Me.Close()
Else
MessageBox.Show(loginerror, "Login Message")
End If
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
AcceptButton = Button1
Me.Show()
Application.DoEvents()
txtuser.Focus()
End Sub
Private Sub txtpass_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtpass.TextChanged
End Sub
Private Sub Label2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label2.Click
End Sub
Private Sub txtuser_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtuser.TextChanged
End Sub
Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click
End Sub
Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click
End Sub
Private Sub Label3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label3.Click
End Sub
End Class
Just in case here is my coding for my main form:
Public Class Mainform
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
Select MessageBox.Show("Are you sure you want to exit?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
Case Windows.Forms.DialogResult.Yes
Case Windows.Forms.DialogResult.No
e.Cancel = True
End Select
End Sub
Private Sub Mainform_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.WorkTableAdapter.Fill(Me.LoginDBDataSet.Work)
Me.AssetTableAdapter.Fill(Me.LoginDBDataSet.Asset)
Me.HistoryTableAdapter.Fill(Me.LoginDBDataSet.History)
Me.InventoryTableAdapter.Fill(Me.LoginDBDataSet.Inventory)
Me.Equipment1TableAdapter.Fill(Me.LoginDBDataSet.Equipment1)
End Sub
Private Sub mainform_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
If e.KeyCode = Keys.Escape Then Me.Close()
End Sub
Private Sub Equipment1BindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Equipment1BindingNavigatorSaveItem.Click
Me.Validate()
Me.Equipment1BindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.LoginDBDataSet)
End Sub
Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click
Mainform_Load(Me, New System.EventArgs)
End Sub
Private Sub ToolStripButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton2.Click
Form1.Show()
Me.Dispose()
Me.Close()
End Sub
Private Sub TabPage1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TabPage1.Click
End Sub
Private Sub IDLabel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
End Sub
Private Sub TabPage5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TabPage5.Click
End Sub
Private Sub Equipment_NameTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
End Sub
Private Sub HistoryDataGridView_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs)
End Sub
Private Sub InventoryDataGridView_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs)
End Sub
Private Sub TabControl1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TabControl1.SelectedIndexChanged
End Sub
Private Sub Machine_IDTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
End Sub
Private Sub _WO_CompletedLabel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
End Sub
Private Sub _WO_CompletedCheckBox_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
End Sub
Private Sub _WO_CompletedCheckBox_CheckedChanged_1(ByVal sender As System.Object, ByVal e As System.EventArgs)
End Sub
Private Sub Machine_IDTextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Machine_IDTextBox1.TextChanged
End Sub
Private Sub _WO_CompletedCheckBox_CheckedChanged_2(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles _WO_CompletedCheckBox.CheckedChanged
End Sub
End Class
you need to determine if the user is administrator or not (usually your user table should have a role attached to the user e.g. administration, employee, etc.)
something like this when you try to log in
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If login() = True and UserRole='Administrator' Then
Welcome.Show()
Me.Close()
elseif login() = True then
WelcomeUser.Show()
Me.Close()
Else
MessageBox.Show(loginerror, "Login Message")
End If
End Sub
Related
When I click the Button1 the MsgBox shows me "1" which is good.
Private Sub Button1_Click(sender As Object, e As RoutedEventArgs) Handles Button1.Click
MsgBox(CType(sender, Button).Name.Replace("Button", ""))
End Sub
I want the MsgBox shows me "30" when I click the Button2.
Private Sub Button2_Click(sender As Object, e As RoutedEventArgs) Handles Button2.Click
Hello30()
End Sub
Sub Hello30()
'The following line is need to be repaired.
MsgBox(CType(sender, ????).Name.Replace("Hello", ""))
End Sub
Not really sure what you're trying to do, but this is an example that'll get you close:
Imports System.Reflection
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Hello30()
End Sub
Sub Hello30()
Dim method As String = MethodBase.GetCurrentMethod.Name
MsgBox(method.Replace("Hello", ""))
End Sub
Visual Basic 2010: How can I put the two values in the same row in my database?
Button 1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim rst As ADODB.Recordset
rst = opentable("Select * from timesheet", ADODB.CursorLocationEnum.adUseServer)
With rst
.AddNew()
.Fields("time1").Value = Date.Now
.Update()
MsgBox("Time in")
End With
End Sub
Button 2
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim rst As ADODB.Recordset
rst = opentable("Select * from timesheet", ADODB.CursorLocationEnum.adUseServer)
With rst
.AddNew()
.Fields("time2").Value = Date.Now
.Update()
MsgBox("Time Out")
End With
End Sub
I am using VB.NET and microsoft access as my database, i am new to VB.NET and i tried to make different access levels depending on the current role.For example, After logging in, Admin will proceed to the main page And if users logs in, they will be directed to a different page. I have already set the different roles in my database, and i found some coding online to help me
However i keep getting the error 'UserRole' is not declared. It may be inaccessible due to its protection level. Do i need to declare UserRole first? if so, how do i do it?
Any help would be much appreciated , thank you :)
Public Class Form1
Dim loginerror As String
Dim UserRole As String
Public Function login()
Dim DBconn As New ADODB.Connection
Dim user As New ADODB.Recordset
Dim Username As String
Dim userDB As String
Dim passDB As String
Dim UserFound As Boolean
DBconn.Open("Provider = Microsoft.Jet.OLEDB.4.0;" & _
"Data Source = '" & Application.StartupPath & "\LoginDB.mdb'")
user.Open("UserTable", DBconn, ADODB.CursorTypeEnum.adOpenStatic, ADODB.LockTypeEnum.adLockOptimistic)
UserFound = False
login = False
Username = "Username = '" & txtuser.Text & "'" '
Do
user.Find(Username)
If user.BOF = False And user.EOF = False Then
userDB = user.Fields("Username").Value.ToString
passDB = user.Fields("Password").Value.ToString
If userDB <> txtuser.Text Then
user.MoveNext()
Else
UserFound = True
If passDB = txtpass.Text Then
UserRole = user.Fields("roles").Value.ToString
user.Close()
DBconn.Close()
Return True
Else
loginerror = "Invalid Password"
user.Close()
DBconn.Close()
Return False
End If
End If
Else
loginerror = "Invalid Username"
user.Close()
DBconn.Close()
Return False
End If
Loop Until UserFound = True
user.Close()
DBconn.Close()
Return False
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If login() = True And UserRole = "admin" Then
adminwelcome.Show()
Me.Close()
ElseIf login() = True Then
Welcome.Show()
Me.Close()
Else
MessageBox.Show(loginerror, "Login Message")
End If
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
AcceptButton = Button1
Me.Show()
Application.DoEvents()
txtuser.Focus()
End Sub
Private Sub txtpass_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtpass.TextChanged
End Sub
Private Sub Label2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label2.Click
End Sub
Private Sub txtuser_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtuser.TextChanged
End Sub
Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click
End Sub
Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click
End Sub
Private Sub Label3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label3.Click
End Sub
End Class
I'm assuming that you're pulling UserRole from the database. You're getting this error because that variable has not been declared yet.
I would pull that information (The user's role) and store it into a class-level variable named UserRole. Then, you'll be able to access it in the Button1_Click sub.
Public Class Form1
Dim loginerror As String
Dim UserRole As String
Public Function login()
Dim DBconn As New ADODB.Connection
Dim user As New ADODB.Recordset
Dim Username As String
Dim userDB As String
Dim passDB As String
Dim UserFound As Boolean
DBconn.Open("Provider = Microsoft.Jet.OLEDB.4.0;" & _
"Data Source = '" & Application.StartupPath & "\LoginDB.mdb'")
user.Open("UserTable", DBconn, ADODB.CursorTypeEnum.adOpenStatic, ADODB.LockTypeEnum.adLockOptimistic)
UserFound = False
login = False
Username = "Username = '" & txtuser.Text & "'" '
Do
user.Find(Username)
If user.BOF = False And user.EOF = False Then
userDB = user.Fields("Username").Value.ToString
passDB = user.Fields("Password").Value.ToString
If userDB <> txtuser.Text Then
user.MoveNext()
Else
UserFound = True
If passDB = txtpass.Text Then
UserRole = user.Fields("UserRole").Value.ToString
user.Close()
DBconn.Close()
Return True
Else
loginerror = "Invalid Password"
user.Close()
DBconn.Close()
Return False
End If
End If
Else
loginerror = "Invalid Username"
user.Close()
DBconn.Close()
Return False
End If
Loop Until UserFound = True
user.Close()
DBconn.Close()
Return False
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If login() = True And UserRole = "admin" Then
adminwelcome.Show()
Me.Close()
ElseIf login() = True Then
Welcome.Show()
Me.Close()
Else
MessageBox.Show(loginerror, "Login Message")
End If
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
AcceptButton = Button1
Me.Show()
Application.DoEvents()
txtuser.Focus()
End Sub
Private Sub txtpass_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtpass.TextChanged
End Sub
Private Sub Label2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label2.Click
End Sub
Private Sub txtuser_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtuser.TextChanged
End Sub
Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click
End Sub
Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click
End Sub
Private Sub Label3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label3.Click
End Sub
End Class
I have a problem loading the saved setting of a CheckBox (Checked True or False). On calling up the saved setting it always comes back as True from IsolatedStorage weather the CheckBox has been checked or not? Please see the code attached and I would appreciate it if someone could show me the error of my ways.
Kind regards
Will
Private Sub Button2_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles Button2.Click
'Rem Save Settings'
If CheckBox1.IsChecked = True Then
IsolatedStorageSettings.ApplicationSettings("MyCheckBox") = CheckBox1.IsChecked = True
ElseIf CheckBox1.IsChecked = False Then
IsolatedStorageSettings.ApplicationSettings("MyCheckBox") = CheckBox1.IsChecked = False
End If
End Sub
Private Sub Button3_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles Button3.Click
' Rem Call Up Saved Settings'
MessageBox.Show("Choose Tank Procedure First")
CheckBox1.IsChecked = (IsolatedStorageSettings.ApplicationSettings("MyCheckBox"))
End Sub
This Answer was kindly given to me by Karmjit Singh from the Microsoft Silverlight Forunm
Private Sub Button1_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles Button1.Click
If IsolatedStorageSettings.ApplicationSettings.Contains("MyCheckSettings") Then
IsolatedStorageSettings.ApplicationSettings("MyCheckSettings") = CheckBox1.IsChecked
Else
IsolatedStorageSettings.ApplicationSettings.Add("MyCheckSettings", CheckBox1.IsChecked)
End If
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles Button2.Click
Dim x As Boolean?
IsolatedStorageSettings.ApplicationSettings.TryGetValue(Of Boolean?)("MyCheckSettings", x)
CheckBox2.IsChecked = x
End Sub
The following is the code which I am using for creating a two dimensional array
Public Class frmGrades
Private Score As Dictionary(Of String, String) = New Dictionary(Of String, String)
Private Sub cmdApply_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdApply.Click
Static intNumber As Integer
ReDim Preserve Score(1, intNumber)
Score(0, intNumber) = txtStudent.Text
Score(1, intNumber) = txtGrade.Text
hsbStudent.Maximum = intNumber
hsbStudent.Value = intNumber
intNumber = intNumber + 1
txtGrade.Clear()
txtStudent.Clear()
txtStudent.Focus()
End Sub
Private Sub hsbStudent_Scroll(ByVal sender As Object, ByVal e As System.Windows.Forms.ScrollEventArgs) Handles hsbStudent.Scroll
txtStudent.Text = Score(0, hsbStudent.Value)
txtGrade.Text = Score(1, hsbStudent.Value)
End Sub
Private Sub cmdFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdFirst.Click
txtStudent.Text = Score(0, hsbStudent.Minimum)
txtGrade.Text = Score(1, hsbStudent.Minimum)
End Sub
Private Sub cmdLast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdLast.Click
txtStudent.Text = Score(0, hsbStudent.Maximum)
txtGrade.Text = Score(1, hsbStudent.Maximum)
End Sub
**Private Sub CmdEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdEdit.Click
If Score.ContainsKey(txtStudent.Text) Then
Score.Item(txtStudent.Text) = txtGrade.Text
Else
Score.Add(txtStudent.Text, txtGrade.Text)
End If
End Sub
End Class**
Now I would like to edit the grade text box which should also change the grade in the array. Any idea on how could this be done.
UPDATE:
OK, I see the code has a little more now and the problem is more clear as well as the need for a different approach. I rewrote your code. See if this works for you:
Public Class frmGrades
Public Class StudentGrade
Public Name As String
Public Grade As String
End Class
Private Score As List(Of StudentGrade) = New List(Of StudentGrade)
Private Sub cmdApply_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdApply.Click
AddStudent()
End Sub
Private Sub hsbStudent_Scroll(ByVal sender As Object, ByVal e As System.Windows.Forms.ScrollEventArgs) Handles hsbStudent.Scroll
Update(hsbStudent.Value - 1) 'lists are zero-based
End Sub
Private Sub cmdFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdFirst.Click
Update(0)
End Sub
Private Sub cmdLast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdLast.Click
Update(hsbStudent.Maximum - 1)
End Sub
Private Sub AddStudent()
Dim nm As New StudentGrade
nm.Name = txtStudent.Text
nm.Grade = txtGrade.Text
Score.Add(nm)
hsbStudent.Minimum = 1
hsbStudent.Maximum = Score.Count
hsbStudent.Value = Score.Count
txtGrade.Clear()
txtStudent.Clear()
txtStudent.Focus()
End Sub
Private Sub Update(i As Integer)
txtStudent.Text = Score(i).Name
txtGrade.Text = Score(i).Grade
End Sub
Private Sub CmdEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdEdit.Click
Dim index As Integer = -1, cnt As Integer = 0
For Each nm As StudentGrade In Score
If nm.Name.ToLower = txtStudent.Text.ToLower Then
index = cnt
Exit For
End If
cnt += 1
Next
If index = -1 Then
AddStudent()
Else
Score(index).Name = txtStudent.Text
Score(index).Grade = txtGrade.Text
hsbStudent.Value = index + 1
End If
End Sub
End Class
With this code you can expand the class StudentGrade to contain whatever you need to store. The list, contrary to dictionary, allow you to use index values as well.