Advertisement
Lewisg005

Untitled

Mar 28th, 2024
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VisualBasic 7.19 KB | Source Code | 0 0
  1. Imports System.Data.OleDb
  2. Imports System.Threading
  3. Public Class FrmStudents
  4.  
  5.     Private Sub FrmStudents_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  6.  
  7.         LoadStudentData()
  8.  
  9.     End Sub
  10.  
  11.  
  12.     Private Sub dgvStudents_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvStudents.CellClick
  13.         'when a cell in the table is clicked it populates the text and combo boxes with the corresponding values in the table
  14.        Dim thisRow As Integer = e.RowIndex
  15.  
  16.         txtStudentID.Text = dgvStudents.Item(0, thisRow).Value
  17.         txtFirstName.Text = dgvStudents.Item(1, thisRow).Value
  18.         txtSurname.Text = dgvStudents.Item(2, thisRow).Value
  19.         cmbGender.Text = dgvStudents.Item(3, thisRow).Value
  20.         cmbHouse.Text = dgvStudents.Item(4, thisRow).Value
  21.         cmbForm.Text = dgvStudents.Item(5, thisRow).Value
  22.  
  23.     End Sub
  24.  
  25.     Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
  26.  
  27.         'enables the combo and text boxes to allow the use to edit the data
  28.        txtFirstName.Enabled = True
  29.         txtSurname.Enabled = True
  30.         cmbGender.Enabled = True
  31.         cmbForm.Enabled = True
  32.         cmbHouse.Enabled = True
  33.         'disables the datatable and edit button to reduce the risk of errors and the porgramme breaking
  34.        dgvStudents.Enabled = False
  35.         btnEdit.Enabled = False
  36.         btnAdd.Enabled = False
  37.         btnConfirm.Enabled = True
  38.         btnCancel.Enabled = True
  39.  
  40.  
  41.     End Sub
  42.  
  43.     Private Sub btnConfirm_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConfirm.Click
  44.         'updates the database with the edited data once confirm is clicked
  45.        SQL = "UPDATE TblStudents SET FirstName = '" & txtFirstName.Text & "', Surname = '" & txtSurname.Text & "', Gender = '" & cmbGender.Text & "', HouseName = '" & cmbHouse.Text & "', FormName = '" & cmbForm.Text & "' WHERE StudentID = " & txtStudentID.Text & ";"
  46.         Cmd = New OleDbCommand(SQL, Con)
  47.         Dim SQLResults As DataTable = PerformCRUD(Cmd)
  48.         'reloads datatable to show new data
  49.        LoadStudentData()
  50.         'resets buttons
  51.        CancelConfirmButtonsReset()
  52.  
  53.     End Sub
  54.  
  55.     Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
  56.         'resets buttons
  57.        CancelConfirmButtonsReset()
  58.  
  59.     End Sub
  60.  
  61.     Sub CancelConfirmButtonsReset()
  62.  
  63.         'resets form back to how it is when it is first loaded
  64.        txtFirstName.Enabled = False
  65.         txtSurname.Enabled = False
  66.         cmbGender.Enabled = False
  67.         cmbForm.Enabled = False
  68.         cmbHouse.Enabled = False
  69.  
  70.         dgvStudents.Enabled = True
  71.         btnEdit.Enabled = True
  72.         btnAdd.Enabled = True
  73.         btnConfirm.Enabled = False
  74.         btnCancel.Enabled = False
  75.         btnAddConfirm.Enabled = False
  76.         btnAddCancle.Enabled = False
  77.  
  78.     End Sub
  79.  
  80.     Sub LoadStudentData()
  81.         'pulls up all student data
  82.        SQL = "SELECT * FROM TblStudents;"
  83.         Cmd = New OleDbCommand(SQL, Con)
  84.         Dim SQLResults As DataTable = PerformCRUD(Cmd)
  85.         dgvStudents.DataSource = SQLResults
  86.  
  87.         LoadHouseSummary()
  88.  
  89.     End Sub
  90.  
  91.     Sub LoadHouseSummary()
  92.  
  93.         SQL = "SELECT HouseName, COUNT(StudentID) As NumberOfStudents FROM TblStudents GROUP BY HouseName;"
  94.         Cmd = New OleDbCommand(SQL, Con)
  95.         Dim HouseSummary As DataTable = PerformCRUD(Cmd)
  96.         dgvHouseSummary.DataSource = HouseSummary
  97.  
  98.         'Clears existing chart data
  99.        chartHouseSummary.Series.Clear()
  100.         chartHouseSummary.Series.Add("SeriesHouseSummary")
  101.         chartHouseSummary.Series("SeriesHouseSummary").ChartType = DataVisualization.Charting.SeriesChartType.Pie
  102.         For i = 0 To HouseSummary.Rows.Count - 1
  103.             chartHouseSummary.Series("SeriesHouseSummary").Points.AddXY(HouseSummary.Rows(i).Item(0), HouseSummary.Rows(i).Item(1))
  104.         Next
  105.  
  106.  
  107.     End Sub
  108.    
  109.  
  110.     Private Sub btnAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAdd.Click
  111.         'this gets enables and clears all of the textboxes so that the user can add a new student
  112.        txtFirstName.Enabled = True
  113.         txtSurname.Enabled = True
  114.         cmbGender.Enabled = True
  115.         cmbForm.Enabled = True
  116.         cmbHouse.Enabled = True
  117.  
  118.         dgvStudents.Enabled = False
  119.         btnEdit.Enabled = False
  120.         btnAdd.Enabled = False
  121.         btnAddConfirm.Enabled = True
  122.         btnAddCancle.Enabled = True
  123.  
  124.         txtStudentID.Text = ""
  125.         txtFirstName.Text = ""
  126.         txtSurname.Text = ""
  127.         cmbGender.Text = ""
  128.         cmbHouse.Text = ""
  129.         cmbForm.Text = ""
  130.  
  131.        
  132.  
  133.  
  134.     End Sub
  135.  
  136.     Private Sub btnAddCancle_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAddCancle.Click
  137.         CancelConfirmButtonsReset()
  138.     End Sub
  139.  
  140.     Private Sub btnAddConfirm_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAddConfirm.Click
  141.         'this SQL statement will add the new student to the database with the info that was typed in the text boxes
  142.        SQL = "INSERT INTO TblStudents (FirstName, Surname, Gender, HouseName, FormName) VALUES ('" & txtFirstName.Text & "', '" & txtSurname.Text & "', '" & cmbGender.Text & "', '" & cmbHouse.Text & "', '" & cmbForm.Text & "');"
  143.         Cmd = New OleDbCommand(SQL, Con)
  144.         Dim SQLResults As DataTable = PerformCRUD(Cmd)
  145.  
  146.         LoadStudentData()
  147.  
  148.         CancelConfirmButtonsReset()
  149.     End Sub
  150.  
  151.     Private Sub btnDeleteStudent_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDeleteStudent.Click
  152.         'this will doeble check if the user is sure they want to delete the student and of they are it wil delete the student with the corresponding studentID to the one selected
  153.        If txtStudentID.Text <> "" Then
  154.             Dim UserResponse As MsgBoxResult = MsgBox("Are you sure that you want to delete record with student id: " & txtStudentID.Text, MsgBoxStyle.YesNo, "Delete Record?")
  155.             If UserResponse = vbYes Then
  156.                 SQL = "DELETE * FROM tblStudents WHERE StudentID = " & txtStudentID.Text & ";"
  157.                 Cmd = New OleDbCommand(SQL, Con)
  158.                 Dim SQLResults As DataTable = PerformCRUD(Cmd)
  159.  
  160.                 LoadStudentData()
  161.             End If
  162.         End If
  163.        
  164.  
  165.     End Sub
  166.  
  167.     Private Sub txtSearch_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtSearch.TextChanged
  168.         'this will compare the text typed into the search text box and bring up all of the students in the table that contain what was typed in thair first or last name  
  169.        SQL = "SELECT * FROM TblStudents WHERE (FirstName LIKE '%" & txtSearch.Text & "%') OR Surname LIKE '%" & txtSearch.Text & "%';"
  170.         Cmd = New OleDbCommand(SQL, Con)
  171.         Dim SQLResults As DataTable = PerformCRUD(Cmd)
  172.         dgvStudents.DataSource = SQLResults
  173.  
  174.     End Sub
  175.  
  176.     Private Sub btnback_Click(sender As Object, e As EventArgs) Handles btnback.Click
  177.         frmAdmin.Show()
  178.         Me.Hide()
  179.  
  180.  
  181.  
  182.     End Sub
  183. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement