Searching and Deleting Data in the Student’s Registration Form

Last time I created an update to the Student's Registration Form. This time, I will create a search and delete to the Student’s Registration Form. Searching a specific record is very important because you don’t have to navigate the records one at a time, just simply type the id of the records and it will automatically appear. While deleting records help you delete the records that are no longer needed. Let's begin: Open Visual Basic 2008, open file of Student’s Registration Form and Add the Button for deleting, TextBox for searching and the Label for the name of the TextBox. First Form After that, click the Module named “studReg”. In the Module create a sub procedure with parameters sql for deleting and searching the data in the database.
  1. 'sub procedure for searching the records in the database
  2.     Public Sub reload(ByVal sql As String)
  3.         Try
  4.  
  5.             con.Open()
  6.  
  7.             With cmd
  8.                 .Connection = con
  9.                 .CommandText = sql
  10.             End With
  11.  
  12.             dt = New DataTable
  13.             da = New MySqlDataAdapter(sql, con)
  14.             da.Fill(dt)
  15.  
  16.             With Form1 'frm represent as a form
  17.  
  18.                 .txtid.Text = dt.Rows(0).Item("s_id")
  19.                 .txtFname.Text = dt.Rows(0).Item("s_fname")
  20.                 .txtLname.Text = dt.Rows(0).Item("lastname")
  21.                 .txtMname.Text = dt.Rows(0).Item("middlename")
  22.                 .rchAddress.Text = dt.Rows(0).Item("s_address")
  23.                 .dtpDbirth.Text = dt.Rows(0).Item("s_bday")
  24.                 .rchPbirth.Text = dt.Rows(0).Item("s_bplace")
  25.                 .txtAge.Text = dt.Rows(0).Item("s_age")
  26.  
  27.                 If dt.Rows(0).Item("s_gender") = "Female" Then 'check if the gender is male or female.
  28.                     .rdoFemaleMale.Checked = True
  29.                 Else
  30.                     .rdioMale.Checked = True
  31.                 End If
  32.                 .cboStatus.Text = dt.Rows(0).Item("s_status")
  33.                 .cboYear.Text = dt.Rows(0).Item("yr")
  34.                 .cbosy.Text = dt.Rows(0).Item("sy")
  35.                 .txtGuardian.Text = dt.Rows(0).Item("s_guardian")
  36.                 .txtRelation.Text = dt.Rows(0).Item("s_guardian_relation")
  37.                 .rchGaddress.Text = dt.Rows(0).Item("s_guardian_add")
  38.                 .txtContact.Text = dt.Rows(0).Item("s_guardian_contact")
  39.             End With
  40.  
  41.         Catch ex As Exception
  42.             MsgBox("No records found!") 'message appear if no rows found.
  43.         End Try
  44.         da.Dispose()
  45.         con.Close()
  46.     End Sub
  47. 'sub procedure for deleting the records in the database
  48.     Public Sub delete(ByVal sql As String)
  49.         Try
  50.             con.Open()
  51.             With cmd
  52.                 .Connection = con
  53.                 .CommandText = sql
  54.  
  55.                 result = cmd.ExecuteNonQuery
  56.  
  57.                 If result = 0 Then
  58.                     MsgBox("Error for deleting student.")
  59.                 Else
  60.                     MsgBox("Student has been deleted.")
  61.                 End If
  62.             End With
  63.         Catch ex As Exception
  64.             MsgBox(ex.Message)
  65.         End Try
  66.         con.Close()
  67.     End Sub
Go back to the Design Views, double click the Delete Button and do the following code in the btn_Delete_Click.
  1. Private Sub btn_Delete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Delete.Click
  2.         Try
  3.             Dim sql As String 'declare a string variable
  4.  
  5.             'store a delete query in the string varible sql
  6.             sql = "DELETE FROM student WHERE s_id = '" & txtid.Text & "'"
  7.  
  8.             delete(sql) 'A SubName for deleting record in the database
  9.  
  10.             cleartext(Me) 'call a SubName for clearing the text in the textbox
  11.  
  12.             Call Form1_Load(sender, e) 'set again the autonumber
  13.  
  14.         Catch ex As Exception
  15.             MsgBox(ex.Message)
  16.         End Try
  17.     End Sub
Go back to the Design Views again, double click a TextBox and do the following code for searching the records in the database.
  1.  Private Sub txt_Search_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txt_Search.TextChanged
  2.         Try
  3.             Dim sql As String 'declare a string variable
  4.  
  5.             'store a select query to the string variable sql
  6.             sql = "SELECT * FROM student WHERE s_id like '%" & txt_Search.Text & "'"
  7.  
  8.             reload(sql) 'A SubName for searching the records in the database
  9.         Catch ex As Exception
  10.             MsgBox(ex.Message)
  11.         End Try
  12.     End Sub
Now you have a complete Student's Registration Form, you can download it and run it on your computer.

Add new comment