Searching and Deleting Data in the Student’s Registration Form
Submitted by janobe on Wednesday, January 1, 2014 - 22:58.
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.
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.
Go back to the Design Views, double click the Delete Button and do the following code in the
Go back to the Design Views again, double click a TextBox and do the following code for searching the records in the database.
Now you have a complete Student's Registration Form, you can download it and run it on your computer.
- 'sub procedure for searching the records in the database
- Public Sub reload(ByVal sql As String)
- Try
- con.Open()
- With cmd
- .Connection = con
- .CommandText = sql
- End With
- dt = New DataTable
- da = New MySqlDataAdapter(sql, con)
- da.Fill(dt)
- With Form1 'frm represent as a form
- .txtid.Text = dt.Rows(0).Item("s_id")
- .txtFname.Text = dt.Rows(0).Item("s_fname")
- .txtLname.Text = dt.Rows(0).Item("lastname")
- .txtMname.Text = dt.Rows(0).Item("middlename")
- .rchAddress.Text = dt.Rows(0).Item("s_address")
- .dtpDbirth.Text = dt.Rows(0).Item("s_bday")
- .rchPbirth.Text = dt.Rows(0).Item("s_bplace")
- .txtAge.Text = dt.Rows(0).Item("s_age")
- If dt.Rows(0).Item("s_gender") = "Female" Then 'check if the gender is male or female.
- .rdoFemaleMale.Checked = True
- Else
- .rdioMale.Checked = True
- End If
- .cboStatus.Text = dt.Rows(0).Item("s_status")
- .cboYear.Text = dt.Rows(0).Item("yr")
- .cbosy.Text = dt.Rows(0).Item("sy")
- .txtGuardian.Text = dt.Rows(0).Item("s_guardian")
- .txtRelation.Text = dt.Rows(0).Item("s_guardian_relation")
- .rchGaddress.Text = dt.Rows(0).Item("s_guardian_add")
- .txtContact.Text = dt.Rows(0).Item("s_guardian_contact")
- End With
- Catch ex As Exception
- End Try
- da.Dispose()
- End Sub
- 'sub procedure for deleting the records in the database
- Public Sub delete(ByVal sql As String)
- Try
- con.Open()
- With cmd
- .Connection = con
- .CommandText = sql
- result = cmd.ExecuteNonQuery
- If result = 0 Then
- Else
- End If
- End With
- Catch ex As Exception
- End Try
- End Sub
btn_Delete_Click
.
- Private Sub btn_Delete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Delete.Click
- Try
- Dim sql As String 'declare a string variable
- 'store a delete query in the string varible sql
- sql = "DELETE FROM student WHERE s_id = '" & txtid.Text & "'"
- delete(sql) 'A SubName for deleting record in the database
- cleartext(Me) 'call a SubName for clearing the text in the textbox
- Call Form1_Load(sender, e) 'set again the autonumber
- Catch ex As Exception
- End Try
- End Sub
- Private Sub txt_Search_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txt_Search.TextChanged
- Try
- Dim sql As String 'declare a string variable
- 'store a select query to the string variable sql
- sql = "SELECT * FROM student WHERE s_id like '%" & txt_Search.Text & "'"
- reload(sql) 'A SubName for searching the records in the database
- Catch ex As Exception
- End Try
- End Sub
Add new comment
- 334 views