Updating Records of the Student’s Registration Form
Submitted by janobe on Sunday, December 29, 2013 - 17:44.
This is the continuation of my previous tutorial which is the Navigation of a Student’s Registration Form. This time, I will teach you how to update the records in a MySQL Database. I change some features in the Registration Form and the Data type of the year in a MySQL Database.
To start with:
Change your Int data type into VarChar in the yr field in MySQL Database. So that, you can put characters and numbers on it.
Open the Visual Basic 2008, open the file of Student's Registration Form and add the Button for the update. It will look like this.
After that, double click the Form and do the following code in the
Click the Module named "studReg" and create a Sub procedure for updating the records in MySQL Database. Just like this.
Go back to the Design Views, double click the Update Button and do the following code for updating the records of the student.
After that, go back to the Design Views again, double click the Textbox(txtid) and do the following code for conditioning the save and add buttons which to be enable or disable it.
Complete Source Code is included.

Form1_Load
for the dropdown style of a Combobox. If you run the project, the ComboBox will look like this.
- 'change the dropdown style of the Combobox
- cboStatus.DropDownStyle = ComboBoxStyle.DropDownList
- cbosy.DropDownStyle = ComboBoxStyle.DropDownList
- cboYear.DropDownStyle = ComboBoxStyle.DropDownList

- Public Sub updates(ByVal sql As String)
- Try
- 'open the connection
- con.Open()
- 'set up your commands
- 'it holds the data to be executed
- With cmd
- 'pass on the value of con to the MySQL command which is Connection
- .Connection = con
- 'role of this is to return text presented by a command object
- .CommandText = sql
- 'executes the data to update in the database
- result = cmd.ExecuteNonQuery
- 'if the data executes less than 0 it will not be updated
- 'but if the data executes greater than 0 it will be updated
- If result = 0 Then
- Else
- End If
- End With
- Catch ex As Exception
- End Try
- End Sub
- Private Sub btn_Update_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Update.Click
- Try
- 'declaring variable sql as string
- Dim sql As String
- 'declaring variable radio as string
- Dim radio As String
- 'conditioning the radiobutton to set the male and female
- ' if the radiobutton is checked it is equals to Female and if not it is equals to male
- If rdoFemaleMale.Checked = True Then
- radio = "Female"
- Else
- radio = "Male"
- End If
- 'Put the Update query to a variable sql
- sql = "Update student set `s_fname` = '" & txtFname.Text _
- & "', `lastname` = '" & txtLname.Text _
- & "', `middlename`='" & txtMname.Text _
- & "', `s_address`= '" & rchAddress.Text _
- & "', `s_age`='" & txtAge.Text _
- & "', `s_bday`='" & dtpDbirth.Text _
- & "', `s_bplace`='" & rchPbirth.Text _
- & "', `s_gender`='" & radio _
- & "', `s_status`='" & cboStatus.Text _
- & "', `s_guardian`='" & txtGuardian.Text _
- & "', `s_guardian_relation`='" & txtRelation.Text _
- & "', `s_guardian_add`='" & rchGaddress.Text _
- & "', `s_guardian_contact`='" & txtContact.Text _
- & "', `sy`='" & cbosy.Text _
- & "', `yr`='" & cboYear.Text _
- & "' where `s_id` = '" & txtid.Text & "'"
- updates(sql) 'call your public SubName for updatin data and put the sql in the parameters list
- cleartext(Me) ' call a public SubName for clearing and put the object form in the parameters list
- Call Form1_Load(sender, e) 'call the form_load to set up again the AutoNumber
- Catch ex As Exception
- End Try
- End Sub
- Private Sub txtid_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtid.TextChanged
- Try
- 'If the value of TextBox change.
- If txtid.Text = maxrows.ToString Then 'check if the value of the TextBox is equal to the total number of rows in the table
- 'result:
- btn_Update.Enabled = False 'disable the Update Button
- btnSave.Enabled = True 'enable the Save Button
- Else 'The TextBox is less than to the total number of rows in the table
- 'result:
- btn_Update.Enabled = True 'enable the Update Button
- btnSave.Enabled = False 'disable the Save Button
- End If
- Catch ex As Exception
- End Try
- End Sub
Add new comment
- 847 views