108 – Insert Statement Using Visual Basic Application for Managing Payroll System Module


Insert Statement using Visual Basic Application for Managing Payroll System Module

In our previous lesson we define the module for Managing Payroll System for SQL statement Insert, Update and Delete. We will now proceed with our application in Visual Basic. Our task here is to be able to add new employee, update its information and delete data which is not needed to be stored.

ow to our frmNewEmployee form and double click the save button. Add the following code below.

  1. Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
  2. If btnSave.Text = "Save" Then
  3. dosave()
  4. Else
  5. doUpdate()
  6. End If
  7. End Sub

As you have observed in our code we control the button by determining what text it contains. This is to define our procedure whether to execute Insert or Update.

Let us now define our dosave() sub procedure by adding a Public Sub in our class.

  1. Public Sub doSave()
  3. DATEHIRED = Format(dtpDateHired.Value, "yyyy-MM-dd")
  5. If txtEmplD.Text = "" Then
  6. ErrorProvider1.SetError(txtEmplD, "Please provide Employee ID Number.")
  7. Else
  8. ErrorProvider1.SetError(txtEmplD, String.Empty)
  9. If txtLastName.Text = "" Then
  10. ErrorProvider1.SetError(txtLastName, "Please provide Last Name.")
  11. Else
  12. ErrorProvider1.SetError(txtLastName, String.Empty)
  13. If txtFirstName.Text = "" Then
  14. ErrorProvider1.SetError(txtFirstName, "Please First Name.")
  15. Else
  16. ErrorProvider1.SetError(txtFirstName, String.Empty)
  17. If txtMiddleName.Text = "" Then
  18. ErrorProvider1.SetError(txtMiddleName, "Please provide Middle Name.")
  19. Else
  20. ErrorProvider1.SetError(txtMiddleName, String.Empty)
  22. If rbFemale.Checked Then
  23. empsex = "Female"
  24. ElseIf rbMale.Checked Then
  25. empsex = "Male"
  26. End If
  28. findthis("select * from <code>tblemployee</code> where <code>EMPLOYEEID</code> =" &amp; txtEmplD.Text &amp; " and <code>SCHOOLID</code> =" &amp; SCHOOLIDNO &amp; "")
  29. If DefaultResult() = True Then
  30. MsgBox("Employee ID number already exist.")
  31. Else
  32. IsSuccessResult = saveInsert("INSERT INTO <code>tblemployee</code> (<code>EMPLOYEEID</code>, <code>LASTNAME</code>, <code>FIRSTNAME</code>, <code>MIDDLENAME</code>, <code>EMPADDRESS</code>, <code>EMPCONTACTNO</code>, <code>EMPSTATUS</code>, <code>EMPSEX</code>, <code>SCHOOLID</code>, <code>DATEHIRED</code>, <code>DEPARTMENTID</code>, <code>NOOFDEPENDENTS</code>)" &amp; _
  33. " VALUES (" &amp; txtEmplD.Text &amp; ", '" &amp; txtLastName.Text &amp; "', '" &amp; txtFirstName.Text &amp; "', '" &amp; txtMiddleName.Text &amp; "','" &amp; txtAddress.Text &amp; "','" &amp; txtContactNo.Text &amp; "','" &amp; cbCivilStatus.Text &amp; "','" &amp; empsex &amp; "'," &amp; SCHOOLIDNO &amp; ",'" &amp; DATEHIRED &amp; "'," &amp; cbdepartment.SelectedValue &amp; "," &amp; txtNoDependents.Text &amp; ")")
  34. If IsSuccessResult = True Then
  36. Me.Close()
  38. Else
  40. End If
  41. End If
  42. End If
  43. End If
  44. End If
  45. End If
  46. End Sub

As you have observe in our code we catch some error for blank data entry which is not accepted during the insert procedure. So you have to add ErrorProvider1 to our form. Next, is the definition of IsSuccessResult variable which will be declare in our module PayrollMod.vb as Boolean. The purpose for this to check our Insert Statement if it is successful or not See code below.

  1. Public IsSuccessResult As Boolean

We just call our saveInsert() sub procedure in our PayrollMod.vb Module and added the Insert statement. For me, using module in our procedure is the best algorithm because you have coded only once and we just have to change our Insert statement. Proceed now to our next lesson for the Update application.

If you have any questions or suggestions about Insert Statement Using Visual Basic Application, please feel free to contact me at our contact page.

Previous topic MySQL Insert, Update, Delete Statement for Managing Payroll System Module

Facebook Comments


Please enter your comment!
Please enter your name here