Public Class Form1
    Private Sub btnempsave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnempsave.Click
        'CREATE A STRING VARIABLE THAT IS USED FOR CHECKING THE GENDER
        Dim rdo As String

        'CHECKING IF THE MALE OR FEMALE ARE CHECKED.
        If rdomale.Checked = True Then
            rdo = "MALE"
        Else
            rdo = "FEMALE"
        End If

        If txtcode.Enabled = True Then
            'STORE YOUR "INSERT QUERY" IN THE VARIABLE THAT YOU HAVE CREATED
            sql = "INSERT INTO tblemployeesWorkInfo (emp_code, d_rate, p_method, e_position, w_status, d_hired)" _
                    & " VALUES ('" & txtcode.Text & "','" & txtdrate.Text & "','" & txtpmethod.Text & "','" & txtposition.Text _
                    & "','" & txtworkstatus.Text & "','" & dtpdhired.Value & "')"
            'CALL THE SUB PROCEDURE THAT YOU HAVE CREATED AND SET YOUR QUERY IN THE PARAMETERS. 
            EmployeeCUD(sql)
            sql = "INSERT INTO tblemployee (emp_code, emp_fname, emp_lname, emp_mname" _
            & ", address, contact, status, birth_date, birth_place, emp_sex, emp_age" _
            & ", emerg_contct) VALUES ('" & txtcode.Text & "','" & txtfname.Text & "','" & txtlname.Text _
            & "','" & txtmname.Text & "','" & txtaddress.Text & "','" & txtcontact.Text & "','" & txtstatus.Text _
            & "','" & dtpdbirth.Value & "','" & txtbplace.Text & "','" & rdo & "','" & txtage.Text & "','" & txtemerg.Text & "')"
            EmployeeCUD(sql)
            MsgBox("The Data Has Been Save.", , "SAVED")
        Else
            sql = "UPDATE tblemployeesWorkInfo SET  d_rate= '" & txtdrate.Text & "', p_method='" & txtpmethod.Text & _
            "', e_position='" & txtposition.Text & "', w_status='" & txtworkstatus.Text & "', d_hired='" & dtpdhired.Value & _
            "'  WHERE emp_code='" & txtcode.Text & "'"
            EmployeeCUD(sql)
            sql = "UPDATE tblemployee SET emp_fname='" & txtfname.Text & "', emp_lname='" & txtlname.Text _
            & "', emp_mname='" & txtmname.Text & "' , address='" & txtaddress.Text & "', contact='" & txtcontact.Text & _
            "', status='" & txtstatus.Text & "', birth_date='" & dtpdbirth.Value & "', birth_place='" & txtbplace.Text & _
            "', emp_sex='" & rdo & "', emp_age='" & txtage.Text & "', emerg_contct='" & txtemerg.Text & _
            "' WHERE emp_code='" & txtcode.Text & "'"
            EmployeeCUD(sql)
            MsgBox("The Data Has Been Updated.", , "UPDATE")
        End If
        txtcode.Enabled = True
        clearAll(GroupBox9)
        clearAll(GroupBox10)
    End Sub

   
    Private Sub btnlist_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnlist.Click
        Try
            pnl_add.SendToBack()
            pnl_list.BringToFront()

            sql = "SELECT e.emp_code as [AssignCode],emp_fname & ' ' & emp_lname & ' ' & emp_mname as [Name]" & _
              ",address as [Address],emp_sex as [Gender], emp_age as [Age],emerg_contct as [ContactNumber]" & _
              ", d_rate as [DailyRate], p_method as [PaymentMethod], e_position as [Position], w_status as [WorkStatus]" & _
              ", d_hired as [HiredDate] FROM tblemployee as e,tblemployeesWorkInfo as w WHERE e.emp_code=w.emp_code"
            EmployeeDTG(sql, dtglist)
        Catch ex As Exception
            Me.Text = ex.Message
        End Try
    End Sub

    Private Sub btnadd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnadd.Click
        Try
            pnl_add.BringToFront()
            pnl_list.SendToBack()
            clearAll(GroupBox9)
            clearAll(GroupBox10)
            txtcode.Enabled = True
        Catch ex As Exception
            Me.Text = ex.Message
        End Try
    End Sub

    Private Sub btnupdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnupdate.Click
        Try
            pnl_add.BringToFront()
            pnl_list.SendToBack()
            txtcode.Text = dtglist.CurrentRow.Cells(0).Value
            txtcode.Enabled = False
            sql = "SELECT * FROM tblemployee as e,tblemployeesWorkInfo as w WHERE e.emp_code=w.emp_code AND  e.emp_code='" & txtcode.Text & "'"
            updateList(sql)
        Catch ex As Exception
            Me.Text = ex.Message
        End Try
    End Sub
    Public Sub updateList(ByVal sql As String)

        EmployeeTXT(sql)
        With dt.Rows(0)
            txtfname.Text = .Item("emp_fname")
            txtlname.Text = .Item("emp_lname")
            txtmname.Text = .Item("emp_mname")
            txtaddress.Text = .Item("address")
            txtcontact.Text = .Item("contact")
            txtstatus.Text = .Item("status")
            dtpdbirth.Value = .Item("birth_date")
            txtbplace.Text = .Item("birth_place")
            If .Item("emp_sex") = "FEMALE" Then
                rdofemale.Checked = True
            Else
                rdomale.Checked = True
            End If
            txtage.Text = .Item("emp_age")
            txtemerg.Text = .Item("emerg_contct")
            txtdrate.Text = .Item("d_rate")
            txtpmethod.Text = .Item("p_method")
            txtposition.Text = .Item("e_position")
            txtworkstatus.Text = .Item("w_status")
            dtpdhired.Value = .Item("d_hired")
        End With
    End Sub

    Private Sub btndelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btndelete.Click
        Try
            sql = "DELETE * FROM tblemployee   " & _
            " WHERE emp_code='" & dtglist.CurrentRow.Cells(0).Value & "'"
            EmployeeCUD(sql)
            sql = "DELETE * FROM tblemployeesWorkInfo   " & _
            " WHERE emp_code='" & dtglist.CurrentRow.Cells(0).Value & "'"
            EmployeeCUD(sql)
            MsgBox("The Data Has Been Deleted.", , "DELETE")

            Call btnlist_Click(sender, e)
        Catch ex As Exception
            Me.Text = ex.Message
        End Try
    End Sub

    Private Sub txtsearch_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtsearch.TextChanged
        Try
            sql = "SELECT e.emp_code as [AssignCode],emp_fname & ' ' & emp_lname & ' ' & emp_mname as [Name]" & _
           ",address as [Address],emp_sex as [Gender], emp_age as [Age],emerg_contct as [ContactNumber]" & _
           ", d_rate as [DailyRate], p_method as [PaymentMethod], e_position as [Position], w_status as [WorkStatus]" & _
           ", d_hired as [HiredDate] FROM tblemployee as e,tblemployeesWorkInfo as w WHERE e.emp_code=w.emp_code " & _
           " AND  e.emp_code LIKE '%" & txtsearch.Text & "%'"
            EmployeeDTG(sql, dtglist)
        Catch ex As Exception
            Me.Text = ex.Message
        End Try
    End Sub
 
    Private Sub btnPreview_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPreview.Click
        Try
            report.Show()
            report.Focus()
            sql = "SELECT e.emp_code as [AssignCode],emp_fname & ' ' & emp_lname & ' ' & emp_mname as [Name]" & _
               ",address as [Address],emp_sex as [Gender], emp_age as [Age],emerg_contct as [ContactNumber]" & _
               ", d_rate as [DailyRate], p_method as [PaymentMethod], e_position as [Position], w_status as [WorkStatus]" & _
               ", d_hired as [HiredDate] FROM tblemployee as e,tblemployeesWorkInfo as w WHERE e.emp_code=w.emp_code" & _
                " AND d_hired >= #" & dtpFrom.Value & "# and d_hired <= #" & dtpTo.Value & "#"
            reports(sql, "listdatehired")
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged
        Try
            report.Show()
            report.Focus()
            sql = "SELECT e.emp_code as [AssignCode],emp_fname & ' ' & emp_lname & ' ' & emp_mname as [Name]" & _
           ",address as [Address],emp_sex as [Gender], emp_age as [Age],emerg_contct as [ContactNumber]" & _
           ", d_rate as [DailyRate], p_method as [PaymentMethod], e_position as [Position], w_status as [WorkStatus]" & _
           ", d_hired as [HiredDate] FROM tblemployee as e,tblemployeesWorkInfo as w WHERE e.emp_code=w.emp_code" 
            reports(sql, "listemployees")
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
 
    Private Sub btnmanageuser_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnmanageuser.Click
        Try
            manageuser.Show()
        Catch ex As Exception

        End Try
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Call btnlist_Click(sender, e)
        btnlogin.SetBounds(6, 3, 134, 23)
        btnmanageuser.Visible = False
        btnlist.Visible = False
        btnadd.Visible = False
        GroupBox1.Visible = False
        lblusername.Visible = False
        clearAll(GroupBox9)
        clearAll(GroupBox10)

    End Sub

   
    Private Sub dtglist_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dtglist.CellContentClick

    End Sub

    Private Sub btnlogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnlogin.Click
        Try
            If btnlogin.Text = "Login" Then
                LoginForm1.Show()
            Else
                btnlogin.Text = "Login"
                btnlogin.SetBounds(6, 3, 134, 23)
                btnmanageuser.Visible = False
                btnlist.Visible = False
                btnadd.Visible = False
                GroupBox1.Visible = False
                lblusername.Text = "user's name"
                lblusername.Visible = False
                PictureBox1.Visible = True
                pnl_list.BringToFront()
                PictureBox1.BringToFront()
                clearAll(GroupBox9)
                clearAll(GroupBox10)
            End If

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
End Class
