Imports System.Data.SqlClient
Public Class Form12

    Private Sub Form12_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        txt1.Enabled = False
        txt2.Enabled = False
        txt3.Enabled = False
        txt4.Enabled = False
        txt6.Enabled = False
        txt7.Enabled = False
        txt8.Enabled = False
        txt10.Enabled = False
        txt5.Enabled = False

        lock()
        searchshow()


        isearch.Enabled = True
        inew.Enabled = True
        isave.Enabled = False
        icancel.Enabled = False
        iupdate.Enabled = False
        idel.Enabled = False

    End Sub
    Private Sub clear()
        txt1.Text = ""
        txt2.Text = ""
        txt3.Text = ""
        txt4.Text = ""
        txt5.Text = ""
        txt6.Text = ""
        txt7.Text = ""
        txt8.Text = ""
        txt9.Text = ""
        txt10.Text = ""
        txt11.Text = ""
        txt12.Text = ""

    End Sub
    Private Sub lock()
        'txt1.Enabled = False
        'txt2.Enabled = False
        'txt3.Enabled = False
        'txt4.Enabled = False
        'txt5.Enabled = False
        'txt6.Enabled = False
        'txt7.Enabled = False
        'txt8.Enabled = False
        txt9.Enabled = False
        'txt10.Enabled = False
        txt11.Enabled = False
        txt12.Enabled = False
    End Sub
    Private Sub unlock()

        'txt1.Enabled = True
        'txt2.Enabled = True
        'txt3.Enabled = True
        'txt4.Enabled = True
        'txt5.Enabled = True
        'txt6.Enabled = True
        'txt7.Enabled = True
        'txt8.Enabled = True
        txt9.Enabled = True
        'txt10.Enabled = True
        txt11.Enabled = True
        txt12.Enabled = True

    End Sub
   



    Private Sub save()
        Call main()
        Con.Open()
        Dim adapter As New SqlDataAdapter
        Dim sql As String
        sql = "insert into admissionfee (patient_id,lname,fname,mname,addres,radioexam,labexam,pharmacy,doctorfee,roombill,othersbill,total) values('" & txt1.Text & "','" & txt2.Text & "', '" & txt3.Text & "', '" & txt4.Text & "', '" & txt5.Text & "','" & txt6.Text & "','" & txt7.Text & "','" & txt8.Text & "','" & txt9.Text & "','" & txt10.Text & "','" & txt11.Text & "','" & txt12.Text & "')"
        Try

            adapter.InsertCommand = New SqlCommand(sql, Con)
            adapter.InsertCommand.ExecuteNonQuery()
            MsgBox("Successful To save this record on database", MsgBoxStyle.Information, "Information On Database")
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
        Con.Close()
    End Sub


    Private Sub updatesave()
        Call main()
        Con.Open()
        Dim adapter As New SqlDataAdapter
        Dim sql As String

        sql = "update admissionfee set patient_id='" & txt1.Text & "',lname='" & txt2.Text & "',fname='" & txt3.Text & "',mname='" & txt4.Text & "',addres='" & txt5.Text & "',radioexam='" & txt6.Text & "',labexam='" & txt7.Text & "',pharmacy='" & txt8.Text & "',doctorfee='" & txt9.Text & "',roombill='" & txt10.Text & "',othersbill='" & txt11.Text & "',total='" & txt12.Text & "' where patient_id ='" & txtsearch.Text & "'"
        Try
            adapter.UpdateCommand = Con.CreateCommand
            adapter.UpdateCommand.CommandText = sql
            adapter.UpdateCommand.ExecuteNonQuery()
            MsgBox("Successful To Update this record on database", MsgBoxStyle.Information, "Information On Database")
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
        Con.Close()
    End Sub

   
    Private Sub deleterecord()
        Call main()
        Con.Open()


        Dim adapter As New SqlDataAdapter
        Dim sql As String

        sql = "delete admissionfee where patient_id ='" & txtsearch.Text & "'"
        Try
            adapter.DeleteCommand = Con.CreateCommand
            adapter.DeleteCommand.CommandText = sql
            adapter.DeleteCommand.ExecuteNonQuery()
            MsgBox("Successful To Delete this record on database", MsgBoxStyle.Information, "Information On Database")
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
        Con.Close()
    End Sub

    '********auto complete Search*********************
    Private Sub searchshow()
        Call main()
        Con.Open()

        Dim cmd As New SqlCommand("SELECT patient_id FROM admissionfee", Con)
        Dim ds As New DataSet
        Dim da As New SqlDataAdapter(cmd)
        da.Fill(ds, "My List") 'list can be any name u want

        Dim col As New AutoCompleteStringCollection
        Dim i As Integer
        For i = 0 To ds.Tables(0).Rows.Count - 1
            col.Add(ds.Tables(0).Rows(i)("patient_id").ToString())
        Next
        txtsearch.AutoCompleteSource = AutoCompleteSource.CustomSource
        txtsearch.AutoCompleteCustomSource = col
        txtsearch.AutoCompleteMode = AutoCompleteMode.Suggest

        'para sa text1
       
        Con.Close()
    End Sub
    Private Sub searchshow1()
        Call main()
        Con.Open()

        Dim cmd As New SqlCommand("SELECT patient_id FROM Patient_table", Con)
        Dim ds As New DataSet
        Dim da As New SqlDataAdapter(cmd)
        da.Fill(ds, "My List") 'list can be any name u want

        Dim col As New AutoCompleteStringCollection
        Dim i As Integer
        For i = 0 To ds.Tables(0).Rows.Count - 1
            col.Add(ds.Tables(0).Rows(i)("patient_id").ToString())
        Next
        txt1.AutoCompleteSource = AutoCompleteSource.CustomSource
        txt1.AutoCompleteCustomSource = col
        txt1.AutoCompleteMode = AutoCompleteMode.Suggest

        'para sa text1

        Con.Close()
    End Sub
    Private Sub showsearch()
        Call main()
        Con.Open()
        Dim UserSelect As SqlCommand
        Dim myreader As SqlDataReader
        Dim sql As String

        sql = "select * from admissionfee where patient_id like '" & (txtsearch.Text) & "%'"
        UserSelect = New SqlCommand(sql, Con)
        myreader = UserSelect.ExecuteReader
        If (myreader.Read = True) Then
            If txtsearch.Text = myreader(0) Then
                unlock()
                inew.Enabled = False
                icancel.Enabled = True
                iupdate.Enabled = True
                idel.Enabled = True

                txt1.Text = myreader(0)
                txt2.Text = myreader(1)
                txt3.Text = myreader(2)
                txt4.Text = myreader(3)
                txt5.Text = myreader(4)
                txt6.Text = myreader(5)
                txt7.Text = myreader(6)
                txt8.Text = myreader(7)
                txt9.Text = myreader(8)
                txt10.Text = myreader(9)
                txt11.Text = myreader(10)
                txt12.Text = myreader(11)
                ycard.Enabled = False




            End If
        Else
            lock()
            MsgBox("No record Found on Database", MsgBoxStyle.Critical, "Error")
            inew.Enabled = True
            icancel.Enabled = False
            iupdate.Enabled = False
            idel.Enabled = False





            clear()
        End If

        Con.Close()
    End Sub
    Private Sub showpatient()
        Call main()
        Con.Open()
        Dim UserSelect As SqlCommand
        Dim myreader As SqlDataReader
        Dim sql As String

        sql = "select * from Patient_table where patient_id like '" & (txt1.Text) & "%'"
        UserSelect = New SqlCommand(sql, Con)
        myreader = UserSelect.ExecuteReader
        If (myreader.Read = True) Then
           

            'txt1.Text = myreader(0)
            txt2.Text = myreader(1)
            txt3.Text = myreader(2)
            txt4.Text = myreader(3)
            txt5.Text = myreader(6)
        Else
            txt6.Text = ""
            txt7.Text = ""
            txt8.Text = ""
            txt10.Text = ""
        End If

        Con.Close()
    End Sub
    Private Sub inew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles inew.Click
        isearch.Enabled = False
        inew.Enabled = False
        isave.Enabled = True
        icancel.Enabled = True
        iupdate.Enabled = False
        idel.Enabled = False
        txtsearch.Enabled = False
        clear()
        txtsearch.Text = ""
        txt1.Enabled = True
        searchshow1()
        txt1.Focus()
        unlock()
    End Sub

    Private Sub isave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles isave.Click
        save()


        clear()
        lock()
        searchshow()

        isearch.Enabled = True
        inew.Enabled = True
        isave.Enabled = False
        icancel.Enabled = False
        iupdate.Enabled = False
        idel.Enabled = False
        txtsearch.Enabled = True
    End Sub

    Private Sub icancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles icancel.Click
        lock()
        clear()
        txtsearch.Text = ""
        txt1.Enabled = False
        isearch.Enabled = True
        inew.Enabled = True
        isave.Enabled = False
        icancel.Enabled = False
        iupdate.Enabled = False
        idel.Enabled = False
        txtsearch.Enabled = True
    End Sub

    Private Sub iupdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles iupdate.Click
        On Error GoTo err


        If txtsearch.Text = "" Then
            MsgBox("Sorry you have no record to update!", MsgBoxStyle.Critical, "Error Update")

        Else
            Select Case MsgBox("Are you sure, you want to Update this Record?", MsgBoxStyle.YesNo)
                Case MsgBoxResult.Yes
                    updatesave()


                    clear()
                    txtsearch.Text = ""
                    lock()

                    searchshow()


                    isearch.Enabled = True
                    inew.Enabled = True
                    isave.Enabled = False
                    icancel.Enabled = False
                    iupdate.Enabled = False
                    idel.Enabled = False
                Case MsgBoxResult.No
                    GoTo err
            End Select


        End If
err:
        Exit Sub
    End Sub

    Private Sub idel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles idel.Click
        On Error GoTo err


        If txtsearch.Text = "" Then
            MsgBox("Sorry you have no record to Delete!", MsgBoxStyle.Critical, "Error Delete")


        Else
            Select Case MsgBox("Are you sure, you want to Delete this Record?", MsgBoxStyle.YesNo)
                Case MsgBoxResult.Yes
                    deleterecord()


                    clear()
                    txtsearch.Text = ""
                    lock()

                    searchshow()


                    isearch.Enabled = True
                    inew.Enabled = True
                    isave.Enabled = False
                    icancel.Enabled = False
                    iupdate.Enabled = False
                    idel.Enabled = False

                Case MsgBoxResult.No
                    GoTo err
            End Select
        End If


err:
        Exit Sub
    End Sub

    Private Sub txtsearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtsearch.Click
        If txtsearch.Text = "" Then
            clear()
        End If
    End Sub

    Private Sub txtsearch_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtsearch.KeyUp
        If e.KeyCode = Keys.Enter Then
            showsearch()
        End If
    End Sub

    Private Sub isearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles isearch.Click
        If txtsearch.Text = "" Then
            MsgBox("Search Patient Id is Null ", MsgBoxStyle.Critical, "Error Search")
        Else
            showsearch()
        End If
    End Sub

    'Find record all belling****************

    

    Private Sub VerifypatientPharmacy()
        Call main()
        Con.Open()
        Dim UserSelect As SqlCommand
        Dim myreader As SqlDataReader
        Dim sql As String

        sql = "select * from  Pharmacy where patient_id like '" & (txt1.Text) & "%'"
        UserSelect = New SqlCommand(sql, Con)
        myreader = UserSelect.ExecuteReader
        If (myreader.Read = True) Then
            txt8.Text = myreader(6)
        Else
            GoTo exx
        End If

        Con.Close()
exx:
        Exit Sub
    End Sub


    Private Sub Verifypatientroom()
        Call main()
        Con.Open()
        Dim UserSelect As SqlCommand
        Dim myreader As SqlDataReader
        Dim sql As String

        sql = "select * from  room where patient_id like '" & (txt1.Text) & "%'"
        UserSelect = New SqlCommand(sql, Con)
        myreader = UserSelect.ExecuteReader
        If (myreader.Read = True) Then
            txt10.Text = myreader(10)
        Else
            GoTo exx
        End If

        Con.Close()
exx:
        Exit Sub
    End Sub


    Private Sub VerifypatientRadiological()
        Call main()
        Con.Open()
        Dim UserSelect As SqlCommand
        Dim myreader As SqlDataReader
        Dim sql As String

        sql = "select * from  Radiological where patient_id like '" & (txt1.Text) & "%'"
        UserSelect = New SqlCommand(sql, Con)
        myreader = UserSelect.ExecuteReader
        If (myreader.Read = True) Then
            txt6.Text = myreader(10)
        Else
            GoTo exx
        End If

        Con.Close()
exx:
        Exit Sub
    End Sub


    Private Sub Verifypatientlaboratory()
        Call main()
        Con.Open()
        Dim UserSelect As SqlCommand
        Dim myreader As SqlDataReader
        Dim sql As String

        sql = "select * from  laboratory where patient_id like '" & (txt1.Text) & "%'"
        UserSelect = New SqlCommand(sql, Con)
        myreader = UserSelect.ExecuteReader
        If (myreader.Read = True) Then
            txt7.Text = myreader(9)
        Else
            GoTo exx
        End If

        Con.Close()
exx:
        Exit Sub
    End Sub


   
    Private Sub txt1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txt1.KeyUp
        If e.KeyCode = Keys.Enter Then
            showpatient()
            VerifypatientPharmacy()
            Verifypatientroom()
            VerifypatientRadiological()
            Verifypatientlaboratory()
        End If
    End Sub

   

    Private Sub txt1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txt1.TextChanged
        If txt1.Text = "" Then
            txt6.Text = ""
            txt7.Text = ""
            txt8.Text = ""
            txt10.Text = ""
        End If
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim a, b, c, d, ee, f As Single
        Dim sum As String
        a = txt6.Text
        b = txt7.Text
        c = txt8.Text
        d = txt9.Text
        ee = txt10.Text
        f = txt11.Text
        sum = a + b + c + d + ee + f
        txt12.Text = sum

    End Sub

   
    Private Sub ycard_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ycard.Click
        If txt12.Text = "" Then
            MsgBox("total is a null please input the value", MsgBoxStyle.Exclamation)
        Else

        
            If ycard.Text = "SWA" Then
                ycard.Text = "Cancel"
                Dim com As String
                Dim a, b As Single
                a = txt12.Text
                b = 0.3
                com = a * b
                com = com - a
                txt12.Text = com
            ElseIf ycard.Text = "Cancel" Then
                ycard.Text = "SWA"
                Dim a, b, c, d, ee, f As Single
                Dim sum As String
                a = txt6.Text
                b = txt7.Text
                c = txt8.Text
                d = txt9.Text
                ee = txt10.Text
                f = txt11.Text
                sum = a + b + c + d + ee + f
                txt12.Text = sum

            End If
        End If
    End Sub
End Class