Imports System.Data.OleDb


Public Class btnSave

  

    Private Sub frmCustomerInfo_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load


        '  SetParent(Me.Handle, AboutGym.Handle)

        txtAge.MaxLength = 2 '// set max. allowed characters.

        txtCustomerNo.Enabled = False
        txtLastName.Enabled = False
        txtFirstName.Enabled = False
        txtLastName.Enabled = False
        txtMiddleName.Enabled = False
        txtAge.Enabled = False
        txtAddress.Enabled = False
        txtContactNo.Enabled = False
        cmbGender.Enabled = False
        cmbCategory.Enabled = False
        btnSave1.Enabled = False
        btnUpdate.Enabled = False


        conn = New OleDbConnection(cn)
        Try
            ' ORIG ------------------------------------------------------------
            conn.Open()

            cmd = New OleDbCommand("select * from CustomerInfo", conn)
            da = New OleDbDataAdapter(cmd)
            ds.Clear()
            da.Fill(ds, "CustomerInfo")
            DataGridView1.DataSource = ds.Tables("CustomerInfo")
            '---------------------------------------------------------
            conn.Close()


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





    Private Sub txtCustomerName_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtLastName.KeyPress
        Dim c As Char = e.KeyChar
        If Char.IsNumber(c) Then
            e.Handled = True
        End If
    End Sub
    Private Sub cmbGender_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles cmbGender.KeyPress
        Dim c As Char = e.KeyChar
        If Char.IsNumber(c) Then
            e.Handled = True
        End If
    End Sub
    Private Sub txtAge_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtAge.KeyPress
        Dim i As Char = e.KeyChar
        If Char.IsLetter(i) Then
            e.Handled = True
        End If
    End Sub

    Private Sub txtContactNo_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtContactNo.KeyPress
        Dim i As Char = e.KeyChar
        If Char.IsLetter(i) Then
            e.Handled = True
        End If
    End Sub
    Private Sub DataGridView1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGridView1.MouseUp
        'Putting infos from dbase to textboxes
        Dim i As Integer
        i = DataGridView1.CurrentRow.Index
        txtCustomerNo.Text = DataGridView1.Item(0, i).Value
        cmbCategory.Text = DataGridView1.Item(1, i).Value
        txtLastName.Text = DataGridView1.Item(2, i).Value
        txtFirstName.Text = DataGridView1.Item(3, i).Value
        txtMiddleName.Text = DataGridView1.Item(4, i).Value
        txtAddress.Text = DataGridView1.Item(5, i).Value
        DateTimePicker1.Text = DataGridView1.Item(6, i).Value
        txtAge.Text = DataGridView1.Item(7, i).Value
        cmbGender.Text = DataGridView1.Item(8, i).Value
        txtContactNo.Text = DataGridView1.Item(9, i).Value

        conn = New OleDbConnection(cn)

    End Sub





    

    Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

   


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

    End Sub

    Private Sub GroupBox1_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GroupBox1.Enter

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd1.Click
        txtLastName.Focus()
        txtLastName.Text = ""
        txtFirstName.Text = ""
        txtMiddleName.Text = ""
        txtAge.Text = ""
        txtAddress.Text = ""
        txtAge.Text = ""
        txtContactNo.Text = ""
        cmbGender.Text = ""
        cmbCategory.Text = ""
        conn = New OleDbConnection(cn)
        'autonum 
        Dim cmd As New OleDbCommand("Select CustomerNo from CustomerInfo", conn)
        conn.Open()
        dr = cmd.ExecuteReader
        While dr.Read()
            txtCustomerNo.Text = dr(0)
        End While
        txtCustomerNo.Text = Val(txtCustomerNo.Text) + 1
        dr.Close()
        conn.Close()
        '   btnSave.Enabled = True
        btnEdit.Enabled = False
        btnDelete.Enabled = False
        txtLastName.Enabled = True
        txtFirstName.Enabled = True
        txtMiddleName.Enabled = True
        txtAge.Enabled = True
        txtAddress.Enabled = True
        txtContactNo.Enabled = True
        cmbGender.Enabled = True
        cmbCategory.Enabled = True
    End Sub

    Private Sub btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave1.Click
        'Save Database

        If txtLastName.Text = "" Then
            MsgBox("Please enter Last Name")
            txtLastName.Focus()

        ElseIf txtFirstName.Text = "" Then
            MsgBox("Please enter First Name")
            txtFirstName.Focus()

        ElseIf txtMiddleName.Text = "" Then
            MsgBox("Please enter Middle Name")
            txtMiddleName.Focus()

        ElseIf txtAddress.Text = "" Then
            MsgBox("Please enter  Address")
            txtAddress.Focus()

        ElseIf txtAge.Text = "" Then
            MsgBox("Please enter  Age")
            txtAge.Focus()

        ElseIf DateTimePicker1.Text = "" Then
            MsgBox("Please enter  Birthday")
            DateTimePicker1.Focus()

        ElseIf cmbGender.Text = "" Then
            MsgBox("Please enter  Gender")
            cmbGender.Focus()

        ElseIf txtContactNo.Text = "" Then
            MsgBox("Please enter your Contact Number")
            txtContactNo.Focus()

        Else
            conn = New OleDbConnection(cn)
            Try
                conn.Open()
                str = "Insert into CustomerInfo values ('" & txtCustomerNo.Text & "','" & cmbCategory.Text & "' ,'" & txtLastName.Text & "' ,'" & txtFirstName.Text & "','" & txtMiddleName.Text & "','" & txtAddress.Text & "', '" & DateTimePicker1.Text & "','" & txtAge.Text & "','" & cmbGender.Text & "', '" & txtContactNo.Text & "')"
                cmd = New OleDbCommand(str, conn)
                cmd.ExecuteNonQuery()
                MsgBox("Record Saved Successfully!")

                txtLastName.Focus()
                txtLastName.Text = ""
                txtFirstName.Text = ""
                txtMiddleName.Text = ""
                txtAge.Text = ""
                txtAddress.Text = ""
                txtAge.Text = ""
                txtContactNo.Text = ""
                cmbGender.Text = ""
                cmbCategory.Text = ""

                txtCustomerNo.Enabled = False
                txtLastName.Enabled = False
                txtFirstName.Enabled = False
                txtMiddleName.Enabled = False
                txtAge.Enabled = False
                txtAddress.Enabled = False
                txtContactNo.Enabled = False
                cmbGender.Enabled = False
                cmbCategory.Enabled = False
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
            'conn.Close()
            Try
                ' ORIG ------------------------------------------------------------
                'conn.Open()

                cmd = New OleDbCommand("select * from CustomerInfo", conn)
                da = New OleDbDataAdapter(cmd)
                ds.Clear()
                da.Fill(ds, "CustomerInfo")
                DataGridView1.DataSource = ds.Tables("CustomerInfo")
                '---------------------------------------------------------
                conn.Close()


            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
            DataGridView1.Refresh()
        End If
        btnEdit.Enabled = True
        btnDelete.Enabled = True
    End Sub

    
    Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
        btnUpdate.Enabled = True
        btnAdd1.Enabled = False
        btnSave1.Enabled = False
        btnDelete.Enabled = False
        txtLastName.Enabled = True
        txtFirstName.Enabled = True
        txtMiddleName.Enabled = True
        txtAge.Enabled = True
        txtAddress.Enabled = True
        txtContactNo.Enabled = True
        cmbGender.Enabled = True
        cmbCategory.Enabled = True
    End Sub

    Private Sub btnUpdate_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
        conn = New OleDbConnection(cn)
        Try
            conn.Open()
            str = "Update CustomerInfo set Category = '" & cmbCategory.Text & "', LastName = '" & txtLastName.Text & "', FirstName =  '" & txtFirstName.Text & "', MiddleName =  '" & txtMiddleName.Text & "', Address =  '" & txtAddress.Text & "', Birthday = '" & DateTimePicker1.Text & "', Age = '" & txtAge.Text & "',  Gender = '" & cmbGender.Text & "', ContactNo = '" & txtContactNo.Text & "'  where CustomerNo = '" & txtCustomerNo.Text & "'"
            '  str = "Update CustomerInfo set CustomerName = '" & txtCustomerName.Text & "' where CustomerNo = '" & txtCustomerNo.Text & "'"
            cmd = New OleDbCommand(str, conn)
            cmd.ExecuteNonQuery()
            'conn.Close()
            MsgBox("Record Updated Successfully!")
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        btnAdd1.Enabled = True
        'conn.Close()
        Try
            ' ORIG ------------------------------------------------------------
            '    conn.Open()

            cmd = New OleDbCommand("select * from CustomerInfo", conn)
            da = New OleDbDataAdapter(cmd)
            ds.Clear()
            da.Fill(ds, "CustomerInfo")
            DataGridView1.DataSource = ds.Tables("CustomerInfo")
            '---------------------------------------------------------
            '  conn.Close()

            txtCustomerNo.Enabled = False
            txtLastName.Enabled = False
            txtFirstName.Enabled = False
            txtMiddleName.Enabled = False
            txtAge.Enabled = False
            txtAddress.Enabled = False
            txtContactNo.Enabled = False
            cmbGender.Enabled = False
            cmbCategory.Enabled = False
            btnAdd1.Enabled = True
            btnDelete.Enabled = True
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        DataGridView1.Refresh()
    End Sub

   
    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        'Delete
        Dim i As Integer
        i = MsgBox("Are you sure you want to delete the record?", vbYesNo, "Sweat and Shape Gym")
        If i = vbYes Then
            conn = New OleDbConnection(cn)
            Try
                conn.Open()
                str = "Delete from CustomerInfo where CustomerNo = '" & txtCustomerNo.Text & "'"
                cmd = New OleDbCommand(str, conn)
                cmd.ExecuteNonQuery()
                MsgBox("Record Deleted!")
                txtCustomerNo.Text = ""
                txtLastName.Text = ""
                txtFirstName.Text = ""
                txtMiddleName.Text = ""
                txtAddress.Text = ""
                txtAge.Text = ""
                cmbGender.Text = ""
                cmbCategory.Text = ""
                txtContactNo.Text = ""

            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
            'conn.Close()
            Try
                ' ORIG ------------------------------------------------------------
                'conn.Open()

                cmd = New OleDbCommand("select * from CustomerInfo", conn)
                da = New OleDbDataAdapter(cmd)
                ds.Clear()
                da.Fill(ds, "CustomerInfo")
                DataGridView1.DataSource = ds.Tables("CustomerInfo")
                '---------------------------------------------------------
                conn.Close()


            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
            DataGridView1.Refresh()
        End If
    End Sub

    Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click

    End Sub

    Private Sub txtCustomerNo_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtCustomerNo.TextChanged

    End Sub

    Private Sub txtContactNo_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtContactNo.TextChanged

    End Sub

    Private Sub Label8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label8.Click

    End Sub
End Class