Imports System.Data
Imports System.Data.OleDb
Public Class Form4
    Dim frm1 As New Form1
    Dim Sql1 As String

    Private Sub CmdHome_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdHome.Click
        Me.Close()
        frm1.Show()
    End Sub
    Private Sub CmdSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdSearch.Click

        If TextBox1.Text = "" Then
            MsgBox("يجب تحديد الكلمة المراد البحث عنها ")
            Exit Sub
        End If

        If RadioButton4.Checked = True Then
            ExactSearch()
            Sql1 = SQL
        Else
            GeneralSearch()
            Sql1 = SQL
        End If
        dbconn.Open()
        Dim dataadaptre1 As New OleDbDataAdapter(SQL, dbconn)
        dataadaptre1.Fill(Dataset1, "table1")
        dbconn.Close()
        TextBox2.DataBindings.Add("text", Dataset1, "table1.name")
        TextBox3.DataBindings.Add("text", Dataset1, "table1.city")
        TextBox4.DataBindings.Add("text", Dataset1, "table1.mobile")
        DataGridView1.DataSource = Dataset1
        DataGridView1.DataMember = "table1"
        CmdSearch.Enabled = False
        CmdNew.Enabled = True
        If Me.BindingContext(Dataset1, "table1").Count = 0 Then
            MsgBox("لا يوجد أي نتيجة , حاول مرة اخرى")
            Exit Sub
        End If
        CmdDelete.Enabled = True
        CmdChange.Enabled = True
    End Sub

    Public Sub ExactSearch()
        If RadioButton1.Checked = True Then
            SQL = "select * from table1 where name='" & TextBox1.Text & "'"
        ElseIf RadioButton2.Checked = True Then
            SQL = "select * from table1 where city='" & TextBox1.Text & "'"
        ElseIf RadioButton3.Checked = True Then
            SQL = "select * from table1 where mobile='" & TextBox1.Text & "'"
        End If
    End Sub
    Public Sub GeneralSearch()
        If RadioButton1.Checked = True Then
            SQL = "select * from table1 where name like '%" & TextBox1.Text & "%'"
        ElseIf RadioButton2.Checked = True Then
            SQL = "select * from table1 where city like '%" & TextBox1.Text & "%'"
        ElseIf RadioButton3.Checked = True Then
            SQL = "select * from table1 where mobile like '%" & TextBox1.Text & "%'"
        End If
    End Sub

    Private Sub CmdNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdNew.Click

        Dataset1.Reset()
        CmdSearch.Enabled = True
        CmdNew.Enabled = False

        Dim frm4 As New Form4
        Me.Close()
        frm4.Show()

    End Sub

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

        RadioButton1.Checked = True
        RadioButton5.Checked = True

    End Sub

    Private Sub CmdDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdDelete.Click

        If MsgBox("هل تريد حذف السجل الحالي", MsgBoxStyle.OkCancel, "تأكيد عملية الحذف") _
             = MsgBoxResult.Cancel Then
            Exit Sub
        End If
        SQL = "delete from table1 where mobile ='" & TextBox4.Text & "'"
        Dim delete1 As New OleDbCommand(SQL, dbconn)
        dbconn.Open()
        delete1.ExecuteNonQuery()
        Dataset1.Clear()
        Dim DataAdapter1 As New OleDbDataAdapter(Sql1, dbconn)
        DataAdapter1.Fill(Dataset1, "table1")
        dbconn.Close()
        MsgBox("تمت عملية الحذف من قاعدة البيانات بنجاح")



    End Sub


    Private Sub CmdChange_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdChange.Click

        CmdSave.Enabled = True
        CmdExit.Enabled = True
        CmdDelete.Enabled = False
        CmdChange.Enabled = False
        TextBox2.ReadOnly = False
        TextBox3.ReadOnly = False
        TextBox4.ReadOnly = False
        mobile = TextBox4.Text

    End Sub

    Private Sub CmdSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdSave.Click

        SQL = "update table1 set name='" & TextBox2.Text & "',city='" & TextBox3.Text & _
            "',mobile='" & TextBox4.Text & "' where mobile ='" & mobile & "'"
        Dim change As New OleDbCommand(SQL, dbconn)
        dbconn.Open()
        change.ExecuteNonQuery()
        Dataset1.Clear()
        Dim DataAdapter1 As New OleDbDataAdapter(Sql1, dbconn)
        DataAdapter1.Fill(Dataset1, "table1")
        dbconn.Close()
        CmdSave.Enabled = False
        CmdExit.Enabled = False
        CmdDelete.Enabled = True
        CmdChange.Enabled = True
        TextBox2.ReadOnly = True
        TextBox3.ReadOnly = True
        TextBox4.ReadOnly = True

    End Sub

    Private Sub CmdExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdExit.Click

        CmdSave.Enabled = False
        CmdExit.Enabled = False
        CmdDelete.Enabled = True
        CmdChange.Enabled = True
        TextBox2.ReadOnly = True
        TextBox3.ReadOnly = True
        TextBox4.ReadOnly = True

    End Sub
End Class