Visual Studio 2019 (VB.NET) How to Search data in an Access database



📚 YouTube (Part 1/3)

📚 YouTube (Part 2/3)

📚 YouTube (Part 3/3)


[Free Source Code by iBasskung]

#BEGIN

Option Explicit On
Option Strict On

Public Class FormPersonnelManagement

Private IsAddNewRecordInProgress As Boolean = False

Private Sub Employee_DataBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs) Handles Employee_DataBindingNavigatorSaveItem.Click

        ' Save
        'Me.Validate()
        'Me.Employee_DataBindingSource.EndEdit()
        'Me.TableAdapterManager.UpdateAll(Me.EmployeeDatabaseDataSet)

        ' TODO :)
        SaveButton.PerformClick()

    End Sub

Private Sub FormPersonnelManagement_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'EmployeeDatabaseDataSet.Employee_Data' table. You can move, or remove it, as needed.
        Me.Employee_DataTableAdapter.Fill(Me.EmployeeDatabaseDataSet.Employee_Data)

    End Sub

Private Sub BindingNavigatorAddNewItem_Click(sender As Object, e As EventArgs) Handles BindingNavigatorAddNewItem.Click
        ' TODO :)
        AddNewButton.PerformClick()
    End Sub

Private Sub BindingNavigatorDeleteItem_Click(sender As Object, e As EventArgs) Handles BindingNavigatorDeleteItem.Click
        ' TODO :)
        DeleteButton.PerformClick()
    End Sub

Private Sub ExitButton_Click(sender As Object, e As EventArgs) Handles ExitButton.Click
        ' Application.Exit()
        ' Nice!
        Me.Close()
    End Sub

Private Sub AddNewButton_Click(sender As Object, e As EventArgs) Handles AddNewButton.Click
        ' Add new
        ' MsgBox("You pressed Add New Button")

        Try
            With AddNewButton
                If .Text = "Add New Record" Then
                    Employee_DataBindingSource.AddNew()
                    .Text = "Cancel"
                Else
                    RefreshData()
                    .Text = "Add New Record"
                End If
            End With

            With FirstNameTextBox
                If (.CanSelect) Then
                    .Text = String.Empty
                    .Select()
                End If
            End With

            IsAddNewRecordInProgress = Convert.ToBoolean(AddNewButton.Text = "Cancel")

        Catch ex As Exception
            MsgBox("An Error Occured: " & ex.Message.ToString(),
                   MsgBoxStyle.OkOnly Or MsgBoxStyle.Information, "Add new record failed.")
        End Try

    End Sub

Private Sub RefreshData()

        Try
            Employee_DataTableAdapter.Fill(Me.EmployeeDatabaseDataSet.Employee_Data)
            Employee_DataBindingSource.RemoveFilter()
        Catch ex As Exception
            MsgBox("Refresh Data Error!")
        End Try

    End Sub

Private Sub SaveButton_Click(sender As Object, e As EventArgs) Handles SaveButton.Click
        ' Save | Update
        ' MsgBox("You pressed Save Button")

        Try

            Dim result As DialogResult
            result = MessageBox.Show("Do you want to save the selected record?", "Save Data : iBasskung Tutorial",
                                     MessageBoxButtons.YesNo, MessageBoxIcon.Question)

            If (result = DialogResult.Yes) Then
                Validate()
                Employee_DataBindingSource.EndEdit()
                TableAdapterManager.UpdateAll(Me.EmployeeDatabaseDataSet)

                MessageBox.Show("The record has been saved successfully.",
                            "Save Data : iBasskung Tutorial", MessageBoxButtons.OK, MessageBoxIcon.Information)

                RefreshData()

                AddNewButton.Text = "Add New Record"

            Else
                ' Exit Sub
                Return
            End If

        Catch ex As Exception
            MessageBox.Show("Save | Update Data Failed: " & ex.Message.ToString(),
                            "Save Data : iBasskung Tutorial", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try

    End Sub

Private Sub DeleteButton_Click(sender As Object, e As EventArgs) Handles DeleteButton.Click
        ' Delete
        ' MsgBox("You pressed Delete Button")

        Try
            If MessageBox.Show("Do you want to permanently delete the selected record?",
                               "Delete Data : iBasskung Tutorial", MessageBoxButtons.YesNo,
                               MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) =
                               Windows.Forms.DialogResult.Yes Then
                Employee_DataBindingSource.RemoveCurrent()

                Employee_DataBindingSource.EndEdit()
                Employee_DataTableAdapter.Update(EmployeeDatabaseDataSet.Employee_Data)

                RefreshData()

                MessageBox.Show("The record has been deleted.",
                            "Delete Data : iBasskung Tutorial", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Else
                Return ' Exit Sub
            End If
        Catch ex As Exception
            MessageBox.Show("Delete Data Failed: " & ex.Message.ToString(),
                            "Delete Data : iBasskung Tutorial", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try

    End Sub

Private Sub FilterCheckBox_CheckedChanged(sender As Object, e As EventArgs) Handles FilterCheckBox.CheckedChanged
        If IsAddNewRecordInProgress = True Then
            AlertWarningMessage()
            Exit Sub
        End If

        Dim boolCheck As Boolean = FilterCheckBox.Checked
        Dim stringFilter As String = $"Status = {boolCheck.ToString()}"

        Employee_DataBindingSource.Filter = stringFilter
        ToolStripStatusLabel1.Text = $"Status: IsActive = {boolCheck.ToString()}"

    End Sub

Private Sub AlertWarningMessage()
        ' Do not allow search while adding new record.
        MsgBox("The search function is disabled while adding new record.",
               MsgBoxStyle.OkOnly Or MsgBoxStyle.Exclamation,
               "Find records : iBasskung Tutorial")
    End Sub

Private Sub SearchButton_Click(sender As Object, e As EventArgs) Handles SearchButton.Click
        ToolStripStatusLabel1.Text = "Status"

        If IsAddNewRecordInProgress = True Then
            AlertWarningMessage()
            Exit Sub
        End If

        If Not String.IsNullOrEmpty(KeywordTextBox.Text) Then
            SearchInAccessDatabase(KeywordTextBox.Text.Replace("'", "''"))
        Else
            RefreshData()
            Return
        End If

    End Sub

Private Sub SearchInAccessDatabase(ByVal stringKeyword As String,
                                       Optional stringFilter As String = "")

        Try

            stringFilter = "(Convert(EmployeeID, 'System.String') LIKE '" & stringKeyword & "')" &
                " OR (CitizenID LIKE '" & stringKeyword & "')" &
                " OR (FirstName LIKE '%" & stringKeyword & "%')" &
                " OR (LastName LIKE '%" & stringKeyword & "%')" &
                " OR (FirstName + ' ' + LastName LIKE '%" & stringKeyword & "%')" &
                " OR (Email LIKE '%" & stringKeyword & "%')" &
                " OR ([Business Phone] LIKE '" & stringKeyword & "')" &
                " OR ([Home Phone] LIKE '" & stringKeyword & "')" &
                " OR ([Mobile Phone] LIKE '" & stringKeyword & "')" &
                " OR (JobTitle LIKE '%" & stringKeyword & "%')" &
                " OR (Address LIKE '%" & stringKeyword & "%')" &
                " OR (City LIKE '%" & stringKeyword & "%')" &
                " OR ([State / Province] LIKE '%" & stringKeyword & "%')"

            ' Sorry for the mistake ^_^

            Employee_DataBindingSource.Filter = stringFilter

            If Employee_DataBindingSource.Count < 1 Then
                MsgBox("-->> " & stringKeyword & " <<--" & vbNewLine & Environment.NewLine &
                       "The search item was not found.", MsgBoxStyle.OkOnly Or MsgBoxStyle.Information,
                       "Find Records : iBasskung Tutorial")

                RefreshData()

            End If

        Catch ex As Exception
            MessageBox.Show("An error occured: " & ex.Message.ToString(),
                            "Find Records : iBasskung Tutorial", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try

    End Sub

End Class

#END

⛳ Follow me around ⛳
✔ Want to get updates on new courses or other cool free stuff? Just follow me on social media if that's your thing!

📚 Pages:

📚 YouTube:

📚 Udemy:

📚 Twitter:

📚 Blogger:

ðŸ’Ŋ THANK YOU SO MUCH! ðŸ’Ŋ

Tags: visual studio 2019, visual basic 2019, vb.net, vb.net tutorial for beginners visual studio 2019, visual basic, vb, visual studio 2019 visual basic tutorial, visual studio 2019 tutorial, how to use visual studio 2019, visual basic tutorial 2019, access, visual studio

#vb #vbnet #visualbasic #visualbasicdatabase #RetailPOS #PointOfSale #POS #EmployeeTrainingManagement #EmployeeManagement #EmployeeManagementSystem #EmployeeTrainingRecords #Software #CodeAMinute #iBasskung #iBasskungTutorial #VisualCSharp #CSharp #Programming #MicrosoftAccess #AccessDatabase #SQLServer #CrystalReports #Udemy #OnlineCourses #YouTube 

Comments

  1. I love your tuts. Keep it up but make facebook post more English for us to understand.
    Thanks.

    ReplyDelete
  2. im getting errors when i click the checked box. says;

    "An unhandled exception of type 'System.Data.EvaluateException' occurred in System.Data.dll

    Additional information: Cannot find column [Status]."

    ReplyDelete

Post a Comment