none
Check 2 text box and validate the data with access RRS feed

  • Question

  • Good day,

    I'm a novice in VB. net and i would like to check 2 text box and if there match based on the data that is store in access. 


    It will prompt a message,

    If yes = sucessful,

    If no = unsucessful.

    Is there a web i can visit to further check on this or can the community provide a sample code. 

    Monday, February 10, 2020 6:03 AM

Answers

  • Good day,

    Thanks for taking your time in helping my question.

    Sadly, I do not quite understand.

    Is it possible if you show the sample code with some interface?

    Its easy so I'm not understanding what is do difficult to understand as the interface is yours.

    Imports System.Data.OleDb
    
    Public Class SampleForm
        Private Sub SearchButton_Click(sender As Object, e As EventArgs) Handles SearchButton.Click
            If Not String.IsNullOrWhiteSpace(EmployeeNumberTextBox.Text) AndAlso Not String.IsNullOrWhiteSpace(InvitationTextBox.Text) Then
                If EmployeeExists(CInt(EmployeeNumberTextBox.Text), InvitationTextBox.Text) Then
                    MessageBox.Show("Exist")
                Else
                    MessageBox.Show("Not exist")
                End If
            End If
        End Sub
        Public Function EmployeeExists(employeeNumber As Integer, invitationCardValue As String) As Boolean
    
            Using cn As New OleDbConnection("Your connection string goes here")
                Using cmd As New OleDbCommand With {.Connection = cn}
                    cmd.CommandText =
                        "SELECT * FROM SomeTable " &
                        "WHERE EmployeeId = @EmployeeId AND CardValue = @CardValue"
    
                    cmd.Parameters.AddWithValue("@EmployeeId", employeeNumber)
                    cmd.Parameters.AddWithValue("@CardValue", invitationCardValue)
    
                    cn.Open()
    
                    Dim reader = cmd.ExecuteReader()
                    Return reader.HasRows
    
                End Using
            End Using
        End Function
    End Class
    


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by Zetsubo69 Tuesday, February 11, 2020 5:48 AM
    Tuesday, February 11, 2020 3:01 AM
    Moderator

All replies

  • The best way is to have a function with parameters that their type matches the fields to find a match. Rather than do this in a button click event and even better perform all and any data operations in a class rather than a form.

    In the following example EmployeeNumber is a Integer and InvitationCard is a string.

    The SELECT statement does SELECT * but you could specify any valid field in the database table. reader.HasRows returns true if there is one or more matches, otherwise false for no matches.

    Public Function EmployeeExists(employeeNumber As Integer, invitationCardValue As String) As Boolean
    
        Using cn As New OleDbConnection("Your connection string goes here")
            Using cmd As New OleDbCommand With {.Connection = cn}
                cmd.CommandText =
                    "SELECT * FROM SomeTable " &
                    "WHERE EmployeeId = @EmployeeId AND CardValue = @CardValue"
    
                cmd.Parameters.AddWithValue("@EmployeeId", employeeNumber)
                cmd.Parameters.AddWithValue("@CardValue", invitationCardValue)
    
                cn.Open()
    
                Dim reader = cmd.ExecuteReader()
                Return reader.HasRows
    
            End Using
        End Using
    End Function

    Note that Parameters with MS-Access are by ordinal position so you could use ? in place of names but using names its easier to read the code to know exactly what you have.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Monday, February 10, 2020 1:13 PM
    Moderator
  • Good day,

    Thanks for taking your time in helping my question.

    Sadly, I do not quite understand.

    Is it possible if you show the sample code with some interface?

    Tuesday, February 11, 2020 2:19 AM
  • Good day,

    Thanks for taking your time in helping my question.

    Sadly, I do not quite understand.

    Is it possible if you show the sample code with some interface?

    Its easy so I'm not understanding what is do difficult to understand as the interface is yours.

    Imports System.Data.OleDb
    
    Public Class SampleForm
        Private Sub SearchButton_Click(sender As Object, e As EventArgs) Handles SearchButton.Click
            If Not String.IsNullOrWhiteSpace(EmployeeNumberTextBox.Text) AndAlso Not String.IsNullOrWhiteSpace(InvitationTextBox.Text) Then
                If EmployeeExists(CInt(EmployeeNumberTextBox.Text), InvitationTextBox.Text) Then
                    MessageBox.Show("Exist")
                Else
                    MessageBox.Show("Not exist")
                End If
            End If
        End Sub
        Public Function EmployeeExists(employeeNumber As Integer, invitationCardValue As String) As Boolean
    
            Using cn As New OleDbConnection("Your connection string goes here")
                Using cmd As New OleDbCommand With {.Connection = cn}
                    cmd.CommandText =
                        "SELECT * FROM SomeTable " &
                        "WHERE EmployeeId = @EmployeeId AND CardValue = @CardValue"
    
                    cmd.Parameters.AddWithValue("@EmployeeId", employeeNumber)
                    cmd.Parameters.AddWithValue("@CardValue", invitationCardValue)
    
                    cn.Open()
    
                    Dim reader = cmd.ExecuteReader()
                    Return reader.HasRows
    
                End Using
            End Using
        End Function
    End Class
    


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by Zetsubo69 Tuesday, February 11, 2020 5:48 AM
    Tuesday, February 11, 2020 3:01 AM
    Moderator
  • Good day,

    Sorry to bring up the thread again.

    As from the above code is to connect to Access as the database.

    Can i have the code to connect for SQL.

    Thursday, February 13, 2020 8:52 AM
  • See the following 

    https://github.com/karenpayneoregon/MoveFromAccessToSqlServerWithVisualBasicNet/blob/master/BasicRead/Classes/DataOperationsAccess.vb#L5


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Thursday, February 13, 2020 10:31 AM
    Moderator