Results 1 to 10 of 10

Thread: Need help with 'Find' coding

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2008
    Posts
    232

    Need help with 'Find' coding

    I am trying to implement a Find button in my form.
    There are 2 text boxes : one for part code and the other for part seq number and 2 Find buttons, one for each of the above.

    Here is my code:
    Code:
    Public Class MRP_Planning
    
        Private Sub Part_Planning_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Me.PartsTableAdapter.Fill(Me.PartDataSet.Parts)
        End Sub
    
        Private Sub btnFindSeqNo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFindSeqNo.Click
            Dim indexNumber As Integer = Me.PartsBindingSource.Find("SequenceNumber", Me.FindBySequenceNo.Text)
            Me.PartsBindingSource.Position = indexNumber
           
        End Sub
    
        Private Sub btnFindPartCode_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFindPartCode.Click
            Dim indexNumber As Integer = Me.PartsBindingSource.Find("PartCode", Me.FindByPartCode.Text)
            Me.PartsBindingSource.Position = indexNumber
        End Sub
    End Class
    Is it possible for me to set the code where there is only 1 button and 1 textbox that the code will read the input values of part code and seq number?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Need help with 'Find' coding

    And then how will the app know whether to search for a part number or sequence number?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    May 2008
    Posts
    232

    Re: Need help with 'Find' coding

    Sorry my mistakes. It's 1 button and 2 textboxes. Will that still be possible?

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Need help with 'Find' coding

    So you want to search for both at the same time, or just one TextBox will be populated at a time?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    May 2008
    Posts
    232

    Re: Need help with 'Find' coding

    If possible, I like it to search both at the same time...

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Need help with 'Find' coding

    That's a bit tricky. The BindingSource only supports searching by one column at a time. You could search the underlying DataTable:
    vb.net Code:
    1. Dim rows As DataRow() = myDataTable.Select(String.Format("SequenceNumber = '{0}' AND PartCode = '{0}'", _
    2.                                                          Me.FindBySequenceNo.Text, _
    3.                                                          Me.FindByPartCode.Text), _
    4.                                            myBindingSource.Filter)
    5.  
    6. If rows.Length > 0 Then
    7.     For index As Integer = 0 To myBindingSource.Count - 1
    8.         If DirectCast(myBindingSource(index), DataRowView).Row = rows(0) Then
    9.             myBindingSource.Position = index
    10.             Exit For
    11.         End If
    12.     Next
    13. End If
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    May 2008
    Posts
    232

    Re: Need help with 'Find' coding

    I gotten 2 errors while inputting the above code.
    1. Name 'myDataTable' is not declared
    2. Operator '=' is not defined for types 'System.Data.DataRow' and 'System.Data.DataRow'

    How do I rid of the errors?

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Need help with 'Find' coding

    1. Say it to yourself: "myDataTable". What do you suppose that refers to? That code is an EXAMPLE. You have to adapt it to YOUR project, so you have to use YOUR DataTable. Don't just copy and paste blindly. Look, read and think about what the code is actually doing.

    2. That one's my fault. It should be 'Is' instead of '='.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    May 2008
    Posts
    232

    Re: Need help with 'Find' coding

    datatable as in? Is it the data source that I bind it to?

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Need help with 'Find' coding

    This:
    Code:
    Me.PartsTableAdapter.Fill(Me.PartDataSet.Parts)
    is YOUR DataTable.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width