Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Find Next

Status
Not open for further replies.
Apr 12, 2004
98
US
I have a program that look to sql to hold it's data. I have the use the following code to find specific data. Let's say I'm looking up Joseph in the database. There might be five Joseph's. I'd like to find the next Joseph if the first wasn't what I was looking for. Any help will be appreciated.

TIA
Dim fileasname as string
dim inttemp as integer
fileasname = InputBox("What agent do you want to find", "Find")
Me.BindingContext(dtagency).Position = 0
For inttemp = 1 To dtagency.Rows.Count - 1
If dtagency.Rows(inttemp).Item("file_as_name") = fileasname Then
ExitFor
Else
Me.BindingContext(dtagency).Position += 1
EndIf
Next
EndSub
 
Also, It always finds the "last" instance of it in the table. I have my table sorted alphabetically.
 
Why not just prompt the user if he/she has found the correct record? This saves them from typing multiple times in an InputBox.
Code:
Sub Whatever()
     Dim cm As CurrencyManger = Me.BindingContext(YourDataSource)
     Dim iOldPosition As Integer = cm.Position 'Needed to reset position if record not found
     Dim iCount As Integer
     Dim bRecordFound As Boolean = False 'Used to flag if a mtach was found
     Dim sSearchString As String = InputBox("What Do You Want To Find?").Trim

     cm.Position = 0 'Set Currency Manager's Position to 0 to start search

     For Each r As DataRow In YourDataTable.Rows
          If r.Item("file_as_name") = sSearchString Then
               If MsgBox("Is this a match?" & VbCrLf & r.Item("file_as_name") & VbCrLf & r.Item("Some_Other_Field_To_Identify_By"), MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
                    cm.Position = iCount
                    bFoundRecord = True
                    Exit For
               End If
          End If
          iCount += 1
     Next

     If bFoundRecord = False Then cm.Position = iOldPosition
End Sub
 
This doesn't work either. Unfortunately it's still going to the last record in the table...
 
It just jumps to the last record of the table anytime you search?
 
Yes, it always goes to the last row in the table. The last code that you (RiverGuy) sent did not work at all...

thanks for the help..
 
I sorted on file_as_name in ascending order. Is this what you're asking?
 
No, I'm asking if you sorted with a DataView, or sorting in your SQL statement, or??
 
That is your problem, then. If you have modified .DefaultView, what you need to do, if you choose to use my code, is to change

Code:
For Each r As DataRow In YourDataTable.Rows


TO:

Code:
For Each r As DataRowView In YourDataTable.DefaultView

If you choose to use any other code samples above, without examining them, I would assume your problem stems from the same thing--you are looping through the DataTable instead of the DataView.
 
Ah but binding data in .Net is completely different than VB 6. I share your opinion if it applies to VB 6, but not VB.Net.
 
I agree, data binding is vastly different in .Net. But binding data to the GUI is still a bad idea. It's still much easier to work with a data abstraction layer. Which in .Net is bound to a data object but in VB would have been a collection of data.

-Rick

----------------------
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top