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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Finding previous records -- searching from last to first

Status
Not open for further replies.

atlanticdit

IS-IT--Management
Jan 15, 2003
29
US
Hi All,
I have used code found here to find a record based on a keyword. I've developed from my limited knowledge a way to search for the next record with the same keyword. What I'm trying to do now is to figure out how to find previous records once I've passed them. I have a Previous button but this is to go back by one record and not by keyword. I've tried making the iCounter start with the record before the current one and end with the first record in the database but it didn't work. Kept telling me it couldn't find any records. This is a SQL 2000 database accessed through VB .NET 2003.

Any thoughts?

This is the code for finding a record based on a keyword: (thanks ThatRickGuy)
Dim iCounter As Integer
Dim bRecordFound As Boolean = False
Dim strKeyword As String

strKeyword = txtSearch.Text
For iCounter = 0 To DsTest1.Tables("Log").Rows.Count - 1
If DsTest1.Tables("Log").Rows(iCounter).Item("Date") = strKeyword Then
bRecordFound = True
Exit For
End If
Next

If bRecordFound Then
Me.BindingContext(DsTest1, "Log").Position = iCounter
Else
MessageBox.Show("Search string not found")
End If

The code for going to the Next record with the keyword is:
Dim iCounter As Integer
Dim bRecordFound As Boolean = False
Dim strKeyword As String

strKeyword = txtSearch.Text
For iCounter = Me.BindingContext(DsTest1, "Log").Position + 1 To DsTest1.Tables("Log").Rows.Count - 1
If DsTest1.Tables("Log").Rows(iCounter).Item("Date") = strKeyword Then
bRecordFound = True
Exit For
End If
Next

If bRecordFound Then
Me.BindingContext(DsTest1, "Log").Position = iCounter
Else
MessageBox.Show("Search string not found")
End If

Thanks,
Dave
 
Another option would be to limit your datatable to just the records that match the search criteria. That would make moving from one to the next in either direction easier.

Code:
dim drSearchResults() as datarow
'bind your displays to the drSearchResults instead of 
'dsTest1 in the constructor or in On_Load

public sub Search(KeyWord as String)
  drSearchResults = DsTest1.Tables("Log").Select("Date=" & KeyWord)
  if drSearchResults.Length = 0 then
    'No Records Found, notify and clear
    MessageBox.Show("Search string not found")
    drSearchResults = DsTest1.Tables.Rows
  else
    MessageBox.Show(drSearchResults.length & " matching records found")
  end if
End sub

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
I would think about using a DataView instead of a DataTable. The filter can be applied or taken off. For example, if you applied a filter and got 3 records, then your back and forward buttons would scroll through just those 3 records. Take the filter off, and it scrolls through the whole table.
 
Thanks ThatRickGuy but when I do the search it is looking for a column named by the Keyword. So if I do a search for Outlook, it will search for a column named Outlook. I have a column named IssueOn that I'm trying to search for the word Outlook. Everytime I try that I get the error message stating that it cannont find a column named Outlook.

Also, the line of code "drSearchResults = DsTest1.Tables("Log").Rows" gives me an error message "...cannot be converted to 1-dimensional array...".

Thoughts?

code:
Code:
strKeyword = txtSearch.Text
        
        drSearchResults = DsTest1.Tables("Log").Select("IssueOn=" & strKeyword)
        If drSearchResults.Length = 0 Then
            MessageBox.Show("Keyword was not found")
            drSearchResults = DsTest1.Tables("Log").Rows
        Else
            MessageBox.Show(drSearchResults.Length & " matching records found")
        End If
 
You're using select on a string field, so you need to enclose the string in quotes.

Code:
strKeyword = txtSearch.Text.Trim
drSearchResults = DsTest1.Tables("Log").Select("IssueOn='" & strKeyword & "'")


Sweep
...if it works dont mess with it
 
Hi,
I am trying to implement the sam functions in a program. Can someone tell me what does DsTest1.Tables refer to in the project?

Thanks

Mo
 
DsTest1 is a dataset and Tables is the table collection in that dataset.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top