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 Wanet Telecoms Ltd 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
 
just out of curiocity, have you looked into the .Rows().find(value) method? You could likely use it to acheive the same goal w/o having to loop through the whole recordset.

-Rick

----------------------
 
I finally got the code I used to work... I'd like to try to stick with it and add to it if possible
 
make a new button with search next on it and add this code

Code:
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 = [b]val(txtrecordnumber.text)[/b] 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

Christiaan Baes
Belgium

If you want to get an answer read this FAQ faq796-2540
There's no such thing as a winnable war - Sting
 
I'm a little confused... I changed my code to match the code you gave but how do I get to the next record?? The code does find the first but if I click on the next button it doesn't go to the next matching record. If I click on the find button again then it asks me for my "What do you want to find again????
 
perhaps this will help

Code:
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 = val(txtrecordnumber.text) [b]+1[/b] 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

Christiaan Baes
Belgium

If you want to get an answer read this FAQ faq796-2540
There's no such thing as a winnable war - Sting
 
What that code does is use a flexible starting point so that each time it is ran it should start searching after the one you are currently on. although I think it's just a tad off.

Code:
  Private m_ContinueSearch As Boolean = False

  Public Sub search()

    Dim intStart As Integer

    If Not m_ContinueSearch Then
      fileasname = InputBox("What agent do you want to find", "Find")
      Me.BindingContext(dtagency).Position = 0
      intStart = 0
    Else
      intStart = Val(txtrecordnumber.Text) + 1
    End If

    For intTemp = intStart To dtagency.Rows.Count - 1
      If dtagency.Rows(intTemp).Item("file_as_name") = FileAsName Then
        Exit For
      Else
        Me.BindingContext(dtagency).Position += 1
      End If
    Next
    m_ContinueSearch = True
    If intTemp = dtagency.Rows.Count - 1 And dtagency.Rows(intTemp).Item("file_as_name") <> FileAsName Then
      MsgBox("Agent not found.")
      m_ContinueSearch = False
    End If
  End Sub

That should continue any search (with out prompting for value) that was successful on it's first run. But I think using a .select or .find could be just as effective depending on how you are using the DS elsewheres.

-Rick

----------------------
 
Thanks ThatRickGuy but the code that you suggested did not work. I kept getting error "There is no row at position 6" so I put m_ContinueSearch = True
If intTemp = dtagency.Rows.Count - 1 And dtagency.Rows(intTemp).Item("file_as_name") <> FileAsName Then
MsgBox("Agent not found.")
m_ContinueSearch = False
End If
in the loop but now I just get message "Agent not found
 
Put a break on this line:
Code:
If dtagency.Rows(intTemp).Item("file_as_name") = FileAsName Then
[/clode]

keep hitting F5 until you get to one that should be true. make sure that there isn't a padding or case issue. you may want to add .ToString.Trim.ToUpper to the field and .Trim.ToUpper to the variable.

-Rick

----------------------
[URL unfurl="true"]http://www.ringdev.com[/URL]
 
Oh, and this part:
Code:
If intTemp = dtagency.Rows.Count - 1 And dtagency.Rows(intTemp).Item("file_as_name") <> FileAsName Then
  MsgBox("Agent not found.")
  m_ContinueSearch = False
End If

needs to be outside the loop, but change it too:

Code:
If intTemp = dtagency.Rows.Count Then
  MsgBox("Agent not found.")
  m_ContinueSearch = False
End If

-Rick

----------------------
 
Hi! I am new in VBA programming and I will be gratefull if someone help me.
My problem is: I have got row data from measurement.The data fill Column A and Column B. I have to work with every cell in column B and calculate their averaged value.For example, at first average of B1, next average of B1 and B2, next average of B1, B2 and B3 and etc.I have used loop for this(but it doesn´t work). Along this, I should compare averaged value to the value of the next cell(for example, average of B1:B10 compared to B11). If the difference between average and the value of the next cell is greater than 0,0004, the loop should stop and the average should be displayed in cell C1.
I have tryied some variants but they didn´t work.
Please, can you offer me some code for my problem.
Thank you in advance.
 
I changed the code as you had suggested. It does come up with the first instance of the name then immediately comes up with the error "Agent not found".


Private m_ContinueSearch As Boolean = False

Private Sub btnfind_agent_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnfind_agent.Click
Dim fileasname As String
Dim inttemp As Integer
Dim intStart As Integer

If Not m_ContinueSearch Then
fileasname = InputBox("What agent do you want to find", "Find")
Me.BindingContext(dtagency).Position = 0
intStart = 0
Else
intStart = Val(txtrecordnumber.Text) + 1
End If
For intTemp = intStart To dtagency.Rows.Count - 1
If dtagency.Rows(intTemp).Item("file_as_name") = FileAsName Then
Exit For
Else
Me.BindingContext(dtagency).Position += 1
End If
Next
m_ContinueSearch = True
If inttemp = dtagency.Rows.Count Then
MsgBox("Agent not found.")
m_ContinueSearch = False
End If
End Sub
 
Okay, slight change, I just set it to use a boolean instead of muckin arround trying to check the iterator position:

Code:
  Private m_ContinueSearch As Boolean = False

  Private Sub btnfind_agent_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnfind_agent.Click
    Dim blnAgentFound As Boolean
    Dim fileasname As String
    Dim inttemp As Integer
    Dim intStart As Integer

    If Not m_ContinueSearch Then
      fileasname = InputBox("What agent do you want to find", "Find")
      Me.BindingContext(dtagency).Position = 0
      intStart = 0
    Else
      intStart = Val(txtrecordnumber.Text) + 1
    End If
    blnAgentFound = False
    For inttemp = intStart To dtagency.Rows.Count - 1
      If dtagency.Rows(inttemp).Item("file_as_name") = fileasname Then
        blnAgentFound = True
        Exit For
      Else
        Me.BindingContext(dtagency).Position += 1
      End If
    Next
    m_ContinueSearch = True
    If Not blnAgentFound Then
      MsgBox("Agent not found.")
      m_ContinueSearch = False
    End If
  End Sub

-Rick

----------------------
 
Thanks I tried your new code but it didn't work for me either. I get the same thing... First instance is found then messagebox pops up right away...
 
this is getting ridiculus(spelling?) or funny
Code:
private intstart as integer = 0

  Private Sub btnfind_agent_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnfind_agent.Click
    Dim blnAgentFound As Boolean
    Dim fileasname As String
    Dim inttemp As Integer

    fileasname = InputBox("What agent do you want to find", "Find")
    Me.BindingContext(dtagency).Position = intstart
    blnAgentFound = False
    For inttemp = intStart To dtagency.Rows.Count - 1
      If dtagency.Rows(inttemp).Item("file_as_name") = fileasname Then
        blnAgentFound = True
        intstart = inttemp
        Exit For
      Else
        Me.BindingContext(dtagency).Position += 1
      End If
    Next
    If Not blnAgentFound Then
      MsgBox("Agent not found.")
      intstart = 0
    End If
  End Sub

Christiaan Baes
Belgium

If you want to get an answer read this FAQ faq796-2540
There's no such thing as a winnable war - Sting
 
I really appreciate all of the help and YES Chris this is "ridiculus". Same results doesn't work. HEELLLLPPPP!!!!!
 
it has to work

Code:
private intstart as integer = 0

  Private Sub btnfind_agent_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnfind_agent.Click
    Dim blnAgentFound As Boolean
    Dim fileasname As String
    Dim inttemp As Integer

    fileasname = InputBox("What agent do you want to find", "Find")
    Me.BindingContext(dtagency).Position = intstart
    blnAgentFound = False
    For inttemp = intStart To dtagency.Rows.Count - 1
      If dtagency.Rows(inttemp).Item("file_as_name") = fileasname Then
        blnAgentFound = True
        intstart = inttemp+1
        Exit For
      Else
        Me.BindingContext(dtagency).Position += 1
      End If
    Next
    If Not blnAgentFound Then
      MsgBox("Agent not found.")
      intstart = 0
    End If
  End Sub

[code]

Christiaan Baes
Belgium

[b]If you want to get an answer read this FAQ faq796-2540[/b]
[i]There's no such thing as a winnable war - Sting[/i]
 
Thanks Chris but I am still getting the same results. Even tried pointing it to a different column in my table that I know has multiple listings of same?????
 
Rick

try not to sound to desperate, you're a guru and and are a rolemodel now ;-)

Denise

I give up for now

send me the datatbase backup file and the project and I will look at it first thing tomorow evening and make even more improvements over this very long weekend.



Christiaan Baes
Belgium

If you want to get an answer read this FAQ faq796-2540
There's no such thing as a winnable war - Sting
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top