Results 1 to 12 of 12

Thread: please tell me how to search in a recordset?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2006
    Location
    Pakistan
    Posts
    262

    please tell me how to search in a recordset?

    Hi,

    I want to serach records from the record set i hv created in the following code.This record set
    Contains values such as

    DD Admin-1
    DD Admin-2
    D-1
    D-2
    AD-1
    AD-2

    I just want to match these fields wid the "combo box value" n as in the example i gave the letters
    b4 "-" can be same,so first i hv to extract the fields which hv same letters b4 - n then to find out
    the last no.Such as first i found

    D-1
    D-2

    thn i hv to check the max no..in this case it is 2..
    So kindly tell me where is the problem in this code as it is giving error on "rs.BOF"
    VB Code:
    1. Public Sub Designation()
    2. Dim find_value As String
    3. Set conn = New clsConnection
    4. Set rs = New ADODB.Recordset
    5.  
    6. Dim count_value As Integer
    7.  
    8. Dim results As String
    9. Dim index As String
    10. Dim no As String
    11.  
    12.  
    13.  
    14.  
    15. conn.clsOpen
    16. find_value = "SELECT Designation from System_Users;"
    17. rs.Open find_value, conn.cn, adOpenDynamic, adLockBatchOptimistic
    18. 'count_value = rs.Fields.Count
    19.  
    20. rs.BOF
    21. If Not rs.EOF Then
    22.  
    23.       Do While Not rs.EOF
    24.         'type_value = rs("Type_Name")
    25.           ' Form4.cmbID.AddItem type_value
    26.        
    27.            results = rs.find("form4.cmbRank.text", adSearchForward)
    28.            index = InStr(index, "-")
    29.            no = Left(results, Len(results) - index)
    30.            MsgBox no, vbOKOnly, "ALERT"
    31.             rs.MoveNext
    32.            
    33.       Loop
    34.  
    35.   End If
    36.   MsgBox results(index), vbOKOnly, "ALERT"
    37.  
    38.  
    39.   conn.clsClose
    40.      
    41. End Sub

    Waiting for ur urgent reply

    Regards,

  2. #2
    Addicted Member
    Join Date
    Oct 2004
    Location
    Clane, Ireland
    Posts
    179

    Re: please tell me how to search in a recordset?

    If I understand your question correctly i.e. you have a value in a combo box, and you want to use that value to find a particular record then:

    You could change your FindValue to include a Where statement.

    If this does not appeal to you, then you need to change the code you have listed below, because the Find will search the entire recordset that you have brought into memory, so running it in a loop will only slow things down for you.

    Because you are running your search forward, you should issue a MOVEFIRST before the find, to ensure that you are not omitting any records.

    Also when using the FIND command, you have to tell it what you want to find
    eg
    Code:
    rs.Find "UsrId = " & Chr$(39) & *****r & Chr$(39), , adSearchForward
    If Rs.EOF then
    'rcd not found
    Else
    'Rcd Found
    End If
    The above implies that you would have to include an extra field in your record set.
    RS.BOF is a conditional that you can check, I don't believe that you can use it to move to the beginning of the file.

    HTH
    JP

    Please rate the postings

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2006
    Location
    Pakistan
    Posts
    262

    Re: please tell me how to search in a recordset?

    This code is giving error "Expected Function or variable" at the line.
    VB Code:
    1. results = rs.find("form4.cmbRank.text", adSearchForward)
    please guide me about the error

    Regards,

  4. #4
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: please tell me how to search in a recordset?

    The Find method is a Sub not a Function.
    The Criteria argument is basically the same as the Where clause in a SQL statement.
    SearchDirection is the third argument not the second.

    VB Code:
    1. strFind = "Designation = '" & form4.cmbRank.text  & "'"
    2. rs.find strFind, , adSearchForward

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2006
    Location
    Pakistan
    Posts
    262

    Re: please tell me how to search in a recordset?

    how the result of that find statement can be known.Means hoew we can retrive the records this statement will search.Please guide me.

  6. #6
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: please tell me how to search in a recordset?

    Just keep calling the Find method until you hit EOF.

    VB Code:
    1. 'Code to Open Recordset
    2. strFind = "Designation = '" & form4.cmbRank.text  & "'"
    3. rs.Find strFind, , adSearchForward
    4.  
    5. Do Until rs.EOF      
    6.      'code to process the record
    7.      rs.Find strFind,1 , adSearchForward
    8. Loop
    Last edited by brucevde; Sep 11th, 2006 at 01:47 PM.

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2006
    Location
    Pakistan
    Posts
    262

    Re: please tell me how to search in a recordset?

    Can u please tell me the type of rsFind?

  8. #8
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: please tell me how to search in a recordset?

    rsFind is a typo and should have been rs.Find

    rs is the ADO Recordset object.

    I updated the code in my previous post.

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2006
    Location
    Pakistan
    Posts
    262

    Re: please tell me how to search in a recordset?

    its giving error "Either BOF or EOF file true..Requested operation require a record.." on the line
    VB Code:
    1. rs.movenext
    Kindly guide me

  10. #10
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: please tell me how to search in a recordset?

    Are you using my code? If yes, MoveNext is not required within the loop.

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2006
    Location
    Pakistan
    Posts
    262

    Re: please tell me how to search in a recordset?

    Ok..Its done..Thankx now please tell me that how I can store the values of this recordset in an array so can have further serach over those values..As now the task is to serach the max no at the end of these values.

    Waiting for ur reply

  12. #12
    Addicted Member
    Join Date
    Oct 2004
    Location
    Clane, Ireland
    Posts
    179

    Re: please tell me how to search in a recordset?

    You do not need to do the search in a loop, unless there are multiple records in the recordset with the value you are looking for. You have a recordset, and the search starts at the current position, and looks from there to the bottom of the recordset, which is why I suggested using the .MOVEFIRST method, to position you at the top of the recordset.

    If the Find was unsucessful, the property .EOF will be set true.

    To load the record into an array, you would need to keep a counter variable, and every time you got a valid find you would add 1 to the counter, and then store the record in the array using the counter.

    HTH
    JP

    Please rate the postings

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