Consulting

Results 1 to 15 of 15

Thread: check if a value is in a recordset

  1. #1

    Exclamation check if a value is in a recordset

    How to check if a value is in a recordset?

    Thanks for your help.

  2. #2
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    ="Cat is in H11:L25 " & SUMPRODUCT(--(H11:L25="cat")) & " times."

    or use COUNTIF.

  3. #3

    Exclamation Recordset is holding data from SQL Server

    Hi,

    The recordset contains data from a table in SQL Server. I need to traverse the recordset in order to find if there is a match between a value in the recordset and the given value.

    Please help me out with the code.

    Thanks for your help.

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Use filter on the recordset.

    [vba]

    RS.Filter = "Gender = 'M'"[/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5

    Exclamation not working

    This is not working. Can you please suggest an alternative or send the full code.

    I am trying to check if a certain value exists in a recordset.

    Thanks for your help.

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Why don't you show us your code and what you want to check for?
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  7. #7

    Exclamation Code

    Here's my code:

    [VBA]Sub test_multiple_rows()
    Dim dbConnection As New ADODB.Connection
    Dim dbRecordset As New ADODB.Recordset
    Dim strConn As String
    Dim strSQL As String
    Dim ary As Variant

    Dim i As Integer
    Dim j As Integer
    Dim n As Integer
    strConn = "Driver={SQL Server};Server=Test-db;uid=hrpms;pwd=hrpms;databasename=hr_pms;"

    dbConnection.ConnectionString = strConn
    dbConnection.ConnectionTimeout = 60
    dbConnection.Open

    strSQL = "Select * from test_employee"


    'dbRecordset.CursorLocation = adUseServer
    'dbRecordset.Open strSQL, dbConnection, adOpenDynamic, adLockReadOnly, adCmdText
    dbRecordset.ActiveConnection = dbConnection
    dbRecordset.Open strSQL
    ary = dbRecordset.GetRows()

    For i = 0 To UBound(ary)
    For j = 0 To UBound(ary)
    If ary(i, j) = 2000 Then
    MsgBox "Found value"
    End If

    Next
    Next

    dbRecordset.Close
    dbConnection.Close
    Set dbRecordset = Nothing
    Set dbConnection = Nothing

    End Sub
    [/VBA]
    Last edited by Bob Phillips; 09-05-2011 at 07:54 AM.

  8. #8
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,720
    Location
    Are you trying to find the value in any field in the recordset, or a particular one? Your code ought to work for the former.
    Be as you wish to seem

  9. #9
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    What is the column name you are checking for that value?
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  10. #10

    Exclamation Employee_Id field

    Hi,

    I want to search in the Employee_id field only.

    Thanks for your help.

  11. #11
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,720
    Location
    Then xld's initial response is exactly what you need. How did you use it?
    Be as you wish to seem

  12. #12

    Exclamation xld's response

    Hi,

    I tried to use XLD's method.

    [vba]If dbRecordset.Filter = "Emp_Id=2000" Then ' 2000 is in the database
    MsgBox "Found value"
    Else
    MsgBox "Not Found value"
    End If

    If dbRecordset.Filter = "Emp_Id=10000" Then ' 10000 is not in the database
    MsgBox "Found value"
    Else
    MsgBox "Not Found value"
    End If[/vba]
    But this is always returning "Not Found value"
    Last edited by Aussiebear; 09-05-2011 at 11:16 PM. Reason: Applied VBA tags to code

  13. #13
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,060
    Location
    Are you sure that its not simply reflecting the the value from the first IF test?
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  14. #14
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    What you should do is something along these lines

    [vba]

    dbRecordset.Filter = "Emp_Id=2000"
    If Not dbecordset.EOF Then

    MsgBox "Record found"
    End If[/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  15. #15

    Exclamation Thanks

    Thanks a lot

Posting Permissions

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