Results 1 to 6 of 6

Thread: Using rs.Seek to Find a Record?

  1. #1

    Thread Starter
    Addicted Member gt123's Avatar
    Join Date
    May 2006
    Location
    United Kingdom
    Posts
    145

    Using rs.Seek to Find a Record?

    Hi there,

    I'd like to add a find record option to my application, and also have a combo box that I would like to load a record relating to the combo box selection. To this end, can anyone explain how I use rs.Seek to find a record within a recordset?

    Thank you, GT

  2. #2
    Fanatic Member Dnereb's Avatar
    Join Date
    Aug 2005
    Location
    Netherlands
    Posts
    863

    Re: Using rs.Seek to Find a Record?

    Example from MS help file abour .seek:
    it uses the example database Northwind.

    This example demonstrates the Seek method by allowing the user to search for a product based on an ID number.

    Code:
    Sub SeekX()
    
        Dim dbsNorthwind As Database
        Dim rstProducts As Recordset
        Dim intFirst As Integer
        Dim intLast As Integer
        Dim strMessage As String
        Dim strSeek As String
        Dim varBookmark As Variant
    
        Set dbsNorthwind = OpenDatabase("Northwind.mdb")
        ' You must open a table-type Recordset to use an index, 
        ' and hence the Seek method.
        Set rstProducts = _
            dbsNorthwind.OpenRecordset("Products", dbOpenTable)
    
        With rstProducts
            ' Set the index.
            .Index = "PrimaryKey"
    
            ' Get the lowest and highest product IDs.
            .MoveLast
            intLast = !ProductID
            .MoveFirst
            intFirst = !ProductID
    
            Do While True
                ' Display current record information and ask user 
                ' for ID number.
                strMessage = "Product ID: " & !ProductID & vbCr & _
                    "Name: " & !ProductName & vbCr & vbCr & _
                    "Enter a product ID between " & intFirst & _
                    " and " & intLast & "."
                strSeek = InputBox(strMessage)
    
                If strSeek = "" Then Exit Do
    
                ' Store current bookmark in case the Seek fails.
                varBookmark = .Bookmark
    
                .Seek "=", Val(strSeek)
    
                ' Return to the current record if the Seek fails.
                If .NoMatch Then
                    MsgBox "ID not found!"
                    .Bookmark = varBookmark
                End If
            Loop
    
            .Close
        End With
    
        dbsNorthwind.Close
    
    End Sub
    why can't programmers keep and 31 Oct and 25 dec apart. Why Rating is Useful
    for every question you ask provide an answer on another thread.

  3. #3

    Thread Starter
    Addicted Member gt123's Avatar
    Join Date
    May 2006
    Location
    United Kingdom
    Posts
    145

    Re: Using rs.Seek to Find a Record?

    Thanks for posting, but I don't really know where to begin with that as my application appears quite different.

    My DB connection is made using the following code:
    VB Code:
    1. 'Sets a new DB connection
    2.         Set cn = New ADODB.Connection
    3.                 cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    4.                 "Data Source=" & App.Path & "\vc.mdb"
    5.         cn.Open
    6.        
    7. 'Sets a new DB recordset
    8.         Set rs = New ADODB.Recordset
    9.             rs.Open "Files", cn, adOpenKeyset, adLockPessimistic, adCmdTable

    My database fields are filled using the following code:
    VB Code:
    1. Private Sub FillFields()
    2.    
    3.     'This fills the text boxes with data from the recordset
    4.         If Not (rs.BOF = True Or rs.EOF = True) Then
    5.             txtCode.Text = rs.Fields("Code")
    6.             txtLocation.Text = rs.Fields("Location") & ""
    7.             txtTitle.Text = rs.Fields("Title") & ""
    8.             txtFormat.Text = rs.Fields("Format") & ""
    9.             txtLength.Text = rs.Fields("Length") & ""
    10.             txtGenre.Text = rs.Fields("Category") & ""
    11.            
    12.         'Loads the title code to be used to load images and videos
    13.             mediacode = rs.Fields("Code")
    14.         Else
    15.             MsgBox "You have reached the first or last record.", vbExclamation, "Cannot Move"
    16.         End If
    17.            
    18. End Sub

    I also have a combo box that lists all of the movie titles using the following code:
    VB Code:
    1. 'Loop adds the titles to the combo box
    2.         Do Until rs.EOF = True
    3.             cmbSelector.AddItem rs.Fields("Title")
    4.             rs.MoveNext
    5.         Loop

    And finally, the user can select a title from the combo box to load the related record using this code:

    VB Code:
    1. Private Sub cmbSelector_Click()
    2.     Dim strSQL As String
    3.    
    4.     'This SQL specifies the data to be loaded
    5.         strSQL = "SELECT * FROM Files WHERE Title = '" + cmbSelector.List(cmbSelector.ListIndex) + "'"
    6.    
    7.     'Closes any open recordset
    8.         rs.Close
    9.    
    10.     'Uses the SQL to load the data into the recordset (rs)
    11.         rs.Open strSQL, cn, adOpenKeyset, adLockPessimistic, adCmdText
    12.  
    13.     'Puts the data (from the recordset) into the controls
    14.         FillFields
    15.  
    16. End Sub

    The problem with this method is that once the user selects a title from the combo box, the 'next' and 'previous' record buttons do not function because there is now only one record. Someone in my other thread said I needed to change my combo box selection to use the seek method instead of loading in just 1 record.

    Can anyone help?

  4. #4
    Fanatic Member Dnereb's Avatar
    Join Date
    Aug 2005
    Location
    Netherlands
    Posts
    863

    Re: Using rs.Seek to Find a Record?

    So now you have to decide on a major issue.

    Do You want to use SQL... giving me more to code but locking the database less
    and in general create a faste application.
    Or do you want to use a dynamic recordset locking up more but easier to code.

    If you opt for the first (my preference) your code could be improved to use an readonly forwardonly recordset to load the first record and the listbox.
    these are much faster.
    Close it afterwards. And put The listindex in to a varaible at form level
    Code:
    Private CurrentRecordIndex as Long
    Rewrite your Fillfields to accept a (Optional) string argument and use the SQL system to find your record.

    If you want the next record you can add 1 to this variable, Set the listindex to this value. give the text of the listbox as argument to your fillfields and done.

    This way you can minimize data trafic.
    Another thing is you need to index these fields to be able to use .seek

    Makes me wonder do you use a bound or unbound form? (ADODC = bound)
    why can't programmers keep and 31 Oct and 25 dec apart. Why Rating is Useful
    for every question you ask provide an answer on another thread.

  5. #5

    Thread Starter
    Addicted Member gt123's Avatar
    Join Date
    May 2006
    Location
    United Kingdom
    Posts
    145

    Re: Using rs.Seek to Find a Record?

    I'm all for which ever method you think is best, I just don't want the code to go way over my head. As for the unbound / bound question, I have no idea what the difference is. I'm very new to this as I only went through Beacon's ADO tutorial two days ago.

    Thanks for your help.

  6. #6
    Fanatic Member Dnereb's Avatar
    Join Date
    Aug 2005
    Location
    Netherlands
    Posts
    863

    Re: Using rs.Seek to Find a Record?

    Okay let's keep it simple.
    Bound form's it isn't the way "Pro's" work but to gewt a grip, it is easier to use.
    Some basic questions

    First things first.
    - Are you in VB 6.0 or ACCESS VBA

    - Do you only want to search for things or do you want to be able to chang, add or delete as well?

    Bound means your dastabox is directly linked to the data in the database and if you change a value it's automaticly changed in the database as well.
    downside is record locking and errorhandling is done by the database not you.
    Unbound means you have to take care of everything.
    why can't programmers keep and 31 Oct and 25 dec apart. Why Rating is Useful
    for every question you ask provide an answer on another thread.

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