Results 1 to 5 of 5
  1. #1
    Ruegen's Avatar
    Ruegen is offline VIP
    Windows 8 Access 2010 64bit
    Join Date
    Jul 2013
    Location
    Australia
    Posts
    1,496

    dao.recordset - only do 10

    I'm trying to move through a recordset but I only want it to move through 10 records and then stop (even if the record count is higher)



    I tried a for loop but I get mixed results.

    a rs.eof works fine but I don't want all the results I only want the 10 of those (or less if there are less)

    remember if there are less than 10 records I can't use rs.movenext or I get an error as there aren't more than 10


    suggestions?

    Code:
    
    Dim rs As dao.Recordset
    Dim db As dao.Database
    
    
    Dim strSQL As String
    
    
    strSQL = "SELECT tblSchoolMemos.*, tblSchoolMemos.ID " & _
    "FROM tblSchoolMemos " & _
    "ORDER BY tblSchoolMemos.ID;"
    
    
    Set db = CurrentDb
    Set rs = db.OpenRecordset(strSQL)
    TextBoxControl = ""
    
    
    
    
    rs.MoveFirst
    rs.MoveLast
    
    
        If rs.RecordCount = 0 Then
        TextBoxControl = "No contact history as of yet"
        Else
    Dim countRec As Long
    countRec = rs.RecordCount
    Debug.Print String(255, vbNewLine)
    Debug.Print countRec
    
    
    rs.MoveFirst
    For i = 0 To IIf(countRec >= 10, 10, countRec)
     i = i + 1
    
                TextBoxControl = i & "   " & rs!contactmemo & "   " & rs!memodate & vbCrLf & TextBoxControl
                rs.MoveNext
    Next
    
    
        End If
    rs.Close
    Set rs = Nothing

  2. #2
    Ruegen's Avatar
    Ruegen is offline VIP
    Windows 8 Access 2010 64bit
    Join Date
    Jul 2013
    Location
    Australia
    Posts
    1,496
    This works too however I can't limit it to ten loops or less

    Code:
    Dim rs As dao.Recordset
    Dim db As dao.Database
    
    
    Dim strSQL As String
    
    
    strSQL = "SELECT tblSchoolMemos.*, tblSchoolMemos.ID " & _
    "FROM tblSchoolMemos " & _
    "ORDER BY tblSchoolMemos.ID;"
    
    
    Set db = CurrentDb
    Set rs = db.OpenRecordset(strSQL)
    TextBoxControl = ""
    
    
    
    
    rs.MoveFirst
    rs.MoveLast
    
    
        If rs.RecordCount = 0 Then
        TextBoxControl = "No contact history as of yet"
        Else
    
    
    Debug.Print String(255, vbNewLine); o
    
    
    
    
    rs.MoveFirst
    Do While Not rs.EOF
                
         
                TextBoxControl =  "   " & rs!contactmemo & "   " & rs!memodate & vbCrLf & TextBoxControl
                rs.MoveNext
    Loop
    
    
        End If
    rs.Close
    Set rs = Nothing

  3. #3
    amrut is offline Expert
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2012
    Location
    Dubai
    Posts
    614
    You can use SELECT TOP 10 instead of selecting all records in the SQL string. You must select the fields to be sorted precisely so as to achieve desired results. Another option is using a counter as you have done in post #1 and testing its value as you loop through the recordset. Perform the calculations as long as it is less then 10 else continue to MoveNext.
    Code:
    i=0
    Do While Not rs.EOF
    i=i+1
    if i<=10 then     
    'perform your calculations
    EndIf
    rs.MoveNext
    Loop

  4. #4
    Ruegen's Avatar
    Ruegen is offline VIP
    Windows 8 Access 2010 64bit
    Join Date
    Jul 2013
    Location
    Australia
    Posts
    1,496
    Quote Originally Posted by amrut View Post
    You can use SELECT TOP 10 instead of selecting all records in the SQL string. You must select the fields to be sorted precisely so as to achieve desired results. Another option is using a counter as you have done in post #1 and testing its value as you loop through the recordset. Perform the calculations as long as it is less then 10 else continue to MoveNext.
    Code:
    i=0
    Do While Not rs.EOF
    i=i+1
    if i<=10 then     
    'perform your calculations
    EndIf
    rs.MoveNext
    Loop
    Yeah, again that method doesn't work either. As soon as I try stuff like this it doesn't give me what I need correctly. It's very weird.

    I might have to make the SQL string limited to 10 results.

  5. #5
    Ruegen's Avatar
    Ruegen is offline VIP
    Windows 8 Access 2010 64bit
    Join Date
    Jul 2013
    Location
    Australia
    Posts
    1,496
    I have

    Code:
    
    Dim rs As dao.Recordset
    Dim db As dao.Database
    
    
    Dim strSQL As String
    
    
    strSQL = "SELECT TOP 10 tblSchoolMemos.*, tblSchoolMemos.ID " & _
    "FROM tblSchoolMemos " & _
    "ORDER BY tblSchoolMemos.ID desc;"
    
    
    Set db = CurrentDb
    Set rs = db.OpenRecordset(strSQL)
    TextBoxControl = ""
    
    
    
    
    rs.MoveFirst
    rs.MoveLast
    
    
        If rs.RecordCount = 0 Then
        TextBoxControl = "No contact history as of yet"
        Else
    
    
    
    
    Debug.Print String(255, vbNewLine); o
    
    
    rs.MoveFirst
    Do Until rs.EOF
    
    
    
    
    TextBoxControl = TextBoxControl & rs!memodate & "   " & rs!contactmemo & vbCrLf
    rs.MoveNext
    Loop
    
    
    
    
    
    
        End If
    rs.Close
    Set rs = Nothing
    it works and I think I will use this method.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 6
    Last Post: 12-03-2013, 11:14 PM
  2. How to Select second Recordset from first recordset
    By FrustratedAlso in forum Programming
    Replies: 28
    Last Post: 05-10-2012, 05:45 PM
  3. Replies: 2
    Last Post: 03-08-2012, 12:59 PM
  4. Recordset
    By Rick West in forum Programming
    Replies: 7
    Last Post: 11-14-2011, 02:40 PM
  5. Replies: 1
    Last Post: 11-13-2009, 03:03 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums