Results 1 to 9 of 9
  1. #1
    jjmclell is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jul 2015
    Posts
    11

    Problem with moving thru recordset

    I'm having an issue with a recordset object where Access seems to start in the middle of the recordset even when I use MoveFirst. The object's RecordCount property also returns 4 more records than are actually in the table (should be 321, returns 325) even when I first use the MoveLast method to ensure I'm accessing all of the records.



    Code:
    Sub test()
    
    
        Dim dbs As Object
        Dim mainTable As Recordset
        Dim testValue As String
            
        Set dbs = Application.CurrentDb
        Set mainTable = dbs.OpenRecordset("tbl_Main", dbOpenTable)
        
        mainTable.MoveFirst
        testValue = mainTable.Fields("partners").Value
        Debug.Print testValue
    End Sub
    The result from this code is the value for the 21st record in the table I'm trying to work with.

  2. #2
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Strange that you are declaring dbs as an object.

    What happens when you try this:
    Code:
    Sub test()
    
        Dim dbs As DAO.Database
        Dim mainTable As DAO.Recordset
        Dim testValue   'variant
    
        Set dbs = CurrentDb
        Set mainTable = dbs.OpenRecordset("tbl_Main", dbOpenTable)
    
        If mainTable.BOF And mainTable.EOF Then
            Debug.Print "No records"
        Else
            mainTable.MoveLast
            Debug.Print "Record count = " & mainTable.RecordCount
    
            mainTable.MoveFirst
            testValue = mainTable.Fields("partners")
            Debug.Print "partners = " & testValue
        End If
        
        'clean up
        mainTable.Close
        Set mainTable = Nothing
        Set dbs = Nothing
    End Sub

  3. #3
    John_G is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    You are creating a recordset from tbl_Main without specifying a sort order. In my experience, when you do that Access will use the PK order, so if "Partners" is not the PK, that is your problem.

    Try using this:

    Set mainTable = dbs.OpenRecordset("Select * from tbl_Main order by partners")


    If you open the table from the Navigation pane, how many records does it have?

  4. #4
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,743

  5. #5
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    If your recordset is more than a single record why is only the first record important, that seems a bit odd to me. I would be more inclined to make sure the SQL statement was pulling only the record needed to cut down on processing time, or alternately cycle through the recordset to pull the information you want from each record. I'd use ssnafu's code and add a loop in the ELSE portion of the code like:

    Code:
    dim sPartner as long ' this is assuming you are pulling the partner name as a string and not actually using the primary key
    maintable.movelast
    debug.print "record count = " & maintable.recordcount
    maintable.movefirst
    
    do while maintable.eof <> true
        spartner = maintable.fields("partner")
        debug.print "    Partner name = " & spartner
        maintable.movenext
    loop

  6. #6
    jjmclell is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jul 2015
    Posts
    11
    Quote Originally Posted by ssanfu View Post
    Strange that you are declaring dbs as an object.

    What happens when you try this:
    Code:
    Sub test()
    
        Dim dbs As DAO.Database
        Dim mainTable As DAO.Recordset
        Dim testValue   'variant
    
        Set dbs = CurrentDb
        Set mainTable = dbs.OpenRecordset("tbl_Main", dbOpenTable)
    
        If mainTable.BOF And mainTable.EOF Then
            Debug.Print "No records"
        Else
            mainTable.MoveLast
            Debug.Print "Record count = " & mainTable.RecordCount
    
            mainTable.MoveFirst
            testValue = mainTable.Fields("partners")
            Debug.Print "partners = " & testValue
        End If
        
        'clean up
        mainTable.Close
        Set mainTable = Nothing
        Set dbs = Nothing
    End Sub
    I tried this code and was experiencing the same problems. I do believe I have figured out the aspect where the first record being returned is a random record in the table. I included this piece of code after setting the mainTable recordset:

    Code:
    mainTable.Index = "PrimaryKey"
    This had the effect of actually returning the first record when using .MoveFirst. As to why 4 extra records were being returned when using .RecordCount I haven't figured out, but I copied the table and deleted the original and the correct number of records started being returned. It is a table that I regularly import to from Excel so maybe something strange happened in a past import.

  7. #7
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,743
    To open a recordset that is Ordered in some repeatable manner, use a query as the source of the recordset.

    For example:

    Dim sql1 as string
    sql1 = "SELECT * from tbl_Main Order By YourImportantFieldHere;"

    Then
    ....
    Set dbs = CurrentDb
    Set mainTable = dbs.OpenRecordset("sql1")

  8. #8
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    I was curious about the number of records returned - but it seems that there was possibly a little corruption.

    OK... a table has NO inherent order. To have a sorted (ordered) list, you must use a query (as orange pointed out).
    What field do you want to sort by?

    Code:
    Sub test_2()
    
        Dim dbs As DAO.Database
        Dim mainTable As DAO.Recordset
        Dim testValue   'variant
        Dim sSQL As String
    
        Set dbs = CurrentDb
    
        sSQL = "SELECT * from tbl_Main Order By ORDER_FIELD;" '< change "ORDER_FIELD" the the field name you want to sort (order) by
        Set mainTable = dbs.OpenRecordset(sSQL)
    
        If mainTable.BOF And mainTable.EOF Then
            Debug.Print "No records"
        Else
            mainTable.MoveLast
            Debug.Print "Record count = " & mainTable.RecordCount
    
            mainTable.MoveFirst
            testValue = mainTable.Fields("partners")
            Debug.Print "partners = " & testValue
        End If
        
        'clean up
        mainTable.Close
        Set mainTable = Nothing
        Set dbs = Nothing
    End Sub

  9. #9
    jjmclell is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jul 2015
    Posts
    11
    The SQL worked perfectly. Thanks to everyone for their help.

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

Similar Threads

  1. Got a problem with Moving into SQL
    By winterh in forum Access
    Replies: 4
    Last Post: 06-08-2012, 07:31 AM
  2. Moving Code Between Data Tables Problem
    By Soule in forum Programming
    Replies: 6
    Last Post: 01-17-2012, 12:43 AM
  3. problem with dynamic moving columns in report
    By princeofdumph in forum Reports
    Replies: 1
    Last Post: 12-16-2011, 06:22 PM
  4. Problem moving database to another computer
    By DB2010MN26 in forum Access
    Replies: 4
    Last Post: 11-22-2011, 04:23 PM
  5. Replies: 7
    Last Post: 04-11-2011, 03:58 PM

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