Why is my RecordSet not starting from record 1? (1 Viewer)

bJay

New member
Local time
Yesterday, 23:06
Joined
Aug 29, 2014
Messages
4
Hello all;

There may be a simple explanation to this, and hoping someone can help me.

I can't figure out why my Recordset start from record # 301 instead of # 1 Here's part of my code:

Code:
Set db2 = CurrentDb
Set rst2 = db2.OpenRecordset(strTable2)

If rst2.RecordCount = 0 Then
    MsgBox "No records to process."
    Exit Function
End If

rst2.MoveLast
rst2.MoveFirst
' loop through it & ...
Do Until rst2.EOF
    ' get id & url
    intId = rst2("ID")
    url = rst2("Url")
Loop

Table has 12,000 records and the first record has an ID of 1. So why is it starting from the record 301? What am I doing wrong?

I thank you for your time.
BJ
 

pbaldy

Wino Moderator
Staff member
Local time
Yesterday, 22:06
Joined
Aug 30, 2003
Messages
36,137
You can't count on the order of records in a table. Open the recordset on an SQL statement that includes an ORDER BY clause.
 

CJ_London

Super Moderator
Staff member
Local time
Today, 06:06
Joined
Feb 19, 2013
Messages
16,701
first and last don't mean anything as such - records are stored and returned in random order unless you sort the data.

so assuming strTable2 is the name of a table, modify your code to

Set rst2 = db2.OpenRecordset("SELECT * FROM [" & strTable2 & "] ORDER BY ID")
 

bJay

New member
Local time
Yesterday, 23:06
Joined
Aug 29, 2014
Messages
4
CJ_London/pbaldy Thank you so much!!! :D ... It never occurred to me that is the case. I just assumed I will get the first record (id=1) in table.

Have a great day!
BJ
 

Users who are viewing this thread

Top Bottom