Hello, thank you for considering my question.
I have some code below which loads a recordset object (rs) from a stored query.
I also have a second recordset object (rsBlank) with the same Field structure as rs.
The goal is to take the records from rs and copy them into rsBlank.
rs is a massive table being distilled down to just a few dozen records.
rsBlank is a blank table... whose sole job is to copy those few dozen records for scrutiny in more involved code later.
Seems simple enough:
Code:
Dim rs As Recordset
Set rs = CurrentDb.OpenRecordset("qryAssignInvoiceNumbers_UnInvoiced", dbOpenSnapshot) 'Get all live orders with no invoice yet.
Dim rsBlank As Recordset
Set rsBlank = CurrentDb.OpenRecordset("tblAssignInvoiceNumbers", dbOpenDynaset) 'I have tried dbOpenTable, which I thought should be sufficient, but I get this same problem.
'Clear the local table.
CurrentDb.Execute "DELETE * FROM tblAssignInvoiceNumbers"
'Collect into local table.
If rs.EOF = False Then rs.MoveFirst 'I added this line hoping it would help avoid the problem. It does not.
Do While Not rs.EOF
rsBlank.AddNew
rsBlank![CompanyName] = rs![CompanyName]
rsBlank![OrderID] = rs![OrderID]
rsBlank![OrderDate] = rs![OrderDate]
rsBlank![Jobname] = rs![Jobname]
rsBlank![Product] = rs![Product]
rsBlank![Description] = rs![Description]
rsBlank![Price] = rs![Price]
rsBlank.Update
rs.MoveNext
Loop
I don't get an "error", but this is the problem:
If you double click on tblAssignInvoiceNumbers, which is the table rsBlank is connected to, the records are not in the same order as they are in rs.
So for example in rs, the first record could have the OrderID 1000 and the last record could be 1100. However, in the tblAssignInvoiceNumbers the middle record might be OrderID 1000.
It get's weirder: It only happens sometimes. I'd say 80% of the time it runs perfectly and rsBlank is a direct copy of the recordset, row-by-row. Other times, it's as if you cut a deck of cards in half: the bottom 50 records starts on top of the table, and the top 50 records are on the bottom! Not sure why.
All I know for sure is: rs is reading fine. I've stepped through the code in break mode and read each field value through each Loop iteration, it's always sorted perfectly in accordance with the ORDER BY clause of the rs Query. But then when the code is finished and you look at the grid being written to, sometimes it's just not in the same order.
Any ideas why this could happen? I can find a workaround perhaps, but I'd like to learn.
Thank you,
Matt