HalloweenWeed
Member
- Local time
- Today, 00:47
- Joined
- Apr 8, 2020
- Messages
- 220
Quick Q: Why is my vba recordset not sorted? I am doing some pretty advanced data validation and error-correction using multiple vba subroutines (let's not get into that as it will take MUCH too long to explain). My subroutine works most efficiently if the recordset data is sorted according to the values in two fields. So I am opening a recordset using an SQL string. But no matter what I put in my sort string, the records are in the order of the source table. I have verified this by using the following code to copy the recordset to a (temporary) table for debugging purposes:
The resulting table is not sorted, but in the exact same order that the source table is in.
This was the original recordset creation string:
but since it didn't work I thought perhaps it was because it was snapshot so I tried:
No joy.
First I tried using a simple SQL string:
but since that didn't work (sorted) I tried duplicating what I wanted in a Query, and putting that SQL string in:
I verified that the resulting query results were sorted. But again the resulting table wasn't.
Now I know there is a way to get around this (run an Access append query without any necessary parameters from vba), but I really want to know what I am doing wrong, is there a way to do this only in a recordset in RAM memory instead?
Code:
'record the query result in a temp table (tblQry, "TmpPtntLgng_tbl") for troubleshooting purposes
Set flds = tblCnf.Fields
Cnt = flds.Count - 1
Index = 0
Do While Not (tblCnf.EOF Or tblCnf.BOF)
tblQry.AddNew
For Index = 0 To Cnt
tblQry.Fields(Index).Value = tblCnf.Fields(Index).Value
Next Index
tblQry.Update
tblCnf.MoveNext
If tblCnf.EOF Then Exit Do
Loop
tblCnf.MoveFirst
This was the original recordset creation string:
Code:
Set tblCnf = DB.OpenRecordset(SQLTmp, dbOpenSnapshot)
Code:
Set tblCnf = DB.OpenRecordset(SQLTmp, dbOpenDynaset, dbReadOnly)
First I tried using a simple SQL string:
Code:
SELECT * FROM PtntLogging_tbl WHERE (Not ZeroLength(PtntLogging_tbl.PtntLN_FN_MI) And VarType(MR_No) > 1) ORDER BY PtntLogging_tbl.NmCnfdnc DESC, PtntLogging_tbl.MRCnfdnc DESC;
Code:
SELECT PtntLogging_tbl.ID_Key, PtntLogging_tbl.ID, PtntLogging_tbl.MasterPtNameRcrdStatus, PtntLogging_tbl.MasterPtNameRcrdIndx, PtntLogging_tbl.MstrNmCnf, PtntLogging_tbl.MR_No, PtntLogging_tbl.MR_txt, PtntLogging_tbl.MasterMRRcrdStatus, PtntLogging_tbl.MasterMRRcrdIndx, PtntLogging_tbl.MstrMRCnf, PtntLogging_tbl.MRMtchIndx, PtntLogging_tbl.PtntNameInpt, PtntLogging_tbl.PtntLN_FN_MI, PtntLogging_tbl.PtLstName, PtntLogging_tbl.PtFrstName, PtntLogging_tbl.PtMidSfxName, PtntLogging_tbl.PtNmMtchIndx, PtntLogging_tbl.NmCnfdnc, PtntLogging_tbl.MRCnfdnc, PtntLogging_tbl.CntExctPtRcrdMtch, PtntLogging_tbl.CntPtntNameMtch, PtntLogging_tbl.CountMR_LN_FN, PtntLogging_tbl.CntExctNameMtch, PtntLogging_tbl.ErrorFlg, PtntLogging_tbl.MRErrorFlg, PtntLogging_tbl.NmErrorFlg, PtntLogging_tbl.InvrtdPtNames, PtntLogging_tbl.NmInvrsnCnfdnc, PtntLogging_tbl.InvrtdPtNamIndx, PtntLogging_tbl.RcmndPtntLN_FN_MI, PtntLogging_tbl.RcmndPtLstName, PtntLogging_tbl.RcmndPtFrstName, PtntLogging_tbl.RcmndPtMidSfxName
FROM PtntLogging_tbl
WHERE (((ZeroLength([PtntLogging_tbl].[PtntLN_FN_MI]))=False) AND ((VarType([MR_No]))>1))
ORDER BY PtntLogging_tbl.NmCnfdnc DESC, PtntLogging_tbl.MRCnfdnc DESC;
Now I know there is a way to get around this (run an Access append query without any necessary parameters from vba), but I really want to know what I am doing wrong, is there a way to do this only in a recordset in RAM memory instead?