¿Cómo recorrer un conjunto de registros dos veces?

I am trying to iterate through a record-set twice. Once to write all of the non-zero results, followed by a second run through to write all the rows that have a zero in a particular column so that all of those rows with the value of 0 are at the end of the file. However since .EOF has been triggered with the first run through it is still "True" when I try to run through it again. What is the best way to run through it twice?

   With CurrentDb.OpenRecordset(strSQL, dbOpenSnapshot)

        Do Until .EOF
             If .Fields(2) = 0 Then
             Else

                strCSV = ""
                 For x = 0 To .Fields.Count - 1
                       'No Qualifier
                       strCSV = strCSV & strDelimiter & Nz(.Fields(x), vbNullString)

                 Next x

                 'Eliminate Back to back strQualifiers or Qualifiers if changed
                 strCSV = Replace(strCSV, strQualifier & strQualifier, "")

                 strPrint = Mid(strCSV, Len(strDelimiter) + 1)
                 Print #intOpenFile, strPrint
            End If
                 .MoveNext
          Loop

          Do Until .EOF
             If Nz(.Fields(2), vbNullString) = 0 Then

                strCSV = ""
                 For x = 0 To .Fields.Count - 1
                       'No Qualifier
                       strCSV = strCSV & strDelimiter & Nz(.Fields(x), vbNullString)

                 Next x

                 'Eliminate Back to back strQualifiers or Qualifiers if changed
                 strCSV = Replace(strCSV, strQualifier & strQualifier, "")

                 strPrint = Mid(strCSV, Len(strDelimiter) + 1)
                 Print #intOpenFile, strPrint
            End If
                 .MoveNext
          Loop

   End With

preguntado el 30 de enero de 12 a las 20:01

3 Respuestas

To answer your question, just used .MoveFirst in between your two runs. But @Remou makes a good point that your can avoid this complication just by adding an ORDER BY into your SQL (+1)

Respondido el 31 de enero de 12 a las 00:01

That worked thank you. And no, an order by wouldn't help in my case. The table itself is created using an order by asc statement but I need the values to be asc with the zeros at the bottom. And I don't know of a way to do that. FYI it needs to be in the order I just explained for it to layer properly in the software it is being displayed in. Anyways, this works great for what I'm doing thanks. (Regardless of how odd what I'm doing is lol). - Bryan

Very old question but appears to have been active recently, and it's still coming up in searches, which is how I ended up here. And, as a new member, I need to start somewhere, so be kind.

The OP appears to be trying to code round a particular requirement, which is to have all the results in ascending numeric order, but with the zero items at the bottom. You can do this in SQL in one statement like this:

SELECT * FROM MyTable
ORDER BY MyNumField DESC, MyOtherSortField
WHERE MyNumField > 0

UNION ALL

SELECT * FROM MyTable
ORDER BY MyOtherSortField
WHERE MyNumField = 0

A UNION merges the results of two or more SELECT statements, removing duplicates. UNION ALL concatenates the results, and is more efficient, as it stops SQL removing duplicates between the two lists - in this case, we know they won't overlap due to the selection criteria being mutually exclusive.

respondido 13 nov., 20:13

You seem to be missing the point of a relational database, which is that it has no order other than the one you impose:

sSQL = "SELECT * FROM MyTable ORDER BY MyNumField DESC"
Set QDF = CurrentDB.CreateQueryDef ("DatOut", sSQL)

DoCmd.TransferText acExportDelim,,"DatOut","C:\Docs\Datout.csv"

Respondido el 31 de enero de 12 a las 00:01

I may be wrong but I don't think I'm missing the point of a relational database, it is simply that the reason I need to do this wasn't included in my question. Essentially I need the rows in the resulting file to be ordered asc starting at 1 and going up and all of the zeros at the end of the file. It has to be that way to layer properly in the program that the file I'm creating gets put into. And unfortunately I can't use the DoCmd.TransferText because because it truncates my decimals down 2 instead of 6+ like it is in the database. - Bryan

In case you want to see an older question I had ask when I was trying to make the DoCmd.TransferText command work: stackoverflow.com/questions/6154812/… - Bryan

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.