Descending loop (1 Viewer)

JPaulo

Developer
Local time
Today, 16:02
Joined
Dec 21, 2009
Messages
185
Hi all;

I want to update from the bottom up (descending), but this loop is doing from top to bottom, as reverse ?


Code:
Private Sub PreçoUnitário_Exit(Cancel As Integer)
DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70
On Error Resume Next
    Dim strUP As String
    Dim db As DAO.Database
    Dim rst As DAO.Recordset
Dim i As Integer
    Set rst = CurrentDb.OpenRecordset("Select * from DetalhesDoPedido Where NúmeroDoPedido=" & Me.NúmeroDoPedido)
    strUP = Nz(DMax("[PreçoUnitário]", "DetalhesDoPedido", "[NúmeroDoPedido]=" & Me.NúmeroDoPedido & ""), 0)
           
    
        Do While Not rst.EOF
                If rst![CódigoDaCategoria] = 1 Then
                rst.Edit
                rst![Preço_Praticado] = strUP
                rst.Update
            ElseIf rst![CódigoDaCategoria] = 8 Then
                Exit Do
                
            End If
            rst.MoveNext
        Loop
  rst.Close
  On Error Resume Next
    DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70
  DoCmd.GoToRecord , , acNewRec
  DoCmd.GoToControl "NúmeroDoPedido"
End Sub
 

vbaInet

AWF VIP
Local time
Today, 16:02
Joined
Jan 22, 2010
Messages
26,374
Hey Mr Paulo hehe! Set the sort order of your query to DESC using ORDER BY.
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 09:02
Joined
Aug 30, 2003
Messages
36,118
Try adding an ORDER BY clause to your SQL statement.
 

JPaulo

Developer
Local time
Today, 16:02
Joined
Dec 21, 2009
Messages
185
Thanks for replying, as I put the ORDER BY DESC in cod ?
 

JPaulo

Developer
Local time
Today, 16:02
Joined
Dec 21, 2009
Messages
185
Sorry for my head, now works a charme.

Very thanks friends

Code:
Private Sub PreçoUnitário_Exit(Cancel As Integer)
DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70
On Error Resume Next
    Dim strUP As String
    Dim db As DAO.Database
    Dim rst As DAO.Recordset
Dim i As Integer
    Set rst = CurrentDb.OpenRecordset("Select * from DetalhesDoPedido Where NúmeroDoPedido=" & Me.NúmeroDoPedido & " ORDER BY ID DESC")
    strUP = Nz(DMax("[PreçoUnitário]", "DetalhesDoPedido", "[NúmeroDoPedido]=" & Me.NúmeroDoPedido & ""), 0)
           
    
              Do While Not rst.EOF
                If rst![CódigoDaCategoria] = 1 Then
                rst.Edit
                rst![Preço_Praticado] = strUP
                rst.Update
            ElseIf rst![CódigoDaCategoria] = 8 Then
                Exit Do
                
            End If
            rst.MoveNext
        Loop
  rst.Close
  On Error Resume Next
    DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70
  DoCmd.GoToRecord , , acNewRec
  DoCmd.GoToControl "NúmeroDoPedido"
End Sub
 

vbaInet

AWF VIP
Local time
Today, 16:02
Joined
Jan 22, 2010
Messages
26,374
You include it somewhere in that line:

Code:
Set rst = CurrentDb.OpenRecordset("Select * from DetalhesDoPedido Where NúmeroDoPedido=" & Me.NúmeroDoPedido)

You have to order by something. Look it up.
 

Users who are viewing this thread

Top Bottom