Results 1 to 6 of 6
  1. #1
    epb is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2012
    Location
    Near Chicago
    Posts
    36

    Loop through records not moving to the next row??

    I believe I need to change the index of a table to index on three fields. Those three fields are [POPart], [PONum], and [POLine]. The table is [CustOrdersTable1].



    The [POLine] field is not populated for the vast majority of records. I wanted to create VBA code to loop through all records sorted by the three fields above and in the order above.

    I did create the code but as I go row by row in the debugger the recordset abosoluteposition is incrementing but the [POPart], [PONum] and [POLine] are not changing their values as the absolute position changes. I am sure there is something simple I am missing.

    Maybe there is a better way to do this but I would still like to know why the rst.absolutepostion increments but the column data does not.

    Here is the code:
    Code:
    Private Sub Command271_Click()
    On Error GoTo Err_Command271_Click
    
    Dim rst As DAO.Recordset
    Dim PONumCount As Long
    Dim CurrentPart As String
    Dim CurrentPONum As String
    
    Set rst = CurrentDb.OpenRecordset("CustOrdersPKFix qry")
    
    Me.AllowEdits = True
    With rst
        If Not (rst.EOF Or rst.BOF) Then
        rst.MoveFirst
        CurrentPart = Me.POPart
        CurrentPONum = Me.PONUMB
        PONumCount = 1
            Do Until rst.EOF
                rst.Edit
                'Check the part number to see if it changed
                'If changed reset the PONumber and set the POline to 01
                If (CurrentPart <> Me.POPart) Then
                    CurrentPart = Me.POPart
                    CurrentPONum = Me.PONUMB
                    PONumCount = 1
                    If (Nz(Me.POLine, 0) = 0) Then
                        Me.POLine = PONumCount
                    Else
                        PONumCount = Me.POLine
                    End If
                Else
                    If (CurrentPONum <> Me.PONUMB) Then
                        CurrentPONum = Me.PONUMB
                        PONumCount = 1
                        If (Nz(Me.POLine, 0) = 0) Then
                            Me.POLine = PONumCount
                        Else
                            PONumCount = Me.POLine
                        End If
                    Else
                        If (Nz(Me.POLine, 0) = 0) Then
                            Me.POLine = PONumCount
                        Else
                             PONumCount = Me.POLine
                        End If
                        PONumCount = PONumCount + 1
                    End If
                End If
                rst.Update
                'Next Record
                rst.MoveNext
            'Next Loop
            Loop
            Me.AllowEdits = False
            rst.Close
        End If
        
    End With
    Set rst = Nothing
    
    Exit_Command271_Click:
    Me.AllowEdits = False
        Exit Sub
    
    Err_Command271_Click:
        MsgBox Err.Description
        Resume Exit_Command271_Click
        
    End Sub

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,640
    You aren't referring to the values from the recordset. One way is:

    rst!FieldName
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    jzwp11 is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2010
    Location
    Dayton, OH
    Posts
    2,901
    I do not see anywhere in your code where you reference the [POPart], [PONum] or [POLine] from the recordset.

    I see where you assign a form control value to a variable like these statements
    CurrentPart = Me.POPart
    CurrentPONum = Me.PONUMB

    I assume that you really meant to reference the recordset value not a form control. The proper syntax is: rst!POPart

    oops I see Paul beat me again.

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,640
    Quote Originally Posted by jzwp11 View Post
    oops I see Paul beat me again.
    Fastest fingers in the west!
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    epb is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2012
    Location
    Near Chicago
    Posts
    36
    Thank you Paul and JzwP11...

    It is working...You have broadened my horizons! I do appreciate it. I understand now.

  6. #6
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,640
    Happy to help!
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

Please reply to this thread with any new information or opinions.

Similar Threads

  1. VBA Loop to Combine Records
    By admessing in forum Queries
    Replies: 23
    Last Post: 03-06-2012, 11:37 AM
  2. Moving Records
    By Taximan in forum Access
    Replies: 5
    Last Post: 01-13-2012, 05:14 PM
  3. Loop through records
    By sam10 in forum Programming
    Replies: 12
    Last Post: 07-07-2011, 02:30 PM
  4. Loop through subform records
    By asmith in forum Forms
    Replies: 6
    Last Post: 10-06-2010, 10:31 AM
  5. Moving records up
    By saqqer in forum Programming
    Replies: 0
    Last Post: 07-27-2009, 08:29 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums