Results 1 to 11 of 11
  1. #1
    tagteam is offline Expert
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2013
    Posts
    501

    Move to next record with where condition

    I have a form where the user selects and invoice number from a combo box. The combo box is filtered to show them only the records that have not been archived (using a date field).



    The user wants a button to go to the next record. That seems pretty easy, even with the built in query wizard BUT i need it to go to the next record that has not ben archived.

    For example. They may be on an invoice with an InvoiceID of 3695 and when they hit next they would need to go to the next record id where the archive date in that record is blank, so move to record 4178 instead of 3697.

    I am sure there is a way to do this with some kind of query to get a value for the next available record but I am not sure. I looked at a few options like DoCmd.GoToRecord but not sure if i can do that since the form is based on a query not the table.

    If someone can just point me in the right direction I can try to figure it out but not sure where to start.
    Thanks

  2. #2
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,556
    You appear to be asking pretty much the same as this thread, except he is using a listbox.
    https://www.accessforums.net/showthread.php?t=87242
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  3. #3
    tagteam is offline Expert
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2013
    Posts
    501
    yes, I tried that code and there must be something different about a list box and a combo box because that does not seem to work. I don't get any errors or anything, it just doesnt do anything

  4. #4
    tagteam is offline Expert
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2013
    Posts
    501
    So i searched a bit and found some code that should move to next item in combo box

    Code:
    Dim IntRow As Integer
    IntRow = Me!cbMoveTo.ListIndex
    If IntRow <> -1 Then
          If IntRow = Me!cbMoveTo.ListCount - 1 Then
             Me!cbMoveTo.Value = Me!cbMoveTo.Column(0, IntRow - 1)
          Else
             Me!cbMoveTo.Value = Me!cbMoveTo.Column(0, IntRow + 1)
          End If
       End If
    However, all that does is clear out the option in the combo box and doesnt move to any other records.

  5. #5
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,556
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  6. #6
    tagteam is offline Expert
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2013
    Posts
    501
    I have this code working a little

    Code:
    cbMoveTo.SetFocusIf cbMoveTo.ListIndex <> cbMoveTo.ListCount - 1 Then
          cbMoveTo.ListIndex = cbMoveTo.ListIndex + 1
       Else
          cbMoveTo.ListIndex = 0
    Call cbMoveTo_AfterUpdate
    End If
    It works to move the record, however, after it does I get the error: run-time error 7777 - You've used the ListIndex property incorrectly

    i have tried a couple variations on this code and they all give me that 7777 error apparently because in access the ListIndex is a readonly value
    Last edited by tagteam; 12-16-2022 at 04:31 PM. Reason: new info

  7. #7
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,250
    How do you define "next"? Meaning how is your combo's row source sorted? Use that (the row source query of the combo) to get you the next invoice number. You can use a domain aggregate (dLookup,dMin, etc.) or a Top 1 query sorted on a unique ID that identifies the records (using greater that the current one).
    To go to that record you can use the form\recordset bookmark method (https://learn.microsoft.com/en-us/of....form.bookmark) or easier the
    Docmd.FindRecord (make sure you set the focus first on the control holding the value you're looking for) -https://learn.microsoft.com/en-us/office/vba/api/access.docmd.findrecord

    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  8. #8
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,423
    I'm not understanding why the form isn't just loaded with records that are not archived and you just use the built in navigation controls to move amongst the records - thus no or very little code.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  9. #9
    CarlettoFed is offline Competent Performer
    Windows 7 64bit Access 2013 32bit
    Join Date
    Dec 2019
    Posts
    274
    Code:
    Private Sub cmdMoveTo_Click()
        Me.cboMoveTo = Me.cboMoveTo.ItemData(Me.cboMoveTo.ListIndex + 1)
    End Sub

  10. #10
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,250
    You would still need to call the AfterUpdate event of the combo as it will not fire if updated programmatically.

    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  11. #11
    tagteam is offline Expert
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2013
    Posts
    501
    the filter Idea might work. I need to rework some of the form and subform a bit and will give it a try

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

Similar Threads

  1. Move to Record
    By qvqv in forum Forms
    Replies: 2
    Last Post: 01-30-2021, 10:05 AM
  2. New record condition
    By rosscortb in forum Access
    Replies: 3
    Last Post: 07-06-2015, 07:19 AM
  3. Replies: 3
    Last Post: 02-06-2015, 01:18 PM
  4. Replies: 3
    Last Post: 12-11-2014, 11:26 AM
  5. Replies: 5
    Last Post: 06-16-2013, 05:25 PM

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