Results 1 to 8 of 8
  1. #1
    sparker75 is offline Competent Performer
    Windows XP Access 2010 32bit
    Join Date
    Jul 2018
    Posts
    107

    Toggle Button to Sort Records

    Hello,

    Can someone help me with the following VBA? I'm trying to toggle a column ascending or descending when the toggle is clicked. I would like to add this to each of my tabular column form so users and sort as they wish.

    This is what I have so far.... My Column that I'm trying to sort is OrderNum

    If Toggle102.Value = True Then


    DoCmd.SetOrderBy OrderNum, DESC
    Else
    DoCmd.SetOrderBy OrderNum, ASC
    End If
    End Sub

    Thank you for your help!

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    DoCmd.SetOrderBy "DESC", "OrderNum"
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    sparker75 is offline Competent Performer
    Windows XP Access 2010 32bit
    Join Date
    Jul 2018
    Posts
    107
    After this

    Private Sub Toggle102_AfterUpdate()
    If Toggle102.Value = True Then
    DoCmd.SetOrderBy "DESC", ([qryOrderStatus]![OrderNum])

    Else
    DoCmd.SetOrderBy "ASC", ([qryOrderStatus]![OrderNum])
    End If
    End Sub


    I get this error

    Click image for larger version. 

Name:	Capture.JPG 
Views:	14 
Size:	25.5 KB 
ID:	34887

  4. #4
    sparker75 is offline Competent Performer
    Windows XP Access 2010 32bit
    Join Date
    Jul 2018
    Posts
    107
    This is what I got from the code you provided so I added the query to it
    Click image for larger version. 

Name:	Capture.JPG 
Views:	13 
Size:	22.7 KB 
ID:	34888

  5. #5
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,977
    Your code is wrong. It was almost correct originally.
    See https://ss64.com/access/setorderby.html

    Try this

    Code:
    Private Sub Toggle102_AfterUpdate()
        If Toggle102.Value = True Then
    DoCmd.SetOrderBy "OrderNum DESC"
    Else
    DoCmd.SetOrderBy "OrderNum ASC"
    End If End Sub
    You could omit the ASC as it's the default
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

  6. #6
    sparker75 is offline Competent Performer
    Windows XP Access 2010 32bit
    Join Date
    Jul 2018
    Posts
    107
    Works! But I would like to sort ASC when the toggle is False. Is this possible?

  7. #7
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,977
    That should already happen in the Else section with the code I provided
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

  8. #8
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Oops, I misunderstood function arguments. Got this one-liner to work:

    DoCmd.SetOrderBy "OrderNum " & IIf(Me.Toggle102, "DESC", "ASC")
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

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

Similar Threads

  1. Toggle Button
    By gheyman in forum Access
    Replies: 2
    Last Post: 07-11-2017, 07:43 AM
  2. Combobox - Toggle sort between columns
    By kagoodwin13 in forum Forms
    Replies: 1
    Last Post: 04-15-2016, 04:51 PM
  3. Replies: 3
    Last Post: 05-07-2014, 09:16 AM
  4. Toggle Button Help
    By dbalilti in forum Access
    Replies: 1
    Last Post: 07-05-2012, 04:23 AM
  5. Toggle Button Options
    By Matthieu in forum Forms
    Replies: 2
    Last Post: 11-23-2009, 04:05 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