Results 1 to 3 of 3
  1. #1
    EGTax is offline Novice
    Windows 10 Access 2010 64bit
    Join Date
    Sep 2017
    Posts
    4

    Adding in sort options into doCmd.OpenReport

    Hello fellow techies,

    I am new to the Access genre and having a bit of trouble trying to figure out how to add in query options such as "Order BY" and "Asc" or "Desc" into the doCmd.OpenReport

    here is my sub:

    Code:
    Private Sub ReportButton_Click()
    Dim sWhere As String
    Dim Order As String
    Dim vars As String
    
    
    If Me.Direction = "Ascending" Then
        Order = " Asc"
    Else
        Order = " Desc"
    End If
    
    vars = "ORDER BY " & Me.SortBy & Order
    
    sWhere = "1=1"
    If Not Me.MyCat = "All" Then sWhere = sWhere & " and [Category]='" & Me.MyCat & "'"
    If Not Me.CBoxOfficeNumber = "All" Then sWhere = sWhere & " and [Office_Number]='" & Me.CBoxOfficeNumber & "'"
    
    If sWhere = "1=1" Then
      DoCmd.OpenReport "EGTaxInventory", acViewPreview
    Else
      DoCmd.OpenReport "EGTaxInventory", acViewPreview, , sWhere, , vars
    End If
    End Sub
    On here I have it in the OpenArgs section of the command, but that doesnt seem to work? although it doesn't error..
    I tried to just add it to the end of the sWhere variable but I got a "Missing Operator" error

    any help is appreciated

    Bob

  2. #2
    EGTax is offline Novice
    Windows 10 Access 2010 64bit
    Join Date
    Sep 2017
    Posts
    4
    ok, so after some more searching I found a work around...

    I went into the report and in the "Open" action I then called the Order By and "Asc/Desc"

    Code:
    Private Sub Report_Open(Cancel As Integer)
    Dim direct As String
    Dim Order As String
    
    direct = [Forms]![Inventory Search By ComboBox]![Direction]
    
    If direct = "Ascending" Then
        Order = " Asc"
    Else
        Order = " Desc"
    End If
    
    Me.OrderBy = [Forms]![Inventory Search By ComboBox]![SortBy] & Order
    Me.OrderByOn = True
    End Sub
    But please, if you know of a better way, I am always open to better solutions.

    Thank you,

    Bob

  3. #3
    orange's Avatar
    orange is online now Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,722
    With Access, reports have their own Sorting and Grouping which should be used.

    Google using ms access reports sorting and grouping for more info and examples.

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

Similar Threads

  1. DoCmd.OpenReport Problem
    By lowesthertz in forum Modules
    Replies: 13
    Last Post: 03-17-2015, 01:17 PM
  2. Replies: 6
    Last Post: 09-12-2013, 05:38 PM
  3. DoCmd.OpenReport WHERE condition
    By bidbud68 in forum Programming
    Replies: 16
    Last Post: 10-19-2012, 05:31 AM
  4. Need help w/ docmd.openreport
    By jwill in forum Reports
    Replies: 3
    Last Post: 06-04-2012, 09:49 PM
  5. Can I pass "sort by" using DoCmd.OpenReport
    By alsoto in forum Reports
    Replies: 3
    Last Post: 04-16-2009, 08:11 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