Re: vba sort a query

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Well you can do it that way, but I don't see a good reason
to use a bunch of code when combo box features can be used
instead. What I was suggesting is that the combo box's
RowSource should be is either a table with the field
name,description or a value list with the same thing. E.g.
"vendor","Branch", "Year([date field])","Year", ...
with the other combo box properties set to:
ColumnCount 2
BoundColumn 1
ColumnWidths 0;
Then the code would just be what Bill and I posted earlier.
(Note that a Requery is not needed).
--
Marsh
MVP [MS Access]


ryan.fitzpatrick3@xxxxxxxxxxx wrote:

I have many comboboxes with different selections and I have this code
that i have on a 'filter' button so when I click the filter the
selections I choose pop up on the fields below. Now I have a new
"sortby" combobox that has 4 criteria that when you select a selection
and click the filter not only will it bring up what is in the
comboboxes but sort by what is selected in the sortby combobox. Isn't
there a code I can add to this vba below since it really revolves
around the filter button being clicked to do anything.

the choices in the sortby combobox are Branch, vendor, item and year

I was thinking I could do an if statement, if cboxsortby = "branch"
then sort the branch field or if cboxvendor = "vendor" then sort by
vendor. I hope this would be an easy addition. What do you think?
Thanks in advance.

Private Sub cmdFilter_Click()

Dim strdoc As String
strdoc = "qryMakeTableQuickOrder"
DoCmd.Requery

'Purpose: Build up the criteria string form the non-blank search
boxes, and apply to the form's Filter.
'Notes: 1. We tack " AND " on the end of each condition so you
can easily add more search boxes; _
we remove the trailing " AND " at the end.
' 2. The date range works like this: _
Both dates = only dates between (both
inclusive. _
Start date only = all dates from this one
onwards; _
End date only = all dates up to (and
including this one).
Dim strWhere As String 'The criteria string.
Dim lngLen As Long 'Length of the criteria
string to append to.
Const conJetDate = "\#mm\/dd\/yyyy\#" 'The format expected for
dates in a JET query string.



'***********************************************************************
'Look at each search box, and build up the criteria string from
the non-blank ones.

'***********************************************************************
'Text field example. Use quotes around the value in the string.
If Not IsNull(Me.cboxCompany) Then
strWhere = strWhere & "([gl_cmp_key] = """ & Me.cboxCompany &
""") AND "
End If

'Another text field example. Use Like to find anywhere in the
field.
If Not IsNull(Me.cboxBranch) Then
strWhere = strWhere & "([so_brnch_key] = """ & Me.cboxBranch &
""") AND "
End If

'Number field example. Do not add the extra quotes.
If Not IsNull(Me.cboxVendor) Then
strWhere = strWhere & "([en_vend_key] = """ & Me.cboxVendor &
""") AND "
End If

If Not IsNull(Me.cboxItem) Then
strWhere = strWhere & "([in_item_key] = """ & Me.cboxItem &
""") AND "
End If

If Not IsNull(Me.cboxBuyer) Then
strWhere = strWhere & "([en_phfmt_key] = """ & Me.cboxBuyer &
""") AND "
End If

If Not IsNull(Me.cboxCommodity) Then
strWhere = strWhere & "([in_comcd_key] = """ &
Me.cboxCommodity & """) AND "
End If

If Not IsNull(Me.cboxYear) Then
strWhere = strWhere & "([Rec Date] = " & Me.cboxYear & ") AND
"
End If

'Yes/No field and combo example. If combo is blank or contains
"ALL", we do nothing.
If Me.cboxIngPack = 2 Then
strWhere = strWhere & "([in_type_key] = " & Chr(34) & 2 & Chr
(34) & ") AND "
ElseIf Me.cboxIngPack = 5 Then
strWhere = strWhere & "([in_type_key] = " & Chr(34) & 5 & Chr
(34) & ") AND "
End If

If Not IsNull(Me.txtFilterVendName) Then
strWhere = strWhere & "([en_vend_name] Like ""*" &
Me.txtFilterVendName & "*"") AND "
End If

If Not IsNull(Me.txtFilterItemName) Then
strWhere = strWhere & "([in_desc] Like ""*" &
Me.txtFilterItemName & "*"") AND "
End If
'Date field example. Use the format string to add the # delimiters
and get the right international format.
'If Not IsNull(Me.txtStartDate) Then
' strWhere = strWhere & "([EnteredOn] >= " & Format
(Me.txtStartDate, conJetDate) & ") AND "
'End If

'Another date field example. Use "less than the next day" since
this field has times as well as dates.
'If Not IsNull(Me.txtEndDate) Then 'Less than the next day.
' strWhere = strWhere & "([EnteredOn] < " & Format
(Me.txtEndDate + 1, conJetDate) & ") AND "
'End If


'***********************************************************************
'Chop off the trailing " AND ", and use the string as the form's
Filter.

'***********************************************************************
'See if the string has more than 5 characters (a trailng " AND ")
to remove.
lngLen = Len(strWhere) - 5
If lngLen <= 0 Then 'Nah: there was nothing in the string.
MsgBox "No criteria", vbInformation, "Nothing to do."
Else 'Yep: there is something there, so remove
the " AND " at the end.
strWhere = Left$(strWhere, lngLen)
'For debugging, remove the leading quote on the next line.
Prints to Immediate Window (Ctrl+G).
Debug.Print strWhere

'Finally, apply the string as the form's Filter.
Me.Filter = strWhere
Me.FilterOn = True
End If
End Sub


On Mar 4, 3:39 pm, Marshall Barton <marshbar...@xxxxxxxxxx> wrote:
The combo box's BoundColumn must contain the name of the
field to be sorted.  The first visible column (specified in
the ColumnWidths property) can contain whatever description
you want.
--
Marsh
MVP [MS Access]

ryan.fitzpatri...@xxxxxxxxxxx wrote:
how would it know to sort by (let's say year) if year was selected in
the cbox? wouldnt it have to be linked, or some logic involved for the
form to know what to sort by?

On Mar 4, 2:34 pm, Marshall Barton wrote:
I have a simple query that lists the information onto a form. I have
information such as item, vendor, year etc. I would like to put a
combobox on that form that when I click the three options in that cbox
(item, vendor, year) the query will sort by that selection? Now would
I need vba code to sort the query or is there an easier way?

Actually, you can sort the form's records without messy with
the query.  Try setting the form's OrderBy property to the
name of the sort field:

        Me.OrderBy = Me.combobox
        Me.OrderByOn = True

If you want a button to revert back to the query's sort,
just use:
                 Me.OrderByOn = False

.



Relevant Pages

  • Re: mail merge selection criteria
    ... You're in the "Filter and Sort" ... Sort" doesn't show the filter I've set. ... Dim TableName As String ... So the select criteria is "pletter ...
    (microsoft.public.word.docmanagement)
  • Re: Is there a way to open a report in preview or report mode and have it be invisible?
    ... first sort is always enabled, the second sort only becomes enabled if ... Build a SQL string that can accomodate your recordsource, filter, ... Dim varCategory As Variant ...
    (comp.databases.ms-access)
  • "data type mismatch" trying to execute dialogue form
    ... I have a report with a form that I am trying to use to filter and then to ... then sort the report up to three levels. ... Dim strSection As String ...
    (microsoft.public.access.reports)
  • newline separated grep output
    ... I'm trying to filter a string so that I can sort it. ... that these strings appear after each other and I can't use cut/sort ...
    (comp.unix.shell)
  • Re: VB6 LISTBOX problem
    ... Have a look at the code below, can you do a sort that is faster ... There are of course all sorts of ways of sorting stuff, especially stuf that is "linked together" as in your UDTs, but to prove the following code sorts it in exactly the same way that your own original code does so, by concatenating all three items of each element into a composite string exactly as you are already doing, but instead of dumping those composite strings into a ListBox it dumps them into a VB string array. ... Private Type SAFEARRAY1D ... Dim StPtr As Long, VAs String, pVAs Long ...
    (microsoft.public.vb.general.discussion)