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
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]
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