Good point Duane. Sometimes I start down the path, and think I’m doing
things right so I just keep at it, even though it’s getting pretty
difficult.
I guess if things are getting too difficult, you gotta step back and try
to
understand if there is a better way to do what you need to do.
Anyway, I’m taking dates (from a ComboBox on a form) and a stock symbol
(from a ComboBox on a form) as well as items in two ListBoxes. The
ListBoxes are messing me up a bit now; I have to loop through to find
(potentially) multiple items in a ListBox. Here is a small sample of the
looping code:
' Build criteria string for Trader
For Each varItem In Me.lstTrader.ItemsSelected
strTrader = strTrader & ",'" & Me.lstTrader.ItemData(varItem) & "'"
Next varItem
If Len(strTrader) = 0 Then
strTrader = "Like '*'"
Else
strTrader = Right(strTrader, Len(strTrader) - 1)
strTrader = "IN(" & strTrader & ")"
End If
Below is the SQL that I’m working with (not quite working just yet):
strSQL = "SELECT * " & vbCrLf & _
"FROM Trades " & vbCrLf & _
"WHERE (((Trades.TDATE) Between [Forms]![QueryForm]![cboFrom] And
[Forms]![QueryForm]![cboTo]) AND " & _
"((Trades.SYMBOL) Like Nz([Forms]![QueryForm]![CboSymbol],""*"")))" & _
"And Nz(Trades.[Cust],""*"") " & strCust & strTraderCondition &
"NZ(Trades.[Trader],""*"") " & strTrader & ");"
The dates and the symbols work fine. The cust and trader work, but if
there’s a null in the cust or trader record, these are ignored…I really
want
these included in the final results of the query. I’ve worked on this for
a
bit today. Tried to resolve myself, but just going in circles now. Any
thoughts, John or Duane?
Thanks so much!
Ryan---
--
Ryan---
If this information was helpful, please indicate this by clicking ''Yes''.
John W. Vinson said:
This returns all records. What I’d like to do, is filter by dates (this
works perfectly fine) and filter by Symbol (in cboSymbol; if user
chooses, I
want to apply this, but if left blank than I want to return all
records). I
want to filter by Customer and/or Trader.
I would suggest a different approach if that's what you want. As noted
elsethread, what you're doing now is going to have dreadful performance
on
large tables, if indeed it can be made to work at all.
Try instead:
SELECT *
FROM Trades
WHERE (((Trades.TDATE) Between [Forms]![QueryForm]![cboFrom] And
[Forms]![QueryForm]![cboTo])
AND ([Trades].[Symbol] = [Forms]![QueryForm]![cboSymbol] OR
[Forms]![QueryForm]![cboSymbol] IS NULL)
AND ([Trades].[Cust] =[Forms]![QueryForm]![cboCust] OR
[Forms]![QueryForm]![cboCust] IS NULL)
AND ([Trades].[Trader]=[Forms]![QueryForm]![cboTrader] OR
[Forms]![QueryForm]![cboTrader] IS NULL);
A WHERE clause in a query is a statement in Boolean algebra: if it
evaluates
to TRUE the record will be returned; if it evaluates to FALSE it won't.
It may
not be obvious, but a WHERE clause need not refer to a table field at
all! In
this case, I'm using the expression "OR [Forms]![QueryForm]![cboSymbol]
IS
NULL" in the logic - if the combo box is empty, that statement will be
TRUE,
and it will therefore ignore the other part of the OR, comparing the
field to
the combo.