 |
|

09-04-2004, 09:02 AM
|
 |
Junior Contributor
|
|
Join Date: Aug 2003
Location: Houston, Texas
Posts: 227
|
|
Wild Card in ADO recordset Filter
|
I want to filter an ADO Recordset using 5 fields.
I use the following filter (I only copy 2 here for clarity):
rsBottles.Filter = "Color = '" & vsColor & "' AND Country = '" & vsCountry & "'"
(I get vsColor, vsCountry, etc. from combo boxes on a user form.)
What do I need to put in vsCountry for instance so as to accept ALL Countries?
I tried:
vsCountry = "%" (and "*" and "_" but this does not work as a wildcard).
Thanks for your help.
Hervé Chain
Houston, Texas
|
|

09-04-2004, 10:35 AM
|
|
Steppe Walker
Retired Moderator * Expert *
|
|
Join Date: Jul 2002
Location: Ukraine/Russia/Belgium
Posts: 7,227
|
|
|
In ADO try use '%' instead of '*'
Regards,
Shurik.
|
__________________
"A diaper is not like a computer that makes satisfying burbling noises from time to time, hinting at great inner complexity." Malcolm Gladwell
"I'm sitting here completely surrounded by no beer." Onslow, 'Keeping up appearances'
|

09-04-2004, 10:55 AM
|
 |
Cum Grano Salis
Retired Moderator * Guru *
|
|
Join Date: Jul 2002
Location: Baltimore, Maryland
Posts: 14,636
|
|
Quote:
I tried:
vsCountry = "%" (and "*" and "_" but this does not work as a wildcard).
|
So you DID try to using the ADO wildcard "%" and it didn't work? Can you show us the exact filter string that you tried. Did you receive an error?
|
__________________
"Artificial Intelligence is no match for natural stupidity." ~unknown
|

09-04-2004, 11:28 AM
|
 |
Junior Contributor
|
|
Join Date: Aug 2003
Location: Houston, Texas
Posts: 227
|
|
|
Here is the pertinent code from the frmReports user form (only one combo for clarity) where I choose the filters:
---------------
Sub cbMaturityDisplay_Click()
If cbxCountry = "All Countries" Then
cbxCountry = "%"
End If
Call frmList.Maturity_View
End Sub
--------------
And the code for the Recordset filter:
--------------
Sub Maturity_View()
Dim vsColor As String, vsCountry As String
vsColor = frmReports.cbxColor
vsCountry = frmReports.cbxCountry
MsgBox (vsCountry)
rsBottles.Filter = "Status = 'Inventory' AND Color = '" & vsColor & "' AND Country = '" & vsCountry & "'"
--------------
Results:
If I select White in cbxColor and Chile in cbxCountry, the datagrid displays all the whites from Chile. Fine.
If I select White and All Countries, the datagrid is empty (no error), meaning (I think) that VB6 interprets % as a country, not as a wild card.
I have the same result is I use * or _ (I found these 3 in various posts).
Are my quotes, etc. incorrect?
Thanks!
Hervé
|
|

09-04-2004, 01:09 PM
|
|
Steppe Walker
Retired Moderator * Expert *
|
|
Join Date: Jul 2002
Location: Ukraine/Russia/Belgium
Posts: 7,227
|
|
Could you do the following:
Code:
Dim sFilter As String
...
sFilter= "Status = 'Inventory' AND Color = '" & vsColor & "' AND Country = '" & vsCountry & "'"
Debug.Print sFilter
Run the code, presss CTRL+G and copy the content of the immmediate window here.
Shurik.
|
__________________
"A diaper is not like a computer that makes satisfying burbling noises from time to time, hinting at great inner complexity." Malcolm Gladwell
"I'm sitting here completely surrounded by no beer." Onslow, 'Keeping up appearances'
|

09-04-2004, 01:28 PM
|
|
Retired Contributor
|
|
Join Date: Mar 2002
Posts: 1,829
|
|
*scratches head*
Help me to understand this... If you are you wanting to select your data for ALL countries, why are you using it as a condition in your query?
What is the difference in (Countries = '%') and Not having that in your query at all?
If you want to select records where the countries field is just not BLANK, then why not just test for that? Not Null and Not ''?
I'm just confused at using a wildcard with no other character like that...
|
|

09-04-2004, 01:46 PM
|
 |
Junior Contributor
|
|
Join Date: Aug 2003
Location: Houston, Texas
Posts: 227
|
|
|
Here it is Shurik:
Status = 'Inventory' AND Color = 'White' AND Country = '%'
Thanks for your time.
Hervé
|
|

09-04-2004, 01:53 PM
|
|
Steppe Walker
Retired Moderator * Expert *
|
|
Join Date: Jul 2002
Location: Ukraine/Russia/Belgium
Posts: 7,227
|
|
Quote:
|
Originally Posted by Agent707
If you are you wanting to select your data for ALL countries, why are you using it as a condition in your query?
|
Good point
Hervé, to return the records including all countries you don't need to include
"... Country = '%'" condition at all
>...that VB6 interprets % as a country, not as a wild card. "
exactly, for "Fra%" would return France, "Chi%"- Chile, but there's no country called %
Regards,
Shurik.
|
__________________
"A diaper is not like a computer that makes satisfying burbling noises from time to time, hinting at great inner complexity." Malcolm Gladwell
"I'm sitting here completely surrounded by no beer." Onslow, 'Keeping up appearances'
|

09-04-2004, 02:51 PM
|
 |
Junior Contributor
|
|
Join Date: Aug 2003
Location: Houston, Texas
Posts: 227
|
|
Maybe I am looking at the problem the wrong way...
I want to filter some bottles using 5 fields:
Color, Country, Region, Varietal, Vintage
However, I may or MAY NOT select one or more of these.
For instance, if I want ALL the Whites from Chile, I need NOT to filter on the other 3 fields (Region, Varietal, and Vintage).
So my rule is a combination of five AND'ed equal statements (Color = vsColor AND Country = vsCountry AND Region = vsRegion...)
I have to use a wild card for the one(s) I do not want to filter or I need to change my rule for EACH possible combination, which is not feasible.
In other word, I cannot delete the "AND Country = vsCountry" part so I have to find a way to make it irrelevant.
Is there another way to do this?
Thanks for your time!
Hervé
|
|

09-04-2004, 04:58 PM
|
|
Junior Contributor
|
|
Join Date: Aug 2003
Location: AlBayda, Libya
Posts: 300
|
|
this might help?? I hope...
Quote:
|
Originally Posted by hchain
Maybe I am looking at the problem the wrong way...
I want to filter some bottles using 5 fields:
Color, Country, Region, Varietal, Vintage
|
Hi
I think I know what are you trying to do correct me if i'm wrong, here is my approach
you want to filter some records from your db using 5 fileds that may or maybe not all present at the same time, so you have 32 cases,
example:
color, country
that means
1,1,0,0,0
color,region
1,0,1,0,0
so you can build an if then construct including all the cases
00000
10000
01000
11000
and so on
in vb it goes like this
if color="" and country="" and region=""........... then
first filter here
elseif color<>"" and country="" .....................then
second filter here
and so on untill you finish every one of them.
i hope this might help, now imagine doing this for 10 fields  do the math...belive it or not I did  Icouldn't find any other way, and i'm not telling any one that he can filter his database and find any thing that he wants...
good luck
BR
SGM
|
|

09-04-2004, 05:06 PM
|
|
Retired Contributor
|
|
Join Date: Mar 2002
Posts: 1,829
|
|
Edit: In response to post#9, not 10
Instead of having a query statement where you just plug in control values, you'd have to write a routine to build your query statement.
I can't speak for everyone, but that's my preferred way of doing things...
Instead of:
Code:
strSQL = "Select A, B, C, D From X Where Z = '" & somecontrol.value & "' " _
& "And R = '" & Someothercontrols.value & "'"
'just an example hardcoded query string
I do a function something like:
Code:
strSQL = GetSQL
Function GetSQL() As String
Dim i As Integer
Dim count As Integer
Dim str() As String 'Array used in building items to select and filter on
'Get an array for items to be "Selected"
ReDim str(UBound(controlarray))
'I usually use a control array of checkboxes for items to select. Adjust to your own controls.
'Control arrays are easier for me.
For i = LBound(controlarray) To UBound(controlarray)
If controlarray(i).Checked Then
str(count) = controlarray(i).Caption
count = count + 1
End If
Next
'Resize your array to match everything that was selected.
ReDim Preserve str(count - 1)
'Start your SQL query string
GetSQL = "Select " & Join(str, ",") & " From MyTable "
count = 0
'Now go through your filters
'In this, we'll use Drop down lists with the TOP of the list being BLANK meaning you DON'T want to filter on that item.
'So in other words, if you have a list of Countries to select from, instead of having ALL Countries, just have a blank value
'which means ALL Countries
If Colors.Text <> "" Then
str(count) = " Colors = '" & Colors.Text & "' "
count = count + 1
End If
If Countries.Text <> "" Then
str(count) = " Country = '" & Countries.Text & "' "
count = count + 1
End If
'....etc... Do an If for each filter
'Reisize the array again
ReDim Preserve str(count - 1)
If str(0) <> "" Then 'There is something to Filter on.
GetSQL = GetSQL & "Where" & str(0)
'See if there is more than just one item
If UBound(str) > 0 Then
For i = 1 To UBound(str)
GetSQL = GetSQL & " And " & str(i)
Next
End If
End If
'Do some Order By code here if you want to
If ComboOrderBy.Text <> "" Then
GetSQL = GetSQL & " Order By " & ComboOrderBy.Text
End If
End Function
I know it looks like a lot of coding just to get a query statement, but it gives you a Lot more flexability...
You'd have to experiment and see what works best for you.
In some cases, hard coded select statements are OK, but I try to avoid them when possible.
Good luck! 
|
Last edited by Agent707; 09-04-2004 at 05:14 PM.
|

09-04-2004, 10:47 PM
|
 |
Cum Grano Salis
Retired Moderator * Guru *
|
|
Join Date: Jul 2002
Location: Baltimore, Maryland
Posts: 14,636
|
|
|
There is no reason to write a complex function that can be handled with ONE sql statement. Hard-coded select statements are the industry standard. Stored Procedures and basic SQL is the main fundamental tool of all database developers. Absolutely use sql to update your table or select records. Query ARE the most flexible, powerful and easiest method of dealing with your table data.
|
__________________
"Artificial Intelligence is no match for natural stupidity." ~unknown
|

09-05-2004, 12:40 AM
|
|
Junior Contributor
|
|
Join Date: Jun 2003
Location: UAE
Posts: 204
|
|
I do it like this:
Code:
'1.Add a string variable at top of form code
dim strFilter as String
'2.Add a sub for making Filter string :
private sub MakeFilterString(strFilterPart as string,AddAND as boolean)
if jstrfilterpart<>"" then
strFilter=strFilter + strFilterPart endif if AddAND=True thenstrFilter=strFilter+" AND " endif end sub
'3.in LostFocus event or better event base on your code "
call MakeFilterString(cbxxxx.text,[true or failes])
'failes is for your last field to prevent adding " AND " to end your 'STRFilterString
and your string is ready .
In case of "ALL Fieldx" no part in string and it is flexible for diferent situatuions.
|
__________________
Be Happy
|

09-05-2004, 05:48 AM
|
 |
Junior Contributor
|
|
Join Date: Aug 2003
Location: Houston, Texas
Posts: 227
|
|
|
[QUOTE=MKoslof]There is no reason to write a complex function that can be handled with ONE sql statement.
What would this ONE statement be?
Thanks!
Hervé
|
|

09-05-2004, 07:15 AM
|
 |
Junior Contributor
|
|
Join Date: Aug 2003
Location: Houston, Texas
Posts: 227
|
|
Thanks to everyone for this brainstorming. I could not have done it without you
Here is the code I came up with. It works great!
In the user form:
-------------------------------
Sub cbMaturityDisplay_Click()
Dim SQLString As String, i As Integer
'Build a "raw" SQL string
If cbxColor <> "All Colors" Then
SQLString = SQLString & "Color = '" & cbxColor & "'" & " AND "
i = i + 1
End If
If cbxVarietal <> "All Varietals" Then
SQLString = SQLString & "Varietal = '" & cbxVarietal & "'" & " AND "
i = i + 1
End If
If cbxCountry <> "All Countries" Then
SQLString = SQLString & "Country = '" & cbxCountry & "'" & " AND "
i = i + 1
End If
If cbxRegion <> "All Regions" And cbxRegion <> "" Then
SQLString = SQLString & "Region = '" & cbxRegion & "'" & " AND "
i = i + 1
End If
If cbxAppelation <> "All Appelations" And cbxAppelation <> "" Then
SQLString = SQLString & "Appelation = '" & cbxAppelation & "'" & " AND "
i = i + 1
End If
'Finalise the SQL String
If i = 0 Then ' No selection
SQLString = "Status = 'Inventory' AND Color <> ''"
Else
SQLString = "Status = 'Inventory' AND " & Left(SQLString, Len(SQLString) - 5)
End If
Call frmList.Maturity_View(SQLString)
Unload Me
End Sub
--------------------------------
In the datagrid to display the filtered Recordset:
--------------------------------
Sub Maturity_View(SQLString)
.....
rsBottles.Filter = SQLString
......
--------------------------------
Let me know if this is not clear. Thanks again to all!
Hervé
|
|

09-05-2004, 09:00 AM
|
 |
Cum Grano Salis
Retired Moderator * Guru *
|
|
Join Date: Jul 2002
Location: Baltimore, Maryland
Posts: 14,636
|
|
|
Looks OK. You can probably clean up your logic here a little but you have the concept down. For one, you can specify your fields where field1 <> "some value" AND NOT NULL. Meaning if the field is not equal to some value and it is not null (not empty) include this in the query.
Good luck.
|
__________________
"Artificial Intelligence is no match for natural stupidity." ~unknown
|

09-05-2004, 10:59 AM
|
 |
Junior Contributor
|
|
Join Date: Aug 2003
Location: Houston, Texas
Posts: 227
|
|
Just learning here...
What's the difference between:
AND Field <> ''
and
AND NOT NULL?
It sure does look more professional  but is there some advantage (memory, readibility for sure, etc.)?
Hervé
|
|

09-05-2004, 12:45 PM
|
 |
Cum Grano Salis
Retired Moderator * Guru *
|
|
Join Date: Jul 2002
Location: Baltimore, Maryland
Posts: 14,636
|
|
|
It depends on how you have designed your tables. In general a field that has no value by default is null, unless you have declared default values. A numerical type would be 0 (depending on your database engine) etc. And if your tables are designed to have required fields, meaning they can not have a null value when entering a new record..you don't have to add this condition at all. Because if the field is <> "some value" that is the only condition you need to check.
|
__________________
"Artificial Intelligence is no match for natural stupidity." ~unknown
|

09-05-2004, 12:54 PM
|
|
Retired Contributor
|
|
Join Date: Mar 2002
Posts: 1,829
|
|
Quote:
|
Originally Posted by hchain
What's the difference between:
AND Field <> ''
and
AND NOT NULL?
|
Suppose the field WAS Null.
Null <> '', So this would be True, thus being in your returned recordset.
Typically, you check both conditions.
Field <> '' And Field Is Not Null.
MSSQL (can't speak for other databases) has an function that "IF" the field Is Null, it will convert it into whatever parameter you give it: IsNull(MyField,'')
This says, If MyField is Null, make it ''.
So you could do this
Where IsNull(MyField,'') <> ''
That convers you on both conditions.
|
|

09-05-2004, 01:04 PM
|
 |
Junior Contributor
|
|
Join Date: Aug 2003
Location: Houston, Texas
Posts: 227
|
|
|
Thanks for your time to give all these explanations.
It's a real pleasure to be a part of this forum.
Hervé
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
|