PC Review


Reply
Thread Tools Rate Thread

"Bookending" wildcards in a Where Clause

 
 
=?Utf-8?B?bW9kbzg=?=
Guest
Posts: n/a
 
      21st Jun 2005
I've found posts that describe this exact situation, but I can't get the
solutions to work. I know I'm missing something really simple, but I've
spent more than an hour on it and need to move on.

On a lookup form, the user can type in all or part of a Client's Name into
the text box [txtClientName], hit submit, and have the results populate a
list box. The submit button uses VBA coding to build the listbox's
rowsource. Here's my WHERE Clause:

sWhere = "WHERE [ClientName] LIKE '*" & [txtClientName] & "*'"

I only get results that start with whatever is in [txtClientName], but I
want results that have it anywhere within the name. To illustrate:

If [txtClientName] = "Corp" then

I want results of:
"Corporate Tax Attorney"
"Hello Corporation"

but I'm only getting:
"Corporate Tax Attorney"

Any assistance is greatly appreciated.
 
Reply With Quote
 
 
 
 
John Spencer (MVP)
Guest
Posts: n/a
 
      22nd Jun 2005
Don't see anything in your code fragment to cause the results you are getting.

Have you tried stopping your code at the where line and examining the variable
sWhere to make sure it is what you expect.

You might try the following, but it really should make no difference.

sWhere = "WHERE [ClientName] LIKE ""*" & [txtClientName] & "*"" "

modo8 wrote:
>
> I've found posts that describe this exact situation, but I can't get the
> solutions to work. I know I'm missing something really simple, but I've
> spent more than an hour on it and need to move on.
>
> On a lookup form, the user can type in all or part of a Client's Name into
> the text box [txtClientName], hit submit, and have the results populate a
> list box. The submit button uses VBA coding to build the listbox's
> rowsource. Here's my WHERE Clause:
>
> sWhere = "WHERE [ClientName] LIKE '*" & [txtClientName] & "*'"
>
> I only get results that start with whatever is in [txtClientName], but I
> want results that have it anywhere within the name. To illustrate:
>
> If [txtClientName] = "Corp" then
>
> I want results of:
> "Corporate Tax Attorney"
> "Hello Corporation"
>
> but I'm only getting:
> "Corporate Tax Attorney"
>
> Any assistance is greatly appreciated.

 
Reply With Quote
 
=?Utf-8?B?bW9kbzg=?=
Guest
Posts: n/a
 
      22nd Jun 2005
Yup, as I said, it was something simple I was missing. Your post was
actually quite helpful, because when you didn't see a problem with that one
line, I focused more on the rest of the code. The solution came within 30
seconds.

Below is my full WHERE Clause section. The problem is that even when
[txtClientID] was left blank by the user, IsNull returned a true value. As a
result, VBA was skipping the line of code that I was trying to fix.
Hopefully seeing this story will save someone out there some frustration.
John, thanks for your time and assistance.

sSelect = "SELECT [ClientID], [ClientName], [ClientSegment] "
sFrom = "FROM [tblClientInfo] "
sOrder = "ORDER BY [ClientName]"
sWhere = ""

If IsNull(txtClientID) = False Then
sWhere = "WHERE [ClientID] LIKE """ & [txtClientID] & "*"" "
End If
If IsNull(txtClientName) = False Then
If sWhere = "" Then
sWhere = "WHERE [ClientName] LIKE ""*" & [txtClientName] & "*"" "
Else
sWhere = sWhere & "AND [ClientName] LIKE ""*" & [txtClientName]
& "*"" "
End If
End If

lstClient.RowSource = sSelect & sFrom & sWhere & sOrder


"John Spencer (MVP)" wrote:

> Don't see anything in your code fragment to cause the results you are getting.
>
> Have you tried stopping your code at the where line and examining the variable
> sWhere to make sure it is what you expect.
>
> You might try the following, but it really should make no difference.
>
> sWhere = "WHERE [ClientName] LIKE ""*" & [txtClientName] & "*"" "
>
> modo8 wrote:
> >
> > I've found posts that describe this exact situation, but I can't get the
> > solutions to work. I know I'm missing something really simple, but I've
> > spent more than an hour on it and need to move on.
> >
> > On a lookup form, the user can type in all or part of a Client's Name into
> > the text box [txtClientName], hit submit, and have the results populate a
> > list box. The submit button uses VBA coding to build the listbox's
> > rowsource. Here's my WHERE Clause:
> >
> > sWhere = "WHERE [ClientName] LIKE '*" & [txtClientName] & "*'"
> >
> > I only get results that start with whatever is in [txtClientName], but I
> > want results that have it anywhere within the name. To illustrate:
> >
> > If [txtClientName] = "Corp" then
> >
> > I want results of:
> > "Corporate Tax Attorney"
> > "Hello Corporation"
> >
> > but I'm only getting:
> > "Corporate Tax Attorney"
> >
> > Any assistance is greatly appreciated.

>

 
Reply With Quote
 
=?Utf-8?B?bW9kbzg=?=
Guest
Posts: n/a
 
      22nd Jun 2005
sorry, I mean IsNull returned a "False" value

"modo8" wrote:

> Yup, as I said, it was something simple I was missing. Your post was
> actually quite helpful, because when you didn't see a problem with that one
> line, I focused more on the rest of the code. The solution came within 30
> seconds.
>
> Below is my full WHERE Clause section. The problem is that even when
> [txtClientID] was left blank by the user, IsNull returned a true value. As a
> result, VBA was skipping the line of code that I was trying to fix.
> Hopefully seeing this story will save someone out there some frustration.
> John, thanks for your time and assistance.
>
> sSelect = "SELECT [ClientID], [ClientName], [ClientSegment] "
> sFrom = "FROM [tblClientInfo] "
> sOrder = "ORDER BY [ClientName]"
> sWhere = ""
>
> If IsNull(txtClientID) = False Then
> sWhere = "WHERE [ClientID] LIKE """ & [txtClientID] & "*"" "
> End If
> If IsNull(txtClientName) = False Then
> If sWhere = "" Then
> sWhere = "WHERE [ClientName] LIKE ""*" & [txtClientName] & "*"" "
> Else
> sWhere = sWhere & "AND [ClientName] LIKE ""*" & [txtClientName]
> & "*"" "
> End If
> End If
>
> lstClient.RowSource = sSelect & sFrom & sWhere & sOrder
>
>
> "John Spencer (MVP)" wrote:
>
> > Don't see anything in your code fragment to cause the results you are getting.
> >
> > Have you tried stopping your code at the where line and examining the variable
> > sWhere to make sure it is what you expect.
> >
> > You might try the following, but it really should make no difference.
> >
> > sWhere = "WHERE [ClientName] LIKE ""*" & [txtClientName] & "*"" "
> >
> > modo8 wrote:
> > >
> > > I've found posts that describe this exact situation, but I can't get the
> > > solutions to work. I know I'm missing something really simple, but I've
> > > spent more than an hour on it and need to move on.
> > >
> > > On a lookup form, the user can type in all or part of a Client's Name into
> > > the text box [txtClientName], hit submit, and have the results populate a
> > > list box. The submit button uses VBA coding to build the listbox's
> > > rowsource. Here's my WHERE Clause:
> > >
> > > sWhere = "WHERE [ClientName] LIKE '*" & [txtClientName] & "*'"
> > >
> > > I only get results that start with whatever is in [txtClientName], but I
> > > want results that have it anywhere within the name. To illustrate:
> > >
> > > If [txtClientName] = "Corp" then
> > >
> > > I want results of:
> > > "Corporate Tax Attorney"
> > > "Hello Corporation"
> > >
> > > but I'm only getting:
> > > "Corporate Tax Attorney"
> > >
> > > Any assistance is greatly appreciated.

> >

 
Reply With Quote
 
james.igoe@gmail.com
Guest
Posts: n/a
 
      23rd Jun 2005
just a minor suggestion, but I find it helpful to use ascii charaters
for quotation marks:

sWhere = "WHERE [ClientName] LIKE " & chr(34) & "*" & [txtClientName] &
"*" & chr(34)

.....easier to read and modify

 
Reply With Quote
 
Tim Ferguson
Guest
Posts: n/a
 
      23rd Jun 2005
"(E-Mail Removed)" <(E-Mail Removed)> wrote in
news:(E-Mail Removed):

> sWhere = "WHERE [ClientName] LIKE " & chr(34) & "*" & [txtClientName] &
> "*" & chr(34)
>
> ....easier to read and modify
>
>


FWIW, it's even easier to read and modify with a function:

"... WHERE ClientName LIKE " & SQLQuote("*" & txtClientName & "*")

because you can then "lose" all the stuff about embedded quote marks and
NULLs at the same time.

Even better, create a Class to handle the whole putting a SQL string
together, creating a connection and returning the recordset:

Set sqlTemp = New cSQLCommand
With sqlTemp
.Add "INSERT INTO Referrals (SerialNum, ReferDt)"
.Add "VALUES (" & txtSerialNum & ", "
.Add " " & .QuoteDate(CDate(strNewDate))
.Add " )"

' MsgBox .SQL

If .Execute <> 0 Then
MsgBox "Update not completed"
End If

End With


Hope that helps


Tim F

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
search for text "beginning with" "containing" wildcards David H Microsoft Excel Worksheet Functions 1 13th Jan 2010 01:24 PM
Excel custom autofilter- how to find wildcard characters but not as wildcards (e.g. "?") in a cell Keith Microsoft Excel Misc 2 22nd Dec 2006 02:27 PM
The search "Find Next" with "wildcards" does not function properly =?Utf-8?B?UGhpbG9z?= Microsoft Word Document Management 4 10th Sep 2006 06:43 AM
"rename" with wildcards in cmd.exe under XP doesn't work correctly =?Utf-8?B?TkFTQWVuZ3I=?= Windows XP General 7 9th Jun 2005 03:22 AM
Re: "AND" Clause in Filter Makes Unbound Text Boxes "Invisible" Duane Hookom Microsoft Access Reports 1 10th Sep 2004 09:52 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:22 AM.