Using 'Like' with 'If' statement ??

D

David

Hi,

Quick question.

If I have a recordset value in RS("ProductName"), is it possible to
test for a part string in this value ?

I would be looking for all products starting 'Blue' and with misc
endings.
So I will require the search string Blue.*

Can I do something like:


If RS("ProductName") = like(Blue.*) then.....

or

can this only be done within the select statement ?


Thanks


David
 
R

Rob Meade

...
If I have a recordset value in RS("ProductName"), is it possible to
test for a part string in this value ?
yes

can this only be done within the select statement ?

If you want to do this within the ASP and not the select statement, you can
use InStr.

InStr will return a numeric value for the character location of the first
found match.

So -

strText = "blue.pants"

Location = InStr(strText, "blue")

In this case, InStr would return as 1 - the first character in the string
etc...

You would need to put this in the loop for your recordset.

Do While Not RS.BOF And Not RS.EOF

strText = RS("text")

Location = InStr(strText, "blue")

If InStr > 0 Then
' do something
End If

RS.MoveNext
Loop


You might also want to put LCase() around strText within the InStr line
above - then you can be sure it wont be case sensitive etc when matching...

To be honest, depending on your needs, it might be a lot quicker to just
change your SQL statement to return all the records you want where it
already contains "blue" in the relevant field etc...

Hope this helps,

Regards

Rob
 
B

Bob Barrows

David said:
Hi,

Quick question.

If I have a recordset value in RS("ProductName"), is it possible to
test for a part string in this value ?

I would be looking for all products starting 'Blue' and with misc
endings.
So I will require the search string Blue.*

Can I do something like:


If RS("ProductName") = like(Blue.*) then.....

or

can this only be done within the select statement ?


Thanks


David

In addition to Robb's suggestion (InStr), you can use the recordset's Filter
property and the recordset's Find method to perform a Like comparison. Check
them out in online help (go to msdn.microsoft.com/library if you have no
online help installed on your pc. The ADO reference is in the Data Access
node)

However, you would be much better off incorporating this filter in the SQL
statement so that you will not be retrieving unneeded records across the
network. Be conservative of your network's resources. In addition, you can
perform more powerful comparisons in SQL than you can in either Filter or
Find.

HTH,
Bob Barrows
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,054
Latest member
LucyCarper

Latest Threads

Top