If statement against several values

W

wshall

Is there a way to have an If statement compare against a few values
instead of just one? I need to convert some strings to convert some
reserved characters (& < > " and '). I would like to have an if
statement that checks for all of them... something like SQL's In
function:

WHERE strFind in ( '&', '>', '<' ..... etc

Is there some way to check against a list of items when using an if
statement?

Thanks!
 
S

Stuart McCall

Is there a way to have an If statement compare against a few values
instead of just one? I need to convert some strings to convert some
reserved characters (& < > " and '). I would like to have an if
statement that checks for all of them... something like SQL's In
function:

WHERE strFind in ( '&', '>', '<' ..... etc

Is there some way to check against a list of items when using an if
statement?

Thanks!

Try grabbing this function:

http://www.smccall.demon.co.uk/Strings.htm#ReplaceList

Then let's say you're converting these characters to HTML-safe expressions:

sResult = ReplaceList(strFind, "&", "&amp;", ">", "&gt;", "<", "&lt;")
 
W

wshall

Thanks, Stuart! I hadn't seen your code site before this. That's a
reason I like Access... lots of code samples to use as starting
places.

Your code would certainly work for this specific situation... but I'm
really trying to learn how to handle this kind of situation in the
future. I'm tried of having to enter repetative OR statements, such
as:

If chrTmp = "&" Or _
chrTmp = "<" Or _
chrTmp = ">" Or _
chrTmp = "'" Or _
chrTmp = """" Then

I'm wondering if there is a better way... something like SQL's IN
statement that I could use for future situations.

Any ideas?

Thanks!
 
S

Stuart McCall

Thanks, Stuart! I hadn't seen your code site before this. That's a
reason I like Access... lots of code samples to use as starting
places.

Your code would certainly work for this specific situation... but I'm
really trying to learn how to handle this kind of situation in the
future. I'm tried of having to enter repetative OR statements, such
as:

If chrTmp = "&" Or _
chrTmp = "<" Or _
chrTmp = ">" Or _
chrTmp = "'" Or _
chrTmp = """" Then

I'm wondering if there is a better way... something like SQL's IN
statement that I could use for future situations.

Any ideas?

Thanks!

Ok, when you have a single value and need to test multiple conditions, a
Select Case statement fits the bill better than If..Then.

Select Case chrTmp
Case "&", "<", ">", "'", """"
'Do whatever
Case Else
'chrTmp is none of the above
End Select
 
B

Bob Quintal

@m36g2000hse.googlegroups.co
m:
Thanks, Stuart! I hadn't seen your code site before this. That's a
reason I like Access... lots of code samples to use as starting
places.

Your code would certainly work for this specific situation... but
I'm really trying to learn how to handle this kind of situation in
the future. I'm tried of having to enter repetative OR statements,
such as:

If chrTmp = "&" Or _
chrTmp = "<" Or _
chrTmp = ">" Or _
chrTmp = "'" Or _
chrTmp = """" Then

I'm wondering if there is a better way... something like SQL's IN
statement that I could use for future situations.

Any ideas?

Thanks!

the Instr() function can do what you want.

If instr(1,"&<>'""","""") > 0 Then
 

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

Top