Is there a more simple way for long IF statement?

buratti

Registered User.
Local time
Today, 10:58
Joined
Jul 8, 2009
Messages
234
I need to us an IF Then statement to see if a memo field contains any or all combinations of certain strings. Now I can write a REALLY LONG If statement to include all my search strings like...

If (InStr(Me.Memo, "string 1") = 1) or (InStr(Me.Memo, "string 2") = 1) or (InStr(Me.Memo, "string 3") = 1)... and so on for however many more strings.

But I was wondering if there was an easier "shorter" way of doing so? Maybe sometihng with an array and/or loop? I should know this, but I'm a little rusty (and tired) at the moment. Any Suggestions?
 
What form is this list in ...
Code:
"string 1", "string 2", "string 3", ... "string n"
A table? An array? A hard-coded list of literal strings?
It's not hard to write a loop, but how to write it depends what shape the data's in. Maybe you still need to put the data into a structure that lends itself to loopage. If all this data's in a table, open a recordset. If it's in an array, use a For..Each..Next loop, and so on...
Mark
 
Although probably not the proper way to structure it, all the strings will be hard coded. Hard coded because there would never be a situation where I would need to add, modify or remove a search string. I was thinking an array. Something like this would in theory work, but I was just wondering if this is the easiest/good practice to use this following technique...

(thrown together, untested and syntax is probably off, but just to get the idea...)

Code:
dim string as string
Dim i as integer
 
String(1) = "String 1"
String(2) = "String 2"
...
String(n) = "String n"
 
For i = 1 to "n"
 
   If instr(me.memo,string(i))>=1 then
        'Whatever needs to be done if string found in field
        Exit Loop
   End If
Next i

I guess I kinda answered my own question, but just asking now if this is "good practice", or is there any "better" way of doing it?
 
I would put them in a table and open a recordset. Hard coded data always feels like a contradiction to me, like I want to code to consume and produce data, not BE data. But the "best" way?
One thing you can do as shorthand to create an array is this ...
Code:
Dim vArray as variant
vArray = Array("Element 1", "Sample 2", 12, "And so on")
and then you might want to explore a For..Each..Next loop which doesn't require an index variable ...
Code:
dim var as variant
for each var in vArray
  If instr(me.memo, var) > 0 then
[COLOR="Green"]    'Whatever needs to be done if string found in field[/COLOR]
    Exit For
  End If
next
Mark
 

Users who are viewing this thread

Back
Top Bottom