If field1 contains x, enter this in field2

I can’t seem to figure out the solution to this basic question: How can I test a field for a word or phrase within a longer piece of text (e.g, “hello” within “hello, good morning”). Then if that field is found, set one other field to some value. So, for example, if “hello” is found, set the second field to “hugs and kisses.” How’s that done?

Let’s call your fields “MyTextField” and “MyOtherField”. Then you could use code like this:

field MyOtherField
formulafill ?(MyTextField contains "hello","hugs and kisses","")

You more-or-less wrote the answer in the subject line:

if Field1 contains "hello"
    Field2="hugs and kisses"
endif

To test only whole whole words rather than just a simple test of whether A contains B (e.g. to match “hello goodbye” but not “helloo”), instead of contains I’d use regexmatch with an appropriate pattern.

Or use the containsword operator instead of contains.

Thank you, KJM, pcnewbie, and gary.

I’m still doing something wrong, and I just don’t get it. See the screenshots, one for each method. In each one, “George” should have the field “saveaseat” marked “yes.” What am I not seeing here?

Specimen !

Specimen2

Specimen 1 is almost correct. The problem is that you have entered code into the Formula pane. See Automatic Field Code vs. Automatic Field Calculations

You could also do this with a formula. The formula would be:

?(attending contains "yes","yes","")

this should be placed in the Formula pane of the saveaseat field.

The code you used in Specimen 2 is valid, shouldn’t be used in automatic data entry code. Instead, this code could be used in a freestanding procedure. When you run this code, it will set the saveaseat field for every record in the database, not just the current record.

Lots of options! I think probably the formula I specified above is your best bet.

Thank you, Jim!

Also worth noting is your field name. In Specimen ! one it’s save a seat, while in Specimen2 two it’s saveaseat.

Because of the spaces in the name, save a seat won’t work in any formula without being properly formatted. It should be «save a seat».

Since saveaseat has no spaces, it can be used as-is.

Thanks, JamesCook. Noted.

In Specimen ! (held down the shift key did you) your If statement is setting the field, “save a seat”, to yes. But your field name, save a seat, appears to contain spaces. It’s my understanding that if a field name contains spaces, and is used in a formula, then it must be bracketed with Chevrons (option l, and shift-option L). Maybe that doesn’t matter anymore?

I do understand that you must put the assignment code in the correct place.

Thanks, designer. I’ve fixed the spaces issue in “save a seat.”

The key combination you gave for chevrons doesn’t work on my keyboard. (I’m on a Mac. I suspect that PandoraX isn’t available on anything else.) What works is backslash for left chevron («) and shift-backslash for right chevron (»).

Thanks!

Yes, you have it right.