VBScript: If InStr(MyText, SearchText) then…

20 Nov

One thing I never liked about VB as a language is that it is too flexible as a programming language, although I am a rather full-fledged VBScript scripter. The flexiblity also introduces bad programming practices.

One of the allowed syntax in VBScript is…

If InStr(MyText, SearchText) then (do something)…

If you look the function for InStr(), it actually returns a position number or 0 (or null). In VB, this is legal, because the IF statement sees a 0 as false and I guess a postive number as true. This gives the appearence that InStr() returns a boolean, when this is not true.

Just because this can be done in VB, does not make it a good programming practice. This is just both lazy and bad practise, in my opinion.

The more correct statement should be:

If InStr(MyText, SearchText) > 0 then (do something)…

This will undeniably tell you what kind of values InStr() returns… a number


Posted by on November 20, 2007 in Scripts, VBScripts



14 responses to “VBScript: If InStr(MyText, SearchText) then…

  1. Steve

    April 18, 2010 at 11:12 pm

    Interestingly, I notice you also wrote your entire post without using any apostrophes. My father was a bit of a Star Trek fan, and I was in high school when The Next Generation series was on TV. As you may recall, there was a robot character named Data whose creator had also created an “evil twin” who was more human. One of the slight character differences between the two that I thought was fascinating (and good writing/characterization by the authors) was the fact that Data could not use abbreviations – he never used “can’t, won’t, doesn’t, isn’t, or any of the others – a sign not only of how he was designed to be precise, but in some regards, his inflexibility was a sign of his lack of humanity.

    Humans also have another valuable trait: efficiency. I once heard a story of a programmer back in the 50s or 60s that had to write super-efficient code due to the hardware restraints of those days. Another, younger programmer was asked to change the program slightly, so he started to read the code (you have to understand it before you can change it). What he found was that the program had an If/Then loop with an incrementing counter … with no exit statement. The younger programmer wracked his brains trying to understand how the code worked properly as written, and the answer came to him weeks later. The program was wrtten in such a way that when the counter reached its desired maximum value the memory buffer rolled back over to 0 (like a car’s odometer) and started over again at the beginning of the program. The old programmer understood the limitations and characteristics of the hardware he was working with enough that he could eliminate a line or two of code (and thereby conserve vital memory space for essential code).

    I would submit to you that your attempt to be thorough as a personal preference, particularly aided by the fact that these days we have more memory space to work with than anyone could ever want. But it’s by no means neccessary, and as such it’s neither lazy nor bad practice. It’s simply your preference.

    • saltwetfish

      April 19, 2010 at 1:59 pm

      Hi Steve,

      Thanks for your interesting comment. I guess the reason is that I was taught during my school days that apostrophes in the form like can’t, I’m or wouldn’t can only be used in spoken form. In written language, we should never shorten the words. That is why I sometimes write without apostrophes, but laziness does creep in… 🙂

  2. Steve

    April 19, 2010 at 10:40 pm

    Interesting. I’ve never heard anyone say they were taught not to use abbreviations in writing. For many years I also refrained from using abbreviations when writing business-related emails and letters. But someone pointed out to me that even emails I intended as informal came across as formal and “stuffy”. I thought about it for a week or so and examined what I was writing and realized it was my lack of contractions. While it’s good for formal writing, well … that’s the point – it comes across as formal. To this day, when I write emails I have to go back through them and edit them with an eye towards removing the formality if it’s not appropriate in the given context… my two biggest faults are not abbreviating, and the use of the word “that”. It’s amazing how often I write it when the sentence works just fine without it.

    • saltwetfish

      April 20, 2010 at 2:42 am

      Hey, thanks for pointing this out, I will try to be less formal next time.

      Incidentally, I have been learning Japanese language and there are short cuts in spoken language that will never be used written language. Okay, never is too strong.

  3. Steve

    April 20, 2010 at 11:34 pm

    Well, you don’t have to try to. I was just pointing out that you have the option. It’s up to you to decide when and if either style is appropriate.

  4. Steve

    April 20, 2010 at 11:41 pm

    PS – I just re-read my previous posts and suddenly remembered that I too was taught not to use contractions in term papers many years ago (1st-5th grades). I think that loosened up some in high school and college though. I guess the rigidity depends on the subject matter and the teacher, as well as the course. In a creative writing course it might be accepted as part of the author’s style. In any case, ’nuff said.

  5. Jeff

    April 8, 2013 at 11:43 am

    I had to put together a quick script at work and didn’t have access to my sample code base, I was googling for “instr” examples because I wanted to search for specific text in an if statement and the samples I found didn’t help.

    I was more specific in my search and looked up the exact text “if instr” because it didn’t click that using instr on its own in an if statement would return a number rather than a boolean yes/no and I just wanted a sample syntax to copy.

    This blog was the first link in the google search, and explained what I was missing.

    If others had followed the suggestion of adding the > 0 it would have helped me find the answer that 30 seconds faster. Having said that, in the quick and nasty 8 line script I’ve written I haven’t bothered adding it in.

    I didn’t notice the lack of contractions in the original post and based on Steve’s Aspergers syndrome inspired writing style I’d suggest you keep doing your own thing and ignore him completely.

    • Kelvin Wong

      April 8, 2013 at 12:40 pm

      Thanks for your comments Jeff, I am glad this is still helpful after so many years!

  6. tradingonup

    April 8, 2013 at 4:17 pm

    Jeff – Bite Me. 🙂

    Kelvin – working on a 2,000+ script in my spare time these days, still using this tip. Thanks again :o)

  7. lkp550

    May 24, 2013 at 11:57 pm

    I’m new to VBScript and using it to do some data entry in a PCOMM application. It’s not easy finding good tips and tricks that can be useful; especially with the kind of program I am dealing with.

    I appreciate your post as has definitely helped me resolve my issue. Do you have, or know of, any good tutorials that explain things the way you did? It sure would help get past many of the obstacles that I’ve been facing.

  8. Matt

    February 22, 2014 at 5:00 am

    This post never dies. I come here at least one every 6 months when I can’t remember how to do this. Hopefully you never let this die, because I’m sure I’ll still back here in the years to come.

  9. Matt

    March 19, 2015 at 3:40 am

    1 year from my last post, I’m here again since I can’t remember the syntax for this. Thanks for leaving this up.

    • Kelvin Wong

      March 19, 2015 at 10:16 am

      I am very glad that after so long this is still helpful!


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: