If/Then Statements in VBS

  • Thread starter Thread starter Micah Chaney
  • Start date Start date
M

Micah Chaney

This may be a stupid question, but I can't figure it out. I'm in Outlook
2003. I'm in a custom form and all I'm trying to do is say if objControlA
is Like "Blue" Then...

I keep getting a Runtime error on that line, and then it won't let me
modify, it shuts down Outlook altogether. Maybe it's 'cause I don't know how
to use the debugging tools. So I guess I have two questions:

1. How would I change the following statement (assume everything's already
Set)...
if objControlA.Value = "Fed Ex" Then
objControlA1.Value = "Tracking Number"
end If
to
if objControlA.Value Like "Fed Ex" Then
objControlA1.Value = "Tracking Number"
end If

So if Control A says Fed Ex Ground or Fed Ex Air or Fed Ex 3-day or Fed Ex
overnight, all those values should make A1's value "Tracking Number".

2. When I do get a runtime error, and I go to the Script Debugger screen,
and I see the line that's causing the problem, how do I change it in the
debugger screen? If that's not possible, how do I stop the seeming loop in
the error message that keeps telling me I have to debug, and won't stop
unless I shut down Outlook?

Any help is appreciated. Thanks in Advance.
 
1) It's probably not the If ... Then that's the problem but the way you're
referring to the form control. If the control is bound to an Outlook
property, then you don't use the control value at all. See
http://www.outlookcode.com/d/propsyntax.htm

2) You don't. Make a note of the line, end the debugging session (don't
click Debug again for the same error), fix it in the form, and try again.
 
1. The "if" works fine if the rest of the criteria says "=" but how do I get
it so the situation I describe happens? Do I need to say
if objControlA.Value = "Fed Ex Ground" or "Fed Ex Air" or "Fed Ex Standard"
or "Fed Ex 2-Day" Then
objControlA1.Value = "Tracking Number"
end If
?
It'll work that way, but I want it to be more flexible, so if we add a "Fed
Ex Overnight" we don't have to modify the code to accommodate.

2. That's how it used to work, now once I click "No" I don't wish to Debug,
it'll pop up again asking me to Debug, and the only way to stop it is to
TaskManager, Kill Outlook. Does this mean I have a loop in my code that's
causing the same error to pop up over and over again. I can't even get to
the Code Form to fix the code.
 
1) The expression between If and Then must return either True or False. If
you have multiple comparisons, each must be a separate expression, separated
by AND or OR, e.g.:

If objControlA.Value = "Fed Ex Ground" OR _
objControlA.Value = "Fed Ex Air" Then

If all the "true" values have the "Fed Ex" in common, then you can check
just part of the control value:

If Left(objControlA.Value, 6) = "Fed Ex" Then

Another approach would be to use a Select Case block.

2) Yes, it sounds like you have a loop.

--
Sue Mosher, Outlook MVP
Author of
Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
Thank you Sue, I did a combination it was the "OR _" that I needed to know.
Thanks a lot. : )
 
Back
Top