How to build an If/then /else statements with multiple conditons

  • Thread starter Thread starter Kathy
  • Start date Start date
K

Kathy

Hi,
I am trying to build an If/Then/Else statement with
several conditions. I took VB several years ago and have
not used it since the class. Can you recommend a tutorial
or book etc. that would provide a simple explanation of
how to build this kind of statement.
Also I just want to be sure that I should be building a
module and not a macro for these types of functions?

Thanks so much,
Kathy
 
You could use;
If condition1 then
do 1
ElseIf condition2 then
do 2
ElseIf conditionN then
do N
Else
do nothing
end if
Or;
Select Case Condtion
Case is = 1
do 1
Case is = 2
do 2
Case Else
do nothing
End Select

Regards,
..d
 
dg had a good reply. Another possibility for multiple condition is something
like this.

If (a=1 And b=2) Or (c=1 And d=2) Then
 
The problem with
If (a=1 And b=2) Or (c=1 And d=2) Then

is that VB doesn't have the feature of lazy evaluation.
If 1 part of the condition generates an error, the whole
line generates an error (even if the program would have
chosen another path)

example

if TRUE=TRUE or rst.fields(0)=TRUE then 'object not set
fcContinue
else
fcabort
end

will give in C++ and alike fcContinue, but in VB ->error
rst not set
 
VBA does the same thing with an IIf, it evaluates both possible answers
whether it needs to or not. This is not a problem, but just something to be
aware of so that you set things up in such a way as to not do that.
 
Hi Kathy,

I'd start by looking at the Help menu for If...then
statement. I'm not sure how easy a file it is to find on
the help menu but if you use the office assistant, you can
ask it "If then else statement" and that should take you to
the correct file.

About macros, I'm not sure which version of Access you're
using but macros are more or less outmoded. The main
reason they are still present in later in versions of
Access is for backward compatibility. I don't know anyone
who would recommend using them at this point.

VB is your best bet. It also gives you the option of using
a Select Case Statement, instead of If..Then, if you have
a lot of conditions to deal with.

The If..Then sytax is:

If Condition Then
code to execute goes here
ElseIf AnotherCondition Then
code to execute goes here
Else
code to execute goes here
End If


The Conditions can deal with various things. For instance,
if you want to base your If..Then structure on the value
entered in a control on a form, you might have:

If Me.ctlName Is Null Then....

or it might be a variable you're testing:

If (varName) Not IsNull Then

You can also link your conditions together with operators
like "And" and "Or" and also comparative operators like >=,
Like, Between, etc.

The trouble is that If..Then's with lots of conditions can
be become very confusing to read. That's where Select Case
comes in.

Again, I recommend the Access Help on Select Case.

Also, another useful thing is that you can combine Select
Case and If...Then constructs.

If you need specific help, reply with a description of your
scenario.

Good Luck!
Elvira
 
Thank you Elvira for your help....here is what I have done
so far:
Function BillingCode(totalvisits)
BillingCode = 317
If totalvisits = 4 Then
BillingCode = 317
ElseIf totalvisits = 3 Then
BillingCode = 317 + 1
ElseIf totalvisits = 2 Then
BillingCode = 317 + 1
ElseIf totalvisits = 1 Then
BillingCode = 317 + 2
ElseIf totalvisits = 0 Then
BillingCode = 317 - 317
End If
End Function
However I need to add a variable that is not a numeric,
but text. It would look something like this,(see below)
but at present it does not work. Can you help...I am
pretty much a newbie that got thrown this project. Thanks
again.

Function BillingCode(totalvisits)
BillingCode = 317
If totalvisits = 4 Then
BillingCode = 317
ElseIf totalvisits = 3 Then
BillingCode = 317 + 1
ElseIf totalvisits = 2 Then
BillingCode = 317 + 1
ElseIf totalvisits = 1 Then
BillingCode = 317 + 2
ELSEIF TOTAL VISITS = 1 AND [SHIFT]="PD" THEN
BILLING CODE = 317 + 6
ElseIf totalvisits = 0 Then
BillingCode = 317 - 317
End If
End Function

thanks again...any suggestions for good books that help
with coding would be appreciated too.
 
However I need to add a variable that is not a numeric,
but text. It would look something like this,(see below)
but at present it does not work. Can you help...I am
pretty much a newbie that got thrown this project. Thanks
again.

Function BillingCode(totalvisits)
BillingCode = 317
If totalvisits = 4 Then
BillingCode = 317
ElseIf totalvisits = 3 Then
BillingCode = 317 + 1
ElseIf totalvisits = 2 Then
BillingCode = 317 + 1
ElseIf totalvisits = 1 Then
BillingCode = 317 + 2
ELSEIF TOTAL VISITS = 1 AND [SHIFT]="PD" THEN
BILLING CODE = 317 + 6
ElseIf totalvisits = 0 Then
BillingCode = 317 - 317
End If
End Function

This depends on where [Shift] is coming from. Generally,
you should communicate values to a procedure through its
arguments. Here's how I would code your function:

Function BillingCode(totalvisits As Long, shift As String)
Select Case totalvisits
Case 4
BillingCode = 317
Case 3, 2
BillingCode = 317 + 1
Case 1
If shift ="PD" Then
BillingCode = 317 + 6
Else
BillingCode = 317 + 2
End If
Case 0
BillingCode = 317 - 317
Case Else
BillingCode = 317
End Select
End Function
 
Back
Top