Proper IF/THEN statements - avoid clutter

stephenlecompte
07-20-2003, 08:27 AM
Can someone give me a better setup for IF/THEN statements than the following code. There are some unnecessary repetitions in the code like the OA.Value = 0 and Local1.Value = 0. I feel so stupid not understanding the variations in the IF/THEN statements. I'm used to dealing with the old BASIC programming. Basically I'm writing a transportation program. Thanks for the help. Stephen

If txtBox(8) = "364" Then United.Value = 1: OA.Value = 0: Local1.Value = 0: Call Text_Box
If txtBox(8) = "2864" Then Mayf.Value = 1: OA.Value = 0: Local1.Value = 0: Call Text_Box
If txtBox(8) = "HO" Then HO.Value = 1: OA.Value = 0: Local1.Value = 0: Call Text_Box
If txtBox(8) = "1764" Then Mayf.Value = 1: OA.Value = 0: Local1.Value = 0: Call Text_Box

-OR-

If txtBox(10).Text = "TX" And txtBox(6).Text = "TX" Then INTRA = 1
If txtBox(10).Text = "tx" And txtBox(6).Text = "tx" Then INTRA = 1
If txtBox(10).Text = "TX" And txtBox(6).Text = "tx" Then INTRA = 1
If txtBox(10).Text = "tx" And txtBox(6).Text = "TX" Then INTRA = 1

- OR -

If United.Value = True And military.Value = 1 And INTRA = 0 Then txtBox(11).Text = Val(AUTONUM1) + Val(UMINTER)
If United.Value = True And military.Value = 1 And INTRA = 1 Then txtBox(11).Text = Val(AUTONUM1) + Val(UMINTRA)

Blackfire
07-20-2003, 08:34 AM
One way to tidy things up might be:


Select Case txtBox(8)
Case is = "364"
whatever.....
Case is = "2864"
whatever.....
Case is = "HO"
whatever......
Case is = "1764"
whatever.....
End Select

bpd
07-20-2003, 08:57 AM
If txtBox(10).Text = "TX" And txtBox(6).Text = "TX" Then INTRA = 1
If txtBox(10).Text = "tx" And txtBox(6).Text = "tx" Then INTRA = 1
If txtBox(10).Text = "TX" And txtBox(6).Text = "tx" Then INTRA = 1
If txtBox(10).Text = "tx" And txtBox(6).Text = "TX" Then INTRA = 1For case insensitivity, try something like:If (StrComp(txtBox(10), "TX", vbTextCompare) = 0) _
And (StrComp(txtBox(6), "TX", vbTextCompare) = 0) Then
INTRA = 1
End If



If United.Value = True And military.Value = 1 And INTRA = 0 Then txtBox(11).Text = Val(AUTONUM1) + Val(UMINTER)
If United.Value = True And military.Value = 1 And INTRA = 1 Then txtBox(11).Text = Val(AUTONUM1) + Val(UMINTRA)For this example there are many possibilities, depending on how many options there are for each portion of the conditional. That is to say, how many possible values for military.Value and INTRA. Here are a couple of suggestions:If United.Value Then
If military.Value = 1 Then
If INTRA = 0 Then
txtBox(11).Text = Val(autonum1) + Val(UMINTER)
ElseIf INTRA = 1 Then
txtBox(11).Text = Val(autonum1) + Val(UMINTRA)
End If

'Instead of the above, you could also do:
txtBox(11).Text = Val(autonum1) _
+ Val(Choose(INTRA + 1, UMINTER, UMINTRA))
Else
'Military.Value <> 1

'If there are many values for Military.Value, perhaps
'a Select statement (like the one Blackfire showed)
'would be best.
End If
Else
'United.Value = False
End If

wengwashere
07-20-2003, 08:57 AM
If txtBox(8) = "364" Then United.Value = 1: OA.Value = 0: Local1.Value = 0: Call Text_Box
If txtBox(8) = "2864" Then Mayf.Value = 1: OA.Value = 0: Local1.Value = 0: Call Text_Box
If txtBox(8) = "HO" Then HO.Value = 1: OA.Value = 0: Local1.Value = 0: Call Text_Box
If txtBox(8) = "1764" Then Mayf.Value = 1: OA.Value = 0: Local1.Value = 0: Call Text_Box


Since you are setting the OA.Value outside the IF THEN, you could do this :

dim blnTrigger as Boolean

blnTrigger = False

If txtBox(8) = "364" then
United.Value = 1
blnTrigger = True
Elseif txtBox(8) = "2864" Then
Mayf.Value = 1:
blnTrigger = True
ElseIf txtBox(8) = "HO" Then HO.Value = 1
HO.Value = 1
blnTrigger = True
ElseIf txtBox(8) = "1764" Then
Mayf.Value = 1
blnTrigger = True
else
blnTrigger = False
End IF

IF BlnTrigger Then
'Your Common Code here...
OA.Value = 0:
Local1.Value = 0:
Call Text_Box
End If


This eliminate the redunduncy of your code.


If txtBox(10).Text = "TX" And txtBox(6).Text = "TX" Then INTRA = 1
If txtBox(10).Text = "tx" And txtBox(6).Text = "tx" Then INTRA = 1
If txtBox(10).Text = "TX" And txtBox(6).Text = "tx" Then INTRA = 1
If txtBox(10).Text = "tx" And txtBox(6).Text = "TX" Then INTRA = 1


Can you explain what this code do?



If United.Value = True And military.Value = 1 And INTRA = 0 Then txtBox(11).Text = Val(AUTONUM1) + Val(UMINTER)
If United.Value = True And military.Value = 1 And INTRA = 1 Then txtBox(11).Text = Val(AUTONUM1) + Val(UMINTRA)


I dont see any difference between the code except for the INTRA = 0 / INTRA = 1. You could do this.


If United.Value = True And military.Value = 1 And (INTRA = 0 Or INTRA = 1) Then
txtBox(11).Text = Val(AUTONUM1) + Val(UMINTER)
End If

Robse
07-20-2003, 09:04 AM
Second example:


If Not StrComp(txtBox(10).Text , txtBox(6).Text , vbTextCompare) Then
'whatever
End If


Edit: No, that's wrong. Look at bpd's code

Third example:


If United.Value = True And military.Value = 1 Then
If INTRA = 0 Then txtBox(11).Text = Val(AUTONUM1) + Val(UMINTER)
If INTRA = 1 Then txtBox(11).Text = Val(AUTONUM1) + Val(UMINTRA)
End If

passel
07-20-2003, 09:49 AM
[QUOTEPOST='stephenlecompte']
If United.Value = True And military.Value = 1 And INTRA = 0 Then txtBox(11).Text = Val(AUTONUM1) + Val(UMINTER)
If United.Value = True And military.Value = 1 And INTRA = 1 Then txtBox(11).Text = Val(AUTONUM1) + Val(UMINTRA)


I dont see any difference between the code except for the INTRA = 0 / INTRA = 1. You could do this.


If United.Value = True And military.Value = 1 And (INTRA = 0 Or INTRA = 1) Then
txtBox(11).Text = Val(AUTONUM1) + Val(UMINTER)
End If


If INTRA = 0 then it's between States (INTERSTATE)
IF INTRA = 1 then it's within the State (INTRASTATE)

stephenlecompte
07-20-2003, 10:56 AM
Ya'll the best

Thanks
Stephen

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum