Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

problem with If...Then statement

Status
Not open for further replies.

PushCode

Programmer
Dec 17, 2003
573
US
I'm looping through some field names, but I don't want two of them to be hyperlinks. It works when I just do it for one field name:
Code:
If oTableField.Name <> "Address" Then
But when I do it with two, it doesn't work:
Code:
If oTableField.Name <> "Address" OR oTableField.Name <> "Phone" Then
It goes ahead and puts hyperlinks on all the field names.

Here's the full relevent code:
Code:
For Each oTableField In oRecordSet.Fields
	Response.Write "<th bgcolor=""#CCCCCC"" align=""left"">"
	If oTableField.Name <> "Address" OR oTableField.Name <> "Phone" Then
		Response.Write "<A href=""" & url & "?csort="& oTableField.Name &"&lastsort="& thissort & """>"
	End If
	Response.Write "<b>" & oTableField.Name & "</b></a></th>"
Next

Any ideas? Thanks!
 
Think about the logic.

Suppose oTableField.Name = "EyeColor". Also suppose you want to make this a hyperlink.

[tt][blue]
If oTableField.Name <> "Address" OR oTableField.Name <> "Phone" Then
If "EyeColor" <> "Address" or "EyeColor" <> "Phone" Then
If True or True Then
If True Then
[/blue][/tt]

Clearly, this will fall through the if (and cause it to be a hyperlink. Now, run through the same exercise with oTableField.Name = "Phone"

[tt][blue]
If oTableField.Name <> "Address" OR oTableField.Name <> "Phone" Then
If "Phone" <> "Address" or "Phone " <> "Phone" Then
If True or False Then
If True Then
[/blue][/tt]

Since True Or False evaluates to True, the code falls through the if check and becomes a hyperlink.

The simple answer, of course, is to change the OR check to an AND.

Make sense?

-George

"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
George, your post is an excellent example of how not only to provide an answer but also to provide an understanding so that others do not repeat the problem. Have a star!

------------------------------------------------------------------------------------------------------------------------
"Men occasionally stumble over the truth, but most of them pick themselves up and hurry off as if nothing ever happened."
- Winston Churchill
 
Thanks for the star Mr. Churchill.

-George

"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top