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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

If/then evaluating wrong

Status
Not open for further replies.

OhioSteve

MIS
Mar 12, 2002
1,352
0
0
US
I am having problems in an ASP page running VB.

I have several if/then statements such as...

if request.form(&quot;DateCompleted&quot;)<>null then
do somthing
end if

The &quot;if&quot; ALWAYS evaluates to false. In other words, the machine always thinks that the value is null. I tried writing the value to a variable and outputting the variable. The variable WILL display a value, so I know that it is not really null, but the machine STILL treats it as a null. I even tried doing the comparison on this variable, so that my code looked like this...

test1=request.form(&quot;DateCompleted&quot;)
response.write test1
if test1<>null then
do somthing
end if

The machine would display test1, but it STILL evaluated the &quot;if&quot; as false.


 
Null can't be used in equality statements (remember that it means, &quot;I don't know&quot; - so how can you have two unknown values be equal?)

Instead use Is Not Null and Is Null. For example:
If myVariable Is Not Null Then
'do stuff
End If

BUT!!!! Request.Form(&quot;myField&quot;) will NEVER be null. Instead, if the variable is empty, it returns the empty string &quot;&quot;, so this works:
If Len(Request.Form(&quot;MyField&quot;) Then
'do stuff
End If
 
ASP page running VB VBScript?!?

The &quot;if&quot; ALWAYS evaluates to false.

the if statement is not going to work incorrectly unless it's told to. sorry, that's really the just of it.

are you confusing null with empty or &quot;&quot;.
for instance

test1=&quot;you&quot;

' this will not work
if test1<>null then
Response.Write &quot;do something&quot;
end if

' but this will
if test1<>&quot;&quot; then
Response.Write &quot;do something&quot;
end if

or even
if NOT isEmpty(test1) then
Response.Write &quot;do something&quot;
end if

_____________________________________________________________________
[sub]Where have all my friends gone to????[/sub]
onpnt2.gif

 
[wink] at least I know I'm not the only one thinking the way I do

_____________________________________________________________________
[sub]Where have all my friends gone to????[/sub]
onpnt2.gif

 
My goal was to STOP the nested code from executing if the field was empty. I have reviewed your comments. After some experimentation I have decided upon this syntax:

if len(request.form(&quot;DateCompleted&quot;))>0
do something
end if

This code seems to work VERY reliably.

Thanks to everyone!
 
I used this model:

If Len(Request.Form(&quot;MyField&quot;) Then
'do stuff
End If

It worked great! Thanks everyone!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top