Link to home
Start Free TrialLog in
Avatar of Aleks
AleksFlag for United States of America

asked on

ASP Syntax for IF statement

I have an IF statement, but seems like my styntax is incorrect.

The area should be displayed IF rs_qnr01  IS null (no results)  or
IF rs_qnr01 is NOT null AND the field rs_qnr01("MiddleNm")  = True

This is the code I have, but its behaving strange.

            <% ' field customized and true
if (rs_qnr01.EOF And rs_qnr01.BOF)  OR ((Not rs_qnr01.EOF Or Not rs_qnr01.BOF) AND (rs_qnr01.Fields.Item("MiddleNm").Value = "True"))  then  
%>  

Open in new window


Any ideas ?
Avatar of Randy Poole
Randy Poole
Flag of United States of America image

If MiddleNm is a boolean then remove the quotes around "True"
Avatar of Aleks

ASKER

I am still getting the same error:

ADODB.Field error '800a0bcd'

Either BOF or EOF is True, or the current record has been deleted. Requested operation requires a current record.

/bluedot/Questionnaire/qnr_01.asp, line 270

in this case 'rs_qnr01'  is null, so it should skip the second part of the statement correct ?  Its trying to run it and hence the error since 'rs_qnr01' is null, so it can't compare the field.

Can we add code so that if "MiddleNm" is null doesnt compare it ?

This is the part of the code with the issue:

(Not rs_qnr01.EOF Or Not rs_qnr01.BOF AND rs_qnr01.Fields.Item("MiddleNm").Value = True)

Its trying to cmpare MiddleNm when there is no rs_qnr01  recordset to begin with.
Avatar of Big Monty
you need to split up the if statements, you cannot check for the existence of data returned in a record set AND for a specific column value. if no data is returned, the column won't exist.

try:

 <% ' field customized and true
if not rs_qnr01.EOF And not rs_qnr01.BOF then
    if rs_qnr01.Fields.Item("MiddleNm").Value = True then  
%>  

do whatever

<%
     end if
end if
%>

if the field MiddleNm is a string value, then you'll need to surround True in quotes
if (rs_qnr01.EOF And rs_qnr01.BOF)  ORELSE ((Not rs_qnr01.EOF Or Not rs_qnr01.BOF) ANDALSO (rs_qnr01.Fields.Item("MiddleNm").Value = True))  then 

Open in new window


Change it so it uses short circuiting so it does not evaluate the next part unless it needs to.
as far as I know, ORELSE is not valid classic asp syntax, but rather vb and vb.net
Avatar of Aleks

ASKER

I tried big monty's solution but it did not work.  it should have displayed the field because rs_qnr01  IS NULL, but it did not. Here is the full code:

<% ' middlename customized and true
if not rs_qnr01.EOF And not rs_qnr01.BOF then
    if rs_qnr01.Fields.Item("MiddleNm").Value = True then  
%>  
               
               <tr>
                 <td>Middle name</td>
                 <td>
                   <div class="col-sm-12">
                     <input name="MiddleNm" type="text" class="form-control" id="MiddleNm" form="form1" value="<%=(rs_contact.Fields.Item("MiddleNm").Value)%>" maxlength="30">
                   </div>
               </td>
			 </tr> <% 	Else  'middlename hidden %>
<input name="MiddleNm" type="hidden" id="MiddleNm" value="<%=(rs_contact.Fields.Item("MiddleNm").Value)%>">
<%
     end if
end if
%>

Open in new window


The code seems to be missing the first part:  (rs_qnr01.EOF And rs_qnr01.BOF)

If that is the case then show the first region, forget the rest. If not then compare the value of the field, if true then show the first region if not go to the else and show the hidden field value.
I think I originally misunderstood you, I thought you wanted to show the field if data was being returned.

can you explain exactly what you want to happen?
Avatar of Aleks

ASKER

Sure.

If rs_qnr01  recordset is empty display

 <tr>
                 <td>Middle name</td>
                 <td>
                   <div class="col-sm-12">
                     <input name="MiddleNm" type="text" class="form-control" id="MiddleNm" form="form1" value="<%=(rs_contact.Fields.Item("MiddleNm").Value)%>" maxlength="30">
                   </div>
               </td>
			 </tr> 

Open in new window


OR if rs_qnr01  is NOT null and returns values, then check value "MiddleNm" if its true then show the above region as well.

If the value of "MiddleNm" is NOT True  then show the hidden field below:

<input name="MiddleNm" type="hidden" id="MiddleNm" value="<%=(rs_contact.Fields.Item("MiddleNm").Value)%>">

Open in new window


So .. the top part should show if either the recordset rs_qnr01 is null or if its not and the field value is true.
The top part should only show if rs_qnr01 is NOT null and the value of the field is false.
ASKER CERTIFIED SOLUTION
Avatar of Big Monty
Big Monty
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Aleks

ASKER

Thanks. I am starting a conference, but will check in one hour.
Avatar of Aleks

ASKER

Can this be done without a function ?  There are like 30 fields on that one page, this would require me to do 30 functions too ?
let's see if this does what you want, then we can worry about the other fields
Avatar of Aleks

ASKER

ok
Avatar of Aleks

ASKER

Microsoft VBScript compilation error '800a0401'

Expected end of statement

/bluedot/Questionnaire/qnr_01.asp, line 278

Response.Write "<tr><td><input name='MiddleNm' type='hidden' id='MiddleNm' value='" & rs_contact.Fields.Item("MiddleNm").Value) & "'></td></tr>"
remove the end ) after the word Value
Avatar of Aleks

ASKER

Seems like this code worked. What do you think ?  Simple the only problem is I have to repeat the first part of the code. not a big deal but still.

               <tr>
                 <td>Middle name</td>
                 <td>
                   <div class="col-sm-12">
                     <input name="MiddleNm" type="text" class="form-control" id="MiddleNm" form="form1" value="<%=(rs_contact.Fields.Item("MiddleNm").Value)%>" maxlength="30">
                   </div>
               </td>
			 </tr>

Open in new window

what do you mean you have to do a repeat?
Avatar of Aleks

ASKER

This part of the code after the first 'else' . Since I can't combine both IFs

              <tr>
                 <td>Middle name</td>
                 <td>
                   <div class="col-sm-12">
                     <input name="MiddleNm" type="text" class="form-control" id="MiddleNm" form="form1" value="<%=(rs_contact.Fields.Item("MiddleNm").Value)%>" maxlength="30">
                   </div>
               </td>
			 </tr>

Open in new window

Avatar of Aleks

ASKER

The code I posted last works, so I will just go with that.
Avatar of Aleks

ASKER

Closest solution
sorry I didnt get back to you sooner, that whole job thing sometimes gets in the way :)

glad you got the solution you needed!