If then statement if record exists

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rfiscus
    New Member
    • Apr 2012
    • 14

    If then statement if record exists

    So this code works great as long as there is a record otherwise it is just blank, I have tried several if then statements but I can't seem to make it work. How can I if then this statement?:

    Code:
    <%		
    	Set oRs=Server.CreateObject("adodb.recordset")
    	strSQL = "SELECT TOP 1 tag, time, round(value, 3) as value FROM pi.piarchive..picomp2 where tag = 'C4_4AQ10P01' and time > DATEADD(DAY, -15, GETDATE()) and value is not null order by time desc"
    	oRs.Open strSQL, conn		
    		
    	Do while not oRs.EOF
    	Response.Write "<td align=""center""><input type=""text"" id=""jtd2nox"" value= '" & oRs ("value") & "' name=""jtd2nox"" class=""inputtext1""/></td>"
    	oRs.MoveNext 
    	loop		
    	%>
  • jhardman
    Recognized Expert Specialist
    • Jan 2007
    • 3405

    #2
    The if statement you are looking for is
    Code:
    if oRs.eof
    this returns true if there are no records, or if you have looped through all the records and reached the end. Try putting your if statement right before the do loop.

    Jared

    Comment

    • rfiscus
      New Member
      • Apr 2012
      • 14

      #3
      This seems to create two input boxes then, first a blank one and then one populated with 'test'. Am I using the if statement incorrectly?

      Code:
      if oRs.eof then
      	Response.Write "<td align=""center""><input type=""text"" id=""ows1nox"" value= 'test' name=""ows1nox"" class=""inputtext1""/></td>"
      	Else
      	Do while not oRs.EOF
      	Response.Write "<td align=""center""><input type=""text"" id=""ows1nox"" value= '" & oRs ("value") & "' name=""ows1nox"" class=""inputtext1""/></td>"
      	oRs.MoveNext 
      	loop
      	End If

      Comment

      • rfiscus
        New Member
        • Apr 2012
        • 14

        #4
        Disregard, I had an extra line of code from troubleshooting , this worked great, thank you.

        Originally posted by rfiscus
        This seems to create two input boxes then, first a blank one and then one populated with 'test'. Am I using the if statement incorrectly?

        Code:
        if oRs.eof then
        	Response.Write "<td align=""center""><input type=""text"" id=""ows1nox"" value= 'test' name=""ows1nox"" class=""inputtext1""/></td>"
        	Else
        	Do while not oRs.EOF
        	Response.Write "<td align=""center""><input type=""text"" id=""ows1nox"" value= '" & oRs ("value") & "' name=""ows1nox"" class=""inputtext1""/></td>"
        	oRs.MoveNext 
        	loop
        	End If

        Comment

        • jhardman
          Recognized Expert Specialist
          • Jan 2007
          • 3405

          #5
          Glad you got it working. Let me make two comments on your code.
          1- whenever you have a long section of html to write, it might make sense to end the asp code and restart it after you are done like this:
          Code:
          if oRs.eof then %>
          <Td align="center"><input type="text">
          <%
          End if
          Notice that I don't have to worry about escaping my quote marks. I find this is easier than using "response.write " if the code is longer than just a line.
          2- the do loop is already conditional. It will be skipped if there are no records, so you don't need an else clause. Just if - end if.

          Jared

          Comment

          • rfiscus
            New Member
            • Apr 2012
            • 14

            #6
            Cool on not needing the Else clause. If I am writing a lot of HTML I do end my asp code instead of using response.write, sometimes if it is just one line it is easier just to escape it out. Thanks for your input.

            Comment

            • Fary4u
              Contributor
              • Jul 2007
              • 273

              #7
              or u can try different one too. as advance check.
              Code:
              If not oRs.eof Then

              Comment

              Working...