By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,866 Members | 2,039 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,866 IT Pros & Developers. It's quick & easy.

If then statement if record exists

P: 14
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?:

Expand|Select|Wrap|Line Numbers
  1. <%        
  2.     Set oRs=Server.CreateObject("adodb.recordset")
  3.     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"
  4.     oRs.Open strSQL, conn        
  5.  
  6.     Do while not oRs.EOF
  7.     Response.Write "<td align=""center""><input type=""text"" id=""jtd2nox"" value= '" & oRs ("value") & "' name=""jtd2nox"" class=""inputtext1""/></td>"
  8.     oRs.MoveNext 
  9.     loop        
  10.     %>
  11.  
Jul 26 '12 #1

✓ answered by jhardman

The if statement you are looking for is
Expand|Select|Wrap|Line Numbers
  1. 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

Share this Question
Share on Google+
6 Replies


jhardman
Expert 2.5K+
P: 3,405
The if statement you are looking for is
Expand|Select|Wrap|Line Numbers
  1. 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
Jul 26 '12 #2

P: 14
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?

Expand|Select|Wrap|Line Numbers
  1.  
  2. if oRs.eof then
  3.     Response.Write "<td align=""center""><input type=""text"" id=""ows1nox"" value= 'test' name=""ows1nox"" class=""inputtext1""/></td>"
  4.     Else
  5.     Do while not oRs.EOF
  6.     Response.Write "<td align=""center""><input type=""text"" id=""ows1nox"" value= '" & oRs ("value") & "' name=""ows1nox"" class=""inputtext1""/></td>"
  7.     oRs.MoveNext 
  8.     loop
  9.     End If
  10.  
  11.  
Jul 30 '12 #3

P: 14
Disregard, I had an extra line of code from troubleshooting, this worked great, thank you.

@rfiscus
Jul 30 '12 #4

jhardman
Expert 2.5K+
P: 3,405
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:
Expand|Select|Wrap|Line Numbers
  1. if oRs.eof then %>
  2. <Td align="center"><input type="text">
  3. <%
  4. 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
Jul 30 '12 #5

P: 14
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.
Jul 30 '12 #6

Fary4u
100+
P: 273
or u can try different one too. as advance check.
Expand|Select|Wrap|Line Numbers
  1. If not oRs.eof Then
Aug 2 '12 #7

Post your reply

Sign in to post your reply or Sign up for a free account.