Print Page | Close Window

Looping, checking fields for zero-length

Printed From: Web Wiz Forums
Category: General Discussion
Forum Name: Classic ASP Discussion
Forum Description: Discussion on Active Server Pages (Classic ASP).
URL: https://forums.webwiz.net/forum_posts.asp?TID=5278
Printed Date: 30 March 2026 at 11:05pm
Software Version: Web Wiz Forums 12.08 - https://www.webwizforums.com


Topic: Looping, checking fields for zero-length
Posted By: spiderbaby
Subject: Looping, checking fields for zero-length
Date Posted: 25 August 2003 at 2:31pm

I've created an Access table with, among other fields, a set of numbered text fields.  Follow1, Follow2, Follow3, etc., all the way to Follow10.  They're non-required, and have no default value.

Now what I need to do is loop through each field and display the contents of each field, but first I need to check to make sure it's not empty.  When the loop finds the first empty field, it ends and no more code is executed.

I'm having difficulty with the looping logic, though.  As it stands now, the script will only execute the if-then code representing a non-empty field, meaning it's not recognizing the empty fields as actually being empty.  The script is attempting to display 5 fields each time, regardless of whether there is content or not.  If all the text fields are blank, it will display 5 line breaks.  I don't know why this is happening.

The way I've set it up is through an array declaration at the beginning, with each element matched up with the corresponding text field.  I then use a for-next loop to run through the array and check each element to make sure it's not empty. 

Here's the Array declaration:

<%
Query = "SELECT * FROM itemlog WHERE num = " & Request("num")
Set DataConn = Server.CreateObject("ADODB.Connection")
DataConn.Open "p2k2"
Set RSlist = Server.CreateObject("ADODB.recordset")
RSlist.Open Query,DataConn,1,2
exist = "true"
Dim thenotes(10)
  thenotes(0) = RSlist("follow1")
  thenotes(1) = RSlist("follow2")
  thenotes(2) = RSlist("follow3")
  thenotes(3) = RSlist("follow4")
  thenotes(4) = RSlist("follow5")
  thenotes(5) = RSlist("follow6")
  thenotes(6) = RSlist("follow7")
  thenotes(7) = RSlist("follow8")
  thenotes(8) = RSlist("follow9")
  thenotes(9) = RSlist("follow10")

%>

And here's the loop:

<%Do Until exist = "false" Or RSlist.EOF  
   For i = 0 to 9                         
     if thenotes(i) = "" then             
       exist = "false"                    
     else                                 
       Response.Write thenotes(i)         
       Response.Write "<BR><BR>"
       i = i+1                            
     End If                               
   Next                                   
RSlist.Movenext                           
Loop%>                                    

Any ideas?  Any/all help is appreciated.




Replies:
Posted By: Flamewave
Date Posted: 25 August 2003 at 4:02pm

Query = "SELECT * FROM itemlog WHERE num = " & Request("num")
Set DataConn = Server.CreateObject("ADODB.Connection")
DataConn.Open "p2k2"
Set RSlist = Server.CreateObject("ADODB.recordset")
RSlist.Open Query,DataConn,1,2
exist = true
Dim thenotes(10)
  thenotes(0) = RSlist("follow1")
  thenotes(1) = RSlist("follow2")
  thenotes(2) = RSlist("follow3")
  thenotes(3) = RSlist("follow4")
  thenotes(4) = RSlist("follow5")
  thenotes(5) = RSlist("follow6")
  thenotes(6) = RSlist("follow7")
  thenotes(7) = RSlist("follow8")
  thenotes(8) = RSlist("follow9")
  thenotes(9) = RSlist("follow10")

Do Until not exist Or RSlist.EOF  
   For i = 0 to 9
     if exist = false then exit for
                          
     if thenotes(i) = "" then             
       exist = false                     
     else                                 
       Response.Write thenotes(i)         
       Response.Write "<BR><BR>"                             
     End If                               
   Next                                   
RSlist.Movenext                           
Loop



-------------
- Flamewave

They say the grass is greener on the other side, but if you really think about it, the grass is greener on both sides.


Posted By: spiderbaby
Date Posted: 26 August 2003 at 7:45am

Thanks for the reply, Flamewave, but unfortunately the problem is still occurring.  Each time this script runs, it executes this

     else                                 
       Response.Write thenotes(i)         
       Response.Write "<BR><BR>" 

5 times.  I've no idea why only 5 times, but that's what is happening.  It's just simply not seeing the fields as being blank.

Any other ideas?



Posted By: MorningZ
Date Posted: 26 August 2003 at 8:17am

while i dont understand what you are trying to do or why......

you have to realize that "NULL" isn't the same as "empty string", so there is two ways to fix this

1) Change the SQL statement so that NULL is empty string
Query = "SELECT * FROM itemlog WHERE num = " & Request("num")
Actually, forget that is would require a lot more code, but you would have: "SELECT IsNull(follow1,''), IsNull(follow2,'')....   etc"

2) Change your IF..THEN check to check for NULL or empty string

Do Until not exist Or RSlist.EOF  
   For i = 0 to 9
     if exist = false then exit for
                          
     if Cstr("" & thenotes(i)) = "" then      'Basically turns NULL into ""
       exist = false                     
     else                                 
       Response.Write thenotes(i)         
       Response.Write "<BR><BR>"                             
     End If                               
   Next                                   
RSlist.Movenext                           
Loop

I still don't totally get why you waste the time/memory/code to put that stuff in an array at first, but maybe the above will help



-------------
Contribute to the working anarchy we fondly call the Internet


Posted By: spiderbaby
Date Posted: 26 August 2003 at 8:58am

Thank you MorningZ.  That seems to have fixed the problem.  I appreciate the help!

To clarify what it is I'm trying to do, I'm setting up a generic work-order system for a group of coworkers.  They just want to be able to assign jobs to each other, add notes to/for open assignments, update the notes, etc.  They said they'd need no more than 5 fields for notes, but I gave them 10 just in case.

This particular code is from the script that displays the notes that have been entered for a given assignment.  I put the elements into an array (noticed you were curious why I did this) so that it would be easier to step through each one in a for loop to check for existing or null values.  At least, that was my thinking.  But I'd be interested to know if you have any ideas on how I could've accomplished this more efficiently.  :)



Posted By: vshriniwasan
Date Posted: 28 August 2003 at 11:44am

I don't know if this will help... try this...


If RSlist.eof or RSlist.bof then
  Response.Write ("&nbsp;")
Else

Do while not RSlist.eof
   For i = 0 to 9
      If Trim(filename or array(i)) = "" then
         'do nothing
      Else
          Response.Write thenotes(i)         
          Response.Write "<BR><BR>"                             
      End If                                
   Next                                   
RSlist.Movenext                           
Loop
End If




Print Page | Close Window

Forum Software by Web Wiz Forums® version 12.08 - https://www.webwizforums.com
Copyright ©2001-2026 Web Wiz Ltd. - https://www.webwiz.net