Web Wiz - Green Windows Web Hosting

  New Posts New Posts RSS Feed - Looping, checking fields for zero-length
  FAQ FAQ  Forum Search   Events   Register Register  Login Login

Looping, checking fields for zero-length

 Post Reply Post Reply
Author
spiderbaby View Drop Down
Newbie
Newbie
Avatar

Joined: 29 April 2003
Status: Offline
Points: 4
Post Options Post Options   Thanks (0) Thanks(0)   Quote spiderbaby Quote  Post ReplyReply Direct Link To This Post Topic: Looping, checking fields for zero-length
    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.

Back to Top
Flamewave View Drop Down
Senior Member
Senior Member
Avatar

Joined: 19 June 2002
Location: United States
Status: Offline
Points: 376
Post Options Post Options   Thanks (0) Thanks(0)   Quote Flamewave Quote  Post ReplyReply Direct Link To This Post 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.
Back to Top
spiderbaby View Drop Down
Newbie
Newbie
Avatar

Joined: 29 April 2003
Status: Offline
Points: 4
Post Options Post Options   Thanks (0) Thanks(0)   Quote spiderbaby Quote  Post ReplyReply Direct Link To This Post 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?

Back to Top
MorningZ View Drop Down
Senior Member
Senior Member
Avatar

Joined: 06 May 2002
Location: United States
Status: Offline
Points: 1793
Post Options Post Options   Thanks (0) Thanks(0)   Quote MorningZ Quote  Post ReplyReply Direct Link To This Post 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
Back to Top
spiderbaby View Drop Down
Newbie
Newbie
Avatar

Joined: 29 April 2003
Status: Offline
Points: 4
Post Options Post Options   Thanks (0) Thanks(0)   Quote spiderbaby Quote  Post ReplyReply Direct Link To This Post 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.  :)

Back to Top
vshriniwasan View Drop Down
Groupie
Groupie
Avatar

Joined: 17 December 2001
Location: United States
Status: Offline
Points: 63
Post Options Post Options   Thanks (0) Thanks(0)   Quote vshriniwasan Quote  Post ReplyReply Direct Link To This Post 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

Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down

Forum Software by Web Wiz Forums® version 12.08
Copyright ©2001-2026 Web Wiz Ltd.


Become a Fan on Facebook Follow us on X Connect with us on LinkedIn Web Wiz Blogs
About Web Wiz | Contact Web Wiz | Terms & Conditions | Cookies | Privacy Notice

Web Wiz is the trading name of Web Wiz Ltd. Company registration No. 05977755. Registered in England and Wales.
Registered office: Web Wiz Ltd, Unit 18, The Glenmore Centre, Fancy Road, Poole, Dorset, BH12 4FB, UK.

Prices exclude VAT at 20% unless otherwise stated. VAT No. GB988999105 - $, € prices shown as a guideline only.

Copyright ©2001-2026 Web Wiz Ltd. All rights reserved.