Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

check if empty table

Status
Not open for further replies.

avivit

Technical User
Jul 5, 2000
456
IL
I have a table with 1 field and it can sometimes be empty.
I want to be able to check in asp page (vbscript) if the
table is empty or not, before any other progress.
How can I do that?
Thanks
 
*grumble* let me regear my thinkin' back to VBS

I assume it's a one record one field table (some sort of sys stats table)

Will the field potentially contain a Null, an empty string or a zero?

set oRst = oCnn.OpenRecordset("select * from table1")
if (IsNull(oRst.Fields(0)) then ... or
if (oRst.Fields(0) = "") then ... or
if (not oRst.Fields(0)) then

that sort of thing? (doing VB from memory, hope I got it right) Ben
+61 403 395 052
 
avivit, this is what I usually do when creating my pages. Just below my recordset I redirect my page to an empty one.

Dim myRS
set myRS=server.createobject("ADODB.recordset")
myRS.open sql, "DSN=db1"
if myRS.EOF then
response.redirect
"emptytable.asp"
end if
QUOTE OF THE DAY
Don't waste time telling people what you are doing or what you are going to do. Results have a way of informing the world.

<%
Jr Clown :eek:)
%>
 
Thanks guys.
Well, actually it can contain few records or not AT ALL.
So.which one should I use?
 
your choice on this one. I'm not familiar with JavaScript at all so, I don't know how it tell you the table is empty. The script I gave you above will always re-direct you to customized page that tell you the table is empty everytime.

Just in case you don't know, I forgot to remind you to loop thru your table to check if empty


<%
Do While Not RSdept.EOF
%>


<tr>
<td width=&quot;13%&quot; align=&quot;left&quot; height=&quot;4&quot;><font size=&quot;2&quot; color=&quot;#000080&quot;><%=myRS(&quot;name&quot;)%></font></td>


<%
RSdept.MoveNext
LOOP
%>


</table>




QUOTE OF THE DAY
The only ideas that will work for you are the ones you put to work.







<%
Jr Clown :eek:)
%>
 

Never mind on my JavaScrip comment, I failed to realized Qaroven was speaking VBScript. QUOTE OF THE DAY
The only ideas that will work for you are the ones you put to work.
<%
Jr Clown
:eek:)
%>
 

Never mind on my JavaScript comment, I failed to realized Qaroven was speaking VBScript. QUOTE OF THE DAY
The only ideas that will work for you are the ones you put to work.
<%
Jr Clown
:eek:)
%>
 
The way to check to see if a recordset you selected is empty is:
Code:
if (rs.eof and rs.bof)

Both will be true if the recordset is empty.

good luck!:)
Paul Prewett
 
JrClown, thanks for the effort. I just did not understand what u were saying in your first msg. Now I do, I used it (the idea) and it's working great.

I used the following line to verify I have any record filled out:

if not rs2.EOF then....

That's all I needed. Thanks much

Link9 - Your way is logically and interesting. But why do I
need to check BOF? Isn't it enough if it's EOF?
I just verified it's not EOF, and than go on with my checkings (rs.moveforst etc...).
If it is EOF, I do nothing. Pls explain if I missed something.
Thanks
 
Well, it's just the convention. It will work every time to see if the recordset is empty. It's what's provided to us by ADO to check for that very thing.

Let's say that for some strange reason, you have 200 records, but by some stroke (or more likely, a mis-stroke), you have moved the recordset to the last record...

Well, then your recordset would be at the .eof, BUT it's not empty.

Every time, no exceptions, the .eof and .bof will both be true if the recordset is empty.

True, for looping, you should use the while not rs.eof, but it's good programming practice to check for the other first, and then I usually even put in the following bit of code (especially in 'trusted components') before I do the while not .eof loop:
Code:
if not (rs.eof and rs.bof) then
  rs.movefirst
end if

It will save you the trouble of an error message at some point in the future... guaranteed ;-)

 
I got it. In this case, I also took care of that option but
using other way:

if not rs.EOF then
rs.moveFirst
do until rs.eof
if not IsNull(rs.fields(&quot;fieldname&quot;)) then
'things I wanna do
end if
rs.moveNext
loop
end if

Your way is still interesting.
Thanks for your explanation.
 

Great :) QUOTE OF THE DAY
The only ideas that will work for you are the ones you put to work.
<%
Jr Clown
:eek:)
%>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top