NULL value in database means no storage in the cell, if you use ADO to query the data and you would like to filter the NULL value, it will be a challenge for a lot of programmers.
Let's see the following code:-
dim rs, strSQL
strSQL = "SELECT EmployeeId, ReportsTo FROM Employees"
Set rs = CreateObject("ADODB.Recordset")
rs.cursorlocation = 3 ' -- adUseClient
rs.Open strSQL, connobj ' -- Assume the connobj already has open connection
do while not rs.eof
rs.movenext
loop
rs.close
The code above is retreiving data from MS SQL Server Northwind database, it tries to get the employeeId and superior employee Id (reportsTo column).
Let's say the requirement is those employee without superior display in one panel and those employee with superior display in another panel.
I believe no one want to query the database two times by using SQL query "SELECT EmployeeId, ReportsTo From Employees WHERE ReportsTo IS NULL" and "SELECT EmployeeId, ReportsTo From Employees WHERE ReportsTo IS NOT NULL", so we will query one time from the database by re-using the above code.
You will add another code before the while loop:-
rs.filter = "ReportsTo IS NULL"
Immediately you will get this error:-
Arguments are of the wrong type, are out of acceptable range, or are in conflict with one another
The reason being is because ADO Filter does not support "IS" and "NULL" statement, even you tried to use the filter as ISNULL(ReportsTo, 'N/A') = 'N/A' also get the same problem.
The only solution to this is back to the SQL query, change the query as
SELECT EmployeeId, ISNULL(ReportsTo, 'N/A') FROM Employees
Then the rs.filter use ReportsTo = 'N/A' will solve the problem, the whole code is shown below:-
dim rs, strSQL
strSQL = "SELECT EmployeeId, ISNULL(ReportsTo, 'N/A') FROM Employees"
Set rs = CreateObject("ADODB.Recordset")
rs.cursorlocation = 3 ' -- adUseClient
rs.Open strSQL, connobj ' -- Assume the connobj already has open connection
rs.Filter = "ReportsTo = 'N/A'"
do while not rs.eof
' -- Display Data in Superior Panel
rs.movenext
loop
rs.Filter = "ReportsTo <> 'N/A'"
do while not rs.eof
' -- Display Data in Subordinate Panel
rs.movenext
loop
rs.close
Cheers!
Subscribe to:
Post Comments (Atom)
8 comments:
Hey, you have a great blog here! You really are very talented and deserve an honest compliment, congradulations!
Now you can join the brand new FREE ADVERTISING BLOG. It is being flooded
by thousands. I am getting exellent results with just a small FREE ad. You can
too and its FREE for life.
Click here at: multi level methods site/blog. It pretty much covers multi level methods related stuff, and more.
Ho, Ho, Ho, The HOLIDAYS are here and it is that time of year to really appreciate the people you love and the friends who are there for you when you are in need. May your life be truly blessed and may you live long and prosper. Best wishes- from me to you, Happy HOLIDAYS...
you have a great blog here! You really are very talented and deserve an honest compliment, congradulations! I'm definitely going to bookmark you!
I have a fantastic site/blog. It pretty much covers fantastic related stuff.
Come and check it out if you get time, Scott :-)
NOW, here it is... FREE advertising, No cost to you!!!
Make Money and get FREE advertising! What a great program for the HOLIDAYS... Check this out NOW for FREE!!!
This opportunity says:
"Your Ad" Will Instantly Be Displayed on Thousands of Websites and Read By over 10 Million People Per Week For FREE, and It Only Takes 60 Seconds to Get Started!
To find out more visit: multi level marketing site. It successfully exposes FREE information covering Traffic and multi level marketing related stuff.Don't
forget, FREE, FREE, FREE. You have nothing to loose!!!
Hey, you have a great blog here! You really are very talented and deserve an honest compliment, congradulations! I'm definitely going to bookmark you!
I have a money site/blog. It pretty much covers money related stuff.
Come and check it out if you get time, Scott :-)
Here it is... FREE advertising, Free download, No cost to you!!!
Get your FREE download NOW! Make Money and get FREE advertising! Here is a great program for the holidays... Check this out NOW for FREE!!!
To find out more visit: sign up for free site. It successfully exposes FREE information covering Traffic and sign up for free related stuff.Don't
forget, FREE, FREE, FREE. You have nothing to loose!!!
NG,
Just wanted to let you know that your ADO Filter tip helped me in my project that I'm working on. Keep up the good work.
Patrick Whitson
"Are you good enough to go to Heaven?"
http://www.needgod.com
http://www.spotbestdeal.com
As soon as you set up your online business, you would definitely come across the challenge of surviving in a quickly growing competitive environment. Moreover, the ever-changing market trends would also pose challenges to you and at such a situation, efficient ecommerce solution may provide you the best business advantage that you can ever dream of. Come in touch with http://www.infyecommercesolution.com and avail of the best ecommerce solutions.
Thanks A Lot
this has perfectly solved my question and my problems!!!!
it's to hard find something useful as this!!!!
Post a Comment