DAO...testing for empty query results?

mrb783

Registered User.
Local time
Today, 16:08
Joined
Oct 28, 2008
Messages
40
Hello all, so here's an example of what I'm doing. I have a sequence of nested tables with "deletion requests" to allow database admins to process deletions. Anyway, I don't want the admins to erroneously delete all related children if it wasn't the intention of the deletion requestor, so I decided to add in a handy message that indicates how many children records (if any) are tied to the record being deleted.

The issue I am experiencing stems from the fact that DAO will always return a row, even if the query is empty. I have tested this using the access SQL builder with the same query and it comes back empty when it should, whereas the DAO will always return 1 row given the same data. Here is an example of the query:

Code:
SQL = "SELECT * FROM (" & _
"SELECT tbl_CustEvidenceIDQuery.EvidenceID, '' AS LogID " & _
"FROM tbl_CustEvidenceIDQuery " & _
"WHERE (tbl_CustEvidenceIDQuery.CustodianID = " & Me.CustodianID.Value & _
" AND tbl_CustEvidenceIDQuery.CustodianID is not null)" & _
" UNION " & _
"SELECT '' AS EvidenceID, tbl_CustSafeInventoryIDQuery.LogID " & _
"FROM tbl_CustSafeInventoryIDQuery " & _
"WHERE (tbl_CustSafeInventoryIDQuery.CustodianID = " & Me.CustodianID.Value & _
" AND tbl_CustSafeInventoryIDQuery.LogID is not null) " & ")"
And here it is again in Access SQL format:

Code:
SELECT * FROM (

SELECT tbl_CustEvidenceIDQuery.EvidenceID, '' AS LogID
FROM tbl_CustEvidenceIDQuery
WHERE (tbl_CustEvidenceIDQuery.CustodianID = [Enter ID]
AND tbl_CustEvidenceIDQuery.EvidenceID is not null)

UNION

SELECT '' AS EvidenceID, tbl_CustSafeInventoryIDQuery.LogID
FROM tbl_CustSafeInventoryIDQuery
WHERE (tbl_CustSafeInventoryIDQuery.CustodianID = [Enter ID]
AND tbl_CustSafeInventoryIDQuery.LogID is not null)
);
Now, if there is 1 child record it will return 1 as the RecordSetVar.RecordCount total. However, it will also do this if the query is empty (no child records). If there are more than 1 child records, it works as intended...so it's only the instance where the query should be empty that is the problem here for me.

Also, I can't for the life of me figure out how to actually determine whether the row is null, as I'm assuming that this will be the deciding factor (if recordcount = 1 And thisrowvalue = "" then blah). I've tried using the GetRows() function to return the values to a variant, then use something like the following to extract the information:

Code:
tmp = RecordSetVar(0,0) '1st row, 1st col
if(tmp = "") then blah
But...obviously it doesn't work for me...Any ideas on a workaround?
 
The Recordset.RecordCount is 0 if there are no records returned. It sounds like you are using Access 2007.
 
Nope, I'm using Access 2003. I get a RecordCount of 1 when the normal query returns 0 records...perhaps there is something wrong with my VBA version of the query?
 
Have you applied the Office 2003 SP3 yet and if so, have you applied the HotFix too?
 
I don't follow this completely but can you do the count on each table separately? Maybe the erroneous count is a result of the union thing - ?
 
I would put the query SQL into a static query and see what returns. If the Recordset.Recount is 1 then you are getting 1 record back.
 
KenHigg: I just attempted that and it still returns 1 record.

RuralGuy: I believe I have already done that? See the code above for the exact SQL query I ran that produces 0 records. As far as I know, the two queries are identical in every way except for what calls them (VBA vs running a normal Access query).
 
I'm sorry. I lost track of the thread over time. My apologies. Are you using macros or VBA? This comment bothers me:
...whereas the DAO will always return 1 row given the same data.
Maybe if you have a test sample that you can zip up and post here we might be able to determine what is going on.
 
I wonder what happens when you hard code the [Enter ID]?
 
KenDigg: It makes no difference.

Both: Attached is the zipped, stripped version of the database you asked for. This has the ID hardcoded into the static query.

The static query is listed as Query1.
The only form in there contains the VBA code. Clicking the "trashcan" button will execute the code. Let me know if you have any further questions.

Also, thank you guys for assisting on this, it's been baffling me.
 

Attachments

I'll let RG tinker with this. If he can't do anything with it I doubt I can...
 
It might take a bit to figure out what is going on. I'll post back.
 
Sounds good. Look forward to the reply. Thanks much!
 
My query skills leave a lot to be desired but the first thing I did was *exacty* copy the Query1 SQL to the code you were executing with SQL = "" ... It returns a Recordset.RecordCount of 0. Obviously the two versions of the SQL are not the same.
 
Huh...well...that worked...I don't get it. It must have been my spacing with the SQL text creation. It works now (granted, I can't use the rs.Movelast command though). Thanks a bunch! Oh, and here's the updated code that corrected the issue.

Code:
SQL = "SELECT * FROM (" & _
" SELECT tbl_CustEvidenceIDQuery.EvidenceID, '' AS LogID" & _
" FROM tbl_CustEvidenceIDQuery" & _
" WHERE (tbl_CustEvidenceIDQuery.CustodianID = " & Me.CustodianID.Value & _
" AND tbl_CustEvidenceIDQuery.EvidenceID is not null)" & _
" UNION" & _
" SELECT '' AS EvidenceID, tbl_CustSafeInventoryIDQuery.LogID" & _
" FROM tbl_CustSafeInventoryIDQuery" & _
" WHERE (tbl_CustSafeInventoryIDQuery.CustodianID = " & Me.CustodianID.Value & _
" AND tbl_CustSafeInventoryIDQuery.LogID is not null))"
 
As you know, the DAO Recordset.RecordCount is not necessarily accurate until you do a .MoveLast *unless* the RecordCount is 0. So test for <> 0 before executing a .MoveLast. The RecordCount may not even be a positive number in the case of a linked table so just do the <> 0 test 1st.
 

Users who are viewing this thread

Back
Top Bottom