How to use Dlookup inside DCount

Posted on 2009-04-22
Medium Priority
Last Modified: 2013-11-27
Hi Experts,

I'm having two tables: tb1 and tb2
I need to count the total number of records in tb1 with the criteria that value in tb1.[field1] can be found in tb2.[myfield]

So this requires me to use a DCount function whereas the criteria would be a Dlookup function returns non-null value. But I'm kinda stuck playing around with "

Can anyone provide the code for this purpose? Let's say I don't want to use reordset or other ways of doing it.

Many thanks!
Question by:LizzJ
  • 3
  • 2
LVL 65

Expert Comment

ID: 24203996
no need for two functions, create a recordset instead
first define your sql

select count(tb1.*)  as TotalCnt
from tb1, tb2
where tb1.field1 = tbl2.myfield

you need to sort out this sql first, ensure join is right

then use it in code like this

dim rs as dao.recordset
dim lTotal as long

lTotal = 0
set rs=currentdb.openrecordset("your sql")
if rs.eof = false then
    lTotal = rs!TotalCnt
end if
set rs=nothing

Now total is in lTotal
LVL 77

Expert Comment

ID: 24204004
"value in tb1.[field1] can be found in tb2.[myfield]"
Just a bit cautious about the wording here.
Do you simply mean where tb1.[field1] = tb2.[myfield] or are you implying something different to that?


Author Comment

ID: 24204867

i've mentioned that i don't want to use recordset

what i mean is tb1.[field1] can be found in tb2.[myfield], is it the same as tb1.[field1] = tb2.[myfield] ?
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 24204904
to illustrate,

here is a example:

tb1:                tb2:
field1             myfield
   aa               aa
   bb               bb

so total count should be 2 since "cc" cannot be found in tb2.[myfield]

I DON'T want to use recordset to do the counting. Instead, I want to use just one function. Is it possible?

Thanks a lot ! Appreciate your help
LVL 65

Expert Comment

ID: 24204957
well you cannot use two tables directly in this function unless it is based on a query

so if you had your query that links the two tables together you can then use DCOUNT

I believe it was clarification on what you asked for by Peter do you want to see if it is equal or is contained within and the sql will differ for this
LVL 65

Accepted Solution

rockiroads earned 2000 total points
ID: 24205002
if you created a query like the one I posted earlier, say u call it qryProductCount then you can do this



Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows Explorer let you handle zip folders nearly as any other folder: Copy, move, change, and delete, etc. In VBA you can also handle normal files and folders, but zip folders takes a little more - and that you'll find here.
In a use case, a user needs to close an opened report by simply pressing the Escape (Esc) key. This can be done by adding macro code in Report_KeyPress or Report_KeyDown event.
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question