Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Ms Access Dcount Help Please! Saying 0 when it is more than 0

Posted on 2016-11-06
18
36 Views
Last Modified: 2016-11-06
Here is my code. They both are above 0 in count but:

Doesn't work:
If DCount("[LocID]", "[ProdLocations]", "[LocID] =" & Me.ProdLocLocID) > 0 Then

Open in new window



Does Work:
If DCount("[ProductID]", "[ProdLocations]", "[ProductID] =" & Me.ProductID) > 0 Then

Open in new window


Whole Code:
If DCount("[ProductID]", "[ProdLocations]", "[ProductID] =" & Me.ProductID) > 0 Then
 If DCount("[LocID]", "[ProdLocations]", "[LocID] =" & Me.ProdLocLocID) > 0 Then
        MsgBox ("IT EXISTS!")
        Else
        MsgBox ("Nothing!")
End If
End If

Open in new window



Please help! LocID and ProductID are both numeric. Sometimes the record count may only be 1.

Thanks!
0
Comment
Question by:Dustin Stanley
  • 10
  • 8
18 Comments
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 41876474
try this

If DCount("*", "ProdLocations", "[LocID] =" & Me.ProdLocLocID) > 0 Then
0
 

Author Comment

by:Dustin Stanley
ID: 41876492
Still says Nothing Rey. It works without the criteria. As in it returns things.
0
 

Author Comment

by:Dustin Stanley
ID: 41876503
YES I figured it out! I thought I had this set up on LocID when it was supposed to be ProdLocID


This Code works:

Private Sub Command35_Click()
'If DCount("[ProductID]", "[ProdLocations]", "[ProductID] =" & Me.ProductID) > 0 Then
 If DCount("[ProdLocID]", "[ProdLocations]", "[ProdLocID] =" & Me.ProdLocLocID) > 0 Then
        MsgBox ("IT EXISTS!")
        Else
        MsgBox ("Nothing!")
End If
'End If
End Sub

Open in new window

0
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 

Author Comment

by:Dustin Stanley
ID: 41876509
How can I combine these dcounts. I need the record to be >0 for both at the same time.
Exists
Else
 Nothing
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 41876513
you can "AND" the dcount

Private Sub Command35_Click()
If DCount("[ProductID]", "[ProdLocations]", "[ProductID] =" & Me.ProductID) > 0 AND  If DCount("[ProdLocID]", "[ProdLocations]", "[ProdLocID] =" & Me.ProdLocLocID) > 0 Then
        MsgBox ("IT EXISTS!")
        Else
        MsgBox ("Nothing!")
End If
End Sub

Open in new window

0
 

Author Comment

by:Dustin Stanley
ID: 41876515
I tried that and there is a expected expression on the second AND  If
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 41876516
oops, remove the second "If"

Private Sub Command35_Click()
If DCount("[ProductID]", "[ProdLocations]", "[ProductID] =" & Me.ProductID) > 0 AND DCount("[ProdLocID]", "[ProdLocations]", "[ProdLocID] =" & Me.ProdLocLocID) > 0 Then
        MsgBox ("IT EXISTS!")
        Else
        MsgBox ("Nothing!")
End If
End Sub

Open in new window

0
 
LVL 120

Accepted Solution

by:
Rey Obrero (Capricorn1) earned 500 total points
ID: 41876519
try this too

Private Sub Command35_Click()
If DCount("*", "[ProdLocations]", "[ProductID] =" & Me.ProductID & " AND [ProdLocID] =" & Me.ProdLocLocID) > 0 Then
        MsgBox ("IT EXISTS!")
        Else
        MsgBox ("Nothing!")
End If
End Sub

Open in new window

0
 

Author Comment

by:Dustin Stanley
ID: 41876534
Thank you but now it says everything exists. Is it reading a single record for both criteria? It seems like it is reading from all records in the table.
0
 

Author Comment

by:Dustin Stanley
ID: 41876540
Sorry I didn't see the second one. But the first one says everything exists and the second one says nothing.
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 41876541
it will read all records that satisfy both criteria.
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 41876543
use the last code I posted.
0
 

Author Comment

by:Dustin Stanley
ID: 41876547
So if in my table there is no single record that match both criteria then it says Nothing correct?

The last code says Nothing!
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 41876548
that is correct.
0
 

Author Comment

by:Dustin Stanley
ID: 41876553
Why and how is the wildcard * used here?
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 41876560
why?  that is the way the function dcount() operates.

if you use the * ,the DCount function calculates the total number of records, including those that contain Null fields.

for more detailed explanation, type dcount in the access help window.
0
 

Author Comment

by:Dustin Stanley
ID: 41876566
Thanks I was just reading about that.

https://support.office.com/en-us/article/DCount-Function-f6b5d78b-ad0b-4e42-be7a-11a64acbf3d3

I got it work thanks Rey!
0
 

Author Closing Comment

by:Dustin Stanley
ID: 41876567
Thanks!
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone 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

Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

840 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