Solved

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

Posted on 2016-11-06
18
26 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 119

Expert Comment

by:Rey Obrero
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
 

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 119

Expert Comment

by:Rey Obrero
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 119

Expert Comment

by:Rey Obrero
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 119

Accepted Solution

by:
Rey Obrero 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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 119

Expert Comment

by:Rey Obrero
ID: 41876541
it will read all records that satisfy both criteria.
0
 
LVL 119

Expert Comment

by:Rey Obrero
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 119

Expert Comment

by:Rey Obrero
ID: 41876548
that is correct.
0
 

Author Comment

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

Expert Comment

by:Rey Obrero
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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

Suggested Solutions

When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
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 …

746 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now