Free Lessons
Courses
Seminars
TechHelp
Fast Tips
Templates
Topic Index
Forum
ABCD
 
Home   Courses   Seminars   Templates   TechHelp   Fast Tips   Forums   Help   Contact   Join   Order   Logon  
 
 
Microsoft Access Developer 9
By Richard Rost   Richard Rost on Twitter Richard Rost on LinkedIn Email Richard Rost   4 years ago


Back to Microsoft Access Developer 9
 

DCount Variable vs IF DCount Upload Images   Link  
Keith Stanton 
6 months ago
Hello,

Is there a reason that I have either forgotten or we haven't been taught yet that we assign a value to a variable vs performing the DCount right in the IF statement?

Why this?

    Dim C
    C = Nz(DCount("*", "QuestionT", "TestID=" & TestCombo), 0)
    If C = 0 Then

vs this?

    If Nz(DCount("*", "QuestionT", "TestID=" & TestCombo), 0) = 0 Then

both work, curious to know if there is a reason for one option over the other.
Adam Schwanz
6 months ago
You can use whichever, using variables is usually a lot more flexible/friendly. You can save a value for later use, change the value of the variable, or use them to not have to repeat code.

Lot easier to say
C = DCOUNT
If C = 0 Then
ElseIf C=1 Then
ElseIf C=2 Then
etc...

Then to do
If Nz(DCount("*", "QuestionT", "TestID=" & TestCombo), 0) = 0 Then
ElseIf Nz(DCount("*", "QuestionT", "TestID=" & TestCombo), 0) = 1 Then
ElseIf Nz(DCount("*", "QuestionT", "TestID=" & TestCombo), 0) = 2 Then
etc.

There's also the whole running through loops, you can do C=C+1 because it can change/save the value, you can't do Nz(DCount("*", "QuestionT", "TestID=" & TestCombo), 0) = Nz(DCount("*", "QuestionT", "TestID=" & TestCombo), 0) + 1 more than once. Variables are just better ;)
Adam Schwanz
6 months ago
Actually, I don't think you can do that DCOUNT = DCOUNT at all, but you get the point.
Keith Stanton
6 months ago
ah ok, that helps a lot.  I haven't come across the need to have multiple options like that yet so I've just used it in an IF statement, but I can see how much more useful the variant option be if more than 1 option/outcome needed to be taken into consideration.
Thank You!
Richard Rost
6 months ago
Yeah, the only reason I would assign it to a variable is if I was going to use it more than once.

This thread is now closed. If you wish to comment, start a NEW discussion, below.
 


Back to Microsoft Access Developer 9 Comments
 

Start a NEW Conversation
 
Only students may post right now. Click here for more information on how you can set up an account. If you are a student, please LOG ON first.
 
Subscribe
 

You may want to read these articles from the 599CD News:

5/26/2022List Items Edit Form
5/25/2022Column Colors
5/24/2022Multi-Table Forms
5/23/2022Variables
5/21/2022Access Developer 39
5/21/2022Access Developer 39 Lessons
5/21/2022Access Developer 39
5/21/2022Select Items in Related Table
5/21/2022Clear List Box
5/21/2022Multi Select List Boxes
 

Learn
 
Access - index
Excel - index
Word - index
Windows - index
PowerPoint - index
Photoshop - index
Visual Basic - index
ASP - index
Seminars
More...
Customers
 
Login
My Account
My Courses
Lost Password
Memberships
Student Databases
Change Email
Info
 
Latest News
New Releases
User Forums
Topic Glossary
Tips & Tricks
Search The Site
Code Vault
Collapse Menus
Help
 
Customer Support
Web Site Tour
FAQs
TechHelp
Consulting Services
About
 
Background
Testimonials
Jobs
Affiliate Program
Richard Rost
Free Lessons
Mailing List
Order
 
Video Tutorials
Handbooks
Memberships
Learning Connection
Idiot's Guide to Excel
Volume Discounts
Payment Info
Shipping
Terms of Sale
Contact
 
Contact Info
Support Policy
Email Richard
Mailing Address
Phone Number
Fax Number
Course Survey
Blog RSS Feed    Twitter

YouTube Channel    LinkedIn