Solved

Dcount and Dlookup

Posted on 2013-06-08
10
443 Views
Last Modified: 2013-06-11
Experts,

I am trying to add a Dlookup to my Dcount.  I dont have a syntax but it doesnt do what I want it to do, which is turn forecolor red if both the Dcount and Dlookup are True.  Maybe there is a better more efficient way to accomplish.   The Dcount part works as I planned however the Dlookup part is the problem.  

Private Sub Form_Current()

If Nz(DCount("ProjID", "tblFX", "ProjID= " & Nz(Me!ID.Value, 0)), 0) > 0 And DLookup("[HedgingStrategy]", "tblFX", "[ProjID] = " & Nz(Me!ID.Value, 0)) <> 3 Or 6 Then 
        Me!cmdFX.ForeColor = vbRed
        Me!cmdFX.FontBold = True
        Me!BoxStatusFX.Visible = True
      Else
        Me!cmdFX.ForeColor = vbBlack   'of the current record of the form
        Me!cmdFX.FontBold = False
        Me!BoxStatusFX.Visible = False
      End If

end sub

Open in new window

0
Comment
Question by:pdvsa
10 Comments
 
LVL 77

Accepted Solution

by:
peter57r earned 500 total points
ID: 39231817
If Nz(DCount("ProjID", "tblFX", "ProjID= " & Nz(Me!ID.Value, 0)), 0) > 0 And
 DLookup("[HedgingStrategy]", "tblFX", "[ProjID] = " & Nz(Me!ID.Value, 0)) <> 3
And DLookup("[HedgingStrategy]", "tblFX", "[ProjID] = " & Nz(Me!ID.Value, 0))<>6 Then
0
 
LVL 57
ID: 39231818
Dim varReturnValue as Variant

varReturnValue = NZ(DLookup("[HedgingStrategy]", "tblFX", "[ProjID] = " & Nz(Me!ID.Value, 0)),0)

' Check if records found and what was found is something other then strategy 3 or 6
If varReturnValue<>0 and varReturnValue <>3 and varReturnValue <>6 Then
        Me!cmdFX.ForeColor = vbRed
        Me!cmdFX.FontBold = True
        Me!BoxStatusFX.Visible = True
      Else
        Me!cmdFX.ForeColor = vbBlack   'of the current record of the form
        Me!cmdFX.FontBold = False
        Me!BoxStatusFX.Visible = False
      End If

end sub

Jim.
0
 
LVL 61

Expert Comment

by:mbizup
ID: 39231819
Try this... your OR syntax is not quite right:


Private Sub Form_Current()

Dim intLookup

intLookup = NZ(DLookup("[HedgingStrategy]", "tblFX", "[ProjID] = " & Nz(Me!ID.Value, 0)),0) 
If Nz(DCount("ProjID", "tblFX", "ProjID= " & Nz(Me!ID.Value, 0)), 0) > 0 And intLookup <> 3 And intLookup <>  6 Then 
        Me!cmdFX.ForeColor = vbRed
        Me!cmdFX.FontBold = True
        Me!BoxStatusFX.Visible = True
      Else
        Me!cmdFX.ForeColor = vbBlack   'of the current record of the form
        Me!cmdFX.FontBold = False
        Me!BoxStatusFX.Visible = False
      End If

end sub

Open in new window

0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 

Author Comment

by:pdvsa
ID: 39231930
thank you for the responses.  

Mbizup's did get the box to turn vbRed but the box turned vbRed for ALL (meaning turned on for 3 or 6 as well as all others that <> 3 or 6)

both Peter's and Jim's did not turn on the box vbRed meaning no vbRed for any.

I tested quite extensively.

let me know what you think is next.  I might be off computer for couple hours.
0
 
LVL 57
ID: 39231997
I think you need to tell us (in english), what the check is supposed to be.

There are records and/or HedgingStrategy is what?

 Really can't tell if the condition logic is correct or not from what you've posted.

Jim.
0
 

Author Comment

by:pdvsa
ID: 39232386
Ok.  I will probably tomorrow.   I have been busy today with my "real job".  Thank you
0
 
LVL 57
ID: 39234556
Please note that by saying "in english", I meant simply not using code.   I was not implying that you were being confusing.

It's just difficult to tell from the code you provided what you intent is and it would be very helpful if it was expressed in a non-code manor.

I do this myself many times when I have difficulty in writing some logic.   I list everything out I need in English, then convert to code one piece at a time.

 That helps you see certain things.   For example, I'd be very suprised if you'll end up needing both the DCount() and DLookup() given that they are both looking at the same table and for the same record.

 The Dcount() is simply telling you if the record exists or not (that's what I believe you were trying to check for), but you get that from the Dlookup() because if it doesn't find one, you'll get a null value back.

  So you'll either have a null (no record) or a non-null (a record with a value that needs to be checked).

  But I'm not 100% sure of the test(s) that your actually trying to make.
Jim.
0
 

Author Comment

by:pdvsa
ID: 39234581
ok thank you.  I have to set aside a little time to check this.  It is not at the top of my list at the moment.  Thanksl for the information...  I might not need both then...
0
 

Author Closing Comment

by:pdvsa
ID: 39236516
Well, I see part of my issue.  I wont tell you what it was because you will laugh at me.  Peter's initial response was correct.  

Anyways, I think I can remove the Dcount as Jim mentioned and leave just the Dlookup but I need to add criteria NOT LIKE "No Risk*" and do away with <>3 and <>6

The below is NOT CORRECT:
If DLookup("[HedgingStrategy]", "tblFX_2", "[ProjID] = " & Nz(Me!ID.Value, 0)) and HedgingStrategy NOT LIKE "No Risk*"  then
0
 

Author Comment

by:pdvsa
ID: 39238115
I tested all answers and all were correct (Jim's, Mbizups as well as the initial response by Peter (who I awarded pts to).
0

Featured Post

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Most if not all databases provide tools to filter data; even simple mail-merge programs might offer basic filtering capabilities. This is so important that, although Access has many built-in features to help the user in this task, developers often n…
It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

832 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