Solved

Dcount and Dlookup

Posted on 2013-06-08
10
438 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
 

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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Familiarize people with the process of utilizing SQL Server functions 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 Ac…

758 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

22 Experts available now in Live!

Get 1:1 Help Now