Link to home
Start Free TrialLog in
Avatar of pdvsa
pdvsaFlag for United States of America

asked on

Dcount and Dlookup

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

ASKER CERTIFIED SOLUTION
Avatar of peter57r
peter57r
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
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

Avatar of pdvsa

ASKER

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.
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.
Avatar of pdvsa

ASKER

Ok.  I will probably tomorrow.   I have been busy today with my "real job".  Thank you
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.
Avatar of pdvsa

ASKER

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...
Avatar of pdvsa

ASKER

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
Avatar of pdvsa

ASKER

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