Solved

Dcount and Dlookup

Posted on 2013-06-08
10
447 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
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…
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…

739 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