Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Dcount and Dlookup

Posted on 2013-06-08
10
Medium Priority
?
454 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 2000 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 58
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
Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

 

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 58
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 58
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
If you’re using QODBC to update QuickBooks data from Microsoft® Access but Access is not showing the updated data, you could have set up QODBC incorrectly.
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

824 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