473,396 Members | 1,797 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Dlookup not returning correct value

46
My dlookup is returning a 0 when it should return a value of 935. I have double check the spelling of the variable and tables. I can't seem to figure out where I'm going wrong on this on.
Expand|Select|Wrap|Line Numbers
  1. [Punch ID] = Nz(DLookup("[Punch ID]", "[Copy of Employee Work Statistics1]", "Employee='" & Forms![Royal time Stamp]![Driver] & "' And [Date]=#" & Date & "#" & " And [Trip Sign Off]=0"), 0)
Thanks for your help,
Vasago
Apr 7 '14 #1

✓ answered by Seth Schrock

Since you are doing so many DLookups on the same table, you would probably increase performance if you just opened the recordset and used the values from that. All the domain functions (those that start with D; DLookup, DCount, DSum, etc.) are very resource intensive and therefore slow down your code. You can open your recordset like this:
Expand|Select|Wrap|Line Numbers
  1. Dim db as DAO.Database
  2. Dim rst As DAO.Recordset
  3. Dim strRst as String
  4.  
  5. Set db = CurrentDb
  6. strRst = "SELECT [Punch ID] " & _
  7.          "FROM [Copy of Employee Work Statistics1] " & strCriteria
  8. Set rst = db.OpenRecordset(strRst, dbOpenDynaset)
  9.  
  10. 'you can reference your values like this
  11. [Punch ID] = rst![Punch ID]
  12.  
  13.  
  14. 'At the end of your code, close what you open 
  15. 'and release what you set
  16. rst.close
  17. set db = nothing
  18. set rst = nothing

10 3233
Vasago
46
I did have one after thought. Could it be because my tables date also has the time stamp attached to the date and how would I add in a format dates to "short date" into the dlookup
Apr 7 '14 #2
Seth Schrock
2,965 Expert 2GB
First, lets start by splitting out your criteria into its own string so that we can debug your code.
Expand|Select|Wrap|Line Numbers
  1. dim strCriteria as string
  2. strCriteria = "Employee='" & Forms![Royal time Stamp]![Driver] & "' And [Date]=#" & Date & "# And [Trip Sign Off]=0"
  3. debug.print strCriteria
  4.  
  5. [Punch ID] = Nz(DLookup("Punch ID", "Copy of Employee Work Statistics1", strCriteria), 0)
The fact that it is returning 0 and that you are using the Nz() function with 0 as the return value makes me think that your DLookup() was returning a null value. Try my code, run it and the post back what gets entered into the immediate window (Ctrl + g to view it). It is possible that the square brackets were also causing the problem, but I'm not positive of that.
Apr 7 '14 #3
Vasago
46
Here is what was in the immediate window.

"Employee='Batson, Leroy' And [Date]=#4/7/2014# And [Trip Sign Off]=0"
Apr 7 '14 #4
Vasago
46
My [Date] field that it is looking up is formatted to a Medium Date. Would that cause issues with the dlookup using the current date?
Apr 7 '14 #5
Seth Schrock
2,965 Expert 2GB
Access is usually pretty good about comparing dates since it is actually comparing the date value (an integer) not the format of the date. It is a bad idea to use reserved words as field names (such as Date). Even when put in brackets, it can sometimes cause problems.

Can you verify the data types of the Employee and [Trip Sign Off] fields that Employee is a text data type and [Trip Sign Off] is a number or yes/no field?

Try creating a query such as the following and see what errors you get or what the results are.
Expand|Select|Wrap|Line Numbers
  1. SELECT [Punch ID]
  2. FROM [Copy of Employee Work Statistics1]
  3. WHERE Employee='Batson, Leroy' 
  4.   And [Date]=#4/7/2014# 
  5.   And [Trip Sign Off]=0
Apr 7 '14 #6
Vasago
46
[Trip Sign Off] Data Type = Date/Time (formatted as short time)
[Employee] Data Type = Short Text

Creating the query listed above outputs the value I am looking for in the Dlookup.
Apr 7 '14 #7
Vasago
46
Here is all the code on the Form Load. Just in case this help out at all.

Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Load()
  2. Dim strCriteria As String
  3.  
  4. strCriteria = "Employee='" & Forms![Royal time Stamp]![Driver] & "' And [Date]=#" & Date & "# And [Trip Sign Off]=0"
  5.  
  6. Debug.Print strCriteria
  7.  
  8. [Punch ID] = Nz(DLookup("Punch ID", "Copy of Employee Work Statistics1", strCriteria), 0)
  9.  
  10. [Type of Work] = Nz(DLookup("[Type of Work]", "[Copy of Employee Work Statistics1]", strCriteria), 0)
  11. If [Type of Work] = "" Then
  12.  
  13. Else
  14. Me.[Type of Work].Locked = True
  15. [Trip_S_on] = Nz(DLookup("[Trip Sign On]", "[Copy of Employee Work Statistics1]", strCriteria), 0)
  16. If [Type of Work] = "Transportation Specialist" Then
  17. Me.[bus number].Visible = True
  18. [bus number] = Nz(DLookup("[Bus Number]", "[Copy of Employee Work Statistics1]", strCriteria), 0)
  19. Me.[bus number].Locked = True
  20.  
  21. Me.Route.Visible = True
  22. [Route] = Nz(DLookup("[Route]", "[Copy of Employee Work Statistics1]", strCriteria), 0)
  23. Me.Route.Locked = True
  24. Else
  25. Me.Route.Locked = False
  26. Me.School.Locked = False
  27. Me.[bus number].Locked = False
  28. End If
  29. If [Route] = "field Trip" Then
  30. Me.[School].Visible = True
  31. [School] = Nz(DLookup("[School]", "[Copy of Employee Work Statistics1]", strCriteria), 0)
  32. Me.School.Locked = True
  33. Else
  34. Me.School.Locked = False
  35. End If
  36. End If
  37.  
  38. End Sub
Apr 7 '14 #8
Seth Schrock
2,965 Expert 2GB
And you are still getting 0 (a wrong value) when using the dlookup()? Hmmm... Try this
Expand|Select|Wrap|Line Numbers
  1. dim strCriteria as string
  2. strCriteria = "Employee='" & Forms![Royal time Stamp]![Driver] & "' And [Date]=#" & Date & "# And [Trip Sign Off]=0"
  3. debug.print strCriteria
  4. debug.print DLookup("Punch ID", "Copy of EmployeeWork Statistics1", strCriteria)
  5.  
  6. [Punch ID] = Nz(DLookup("Punch ID", "Copy of Employee Work Statistics1", strCriteria), 0)
Apr 7 '14 #9
Seth Schrock
2,965 Expert 2GB
Since you are doing so many DLookups on the same table, you would probably increase performance if you just opened the recordset and used the values from that. All the domain functions (those that start with D; DLookup, DCount, DSum, etc.) are very resource intensive and therefore slow down your code. You can open your recordset like this:
Expand|Select|Wrap|Line Numbers
  1. Dim db as DAO.Database
  2. Dim rst As DAO.Recordset
  3. Dim strRst as String
  4.  
  5. Set db = CurrentDb
  6. strRst = "SELECT [Punch ID] " & _
  7.          "FROM [Copy of Employee Work Statistics1] " & strCriteria
  8. Set rst = db.OpenRecordset(strRst, dbOpenDynaset)
  9.  
  10. 'you can reference your values like this
  11. [Punch ID] = rst![Punch ID]
  12.  
  13.  
  14. 'At the end of your code, close what you open 
  15. 'and release what you set
  16. rst.close
  17. set db = nothing
  18. set rst = nothing
Apr 7 '14 #10
Vasago
46
Seth,

I can't thank you enough for your help and for the "access VBA Class".

Sincerely,
Vasago
Apr 7 '14 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: mjm | last post by:
Folks, Stroustrup indicates that returning by value can be faster than returning by reference but gives no details as to the size of the returned object up to which this holds. My question is...
3
by: Phoenix | last post by:
I am trying to print date headings over comments (as headings) I have a simple XML file : <comments> <comment id=1234 yyyymmdd="20041230" flag="Y">..text..</comment> <comment id=1309...
0
by: Epson Barnett | last post by:
I have a C# Web application I'm working on that loads a csv file on application start. The csv is an inventory file which is used to create a collection in the Application object. If the collection...
11
by: JKop | last post by:
AnyClass Blah() { AnyClass poo; return poo; } As we all know, in the above, the compiler is entitled to:
8
by: Roy Gourgi | last post by:
Hi, I would like to able to able to return a value from a called function but also retain the loop counter in the called function the next time I call it again. TIA Roy
4
by: Earl T | last post by:
When I try to get the netscape version for version 7, I get the HttpBrowserCapabilities class returning the version as 5 and not 7. (see code and output below) CODE HttpBrowserCapabilities...
2
by: Nathan Sokalski | last post by:
I have a DropDownList that is returning the value from index 0 regardless of which item is selected. The code that I am using to test which index it is returning is: Label1.Text =...
11
Parul Bagadia
by: Parul Bagadia | last post by:
I m returning a value of pointer of a structure from one function to main; but whenever it comes in main it becomes NULL... i used another pointer in main and equated its value to returning pointer...
7
by: Ryan Le Piane | last post by:
Hi everyone, I am trying to program a Form Pay Estimator and it calculates the gross pay, taxes owed, and net pay for an individual employee. For some reason my CalculateTaxes method in my Pay()...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.