Results 1 to 3 of 3
  1. #1
    Markb384 is offline Advanced Beginner
    Windows XP Access 2010 32bit
    Join Date
    Jan 2014
    Posts
    44

    Multiple Criteria for DLookup using Dates and Strings

    Hi guys

    I am trying to use DLookup to test whether a record exists for a certain date for a certain piece of equipment within a table, which is then used to concantanate a string which I later use in a message box. However, I can't seem to get it to work with my test data.

    My code is..
    Code:
    Dim sdate As Date
    Dim equipID As String, testString As String, msgString As String
    
    'check a date has been selected and if one has, inform the user of which tests were performed on that date
        If IsNull([Forms]![Calculation Results]!
    [List_date].Column(0)) Then
            MsgBox ("Please enter a valid date")
            Exit Sub
        Else
            sdate = [Forms]![Calculation Results]!
    [List_date].Column(0)
            equipID = [Forms]![Calculation Results]![Equipment_ID]
            testString = "[Equipment ID] = '" & equipID & "' AND [Survey Date] = #" & sdate & "#"
            
            If Not IsNull(DLookup("[Survey Date]", "[Results_Hutt_Dig]", testString)) Then msgString = msgString & "- message1" & vbCrLf
       End If
    I have tried to use a single string, as there will be a few of these If Not Null evaluations on several similar tables so it saved typing. I can't seem to get it to work with manually typing into the criteria for DLookup either. Originally, when viewed in the locals windows, sdate was showing the # delimiters, but these disappeared within the string, so I have added them manually. Neither version works unfortunately.



    Any ideas? It's probably something really simple and stupid.


    Edit: and much searching on the web, I had the idea to ensure that the dates were in the right format (based on a similar solution someone else had). Using format(date,"ddmmyyyy") seemed to fix it. The final code is below now
    Code:
    Dim sdate As DateDim equipID As String, testString As String, msgString As String
    
    
    'check a date has been selected and if one has, inform the user of which tests were performed on that date
        If IsNull([Forms]![Calculation Results]!
    [List_date].Column(0)) Then
            MsgBox ("Please enter a valid date")
        Else
            sdate = [Forms]![Calculation Results]!
    [List_date].Column(0)
            equipID = [Forms]![Calculation Results]![Equipment_ID]
            testString = "[Equipment ID] = '" & equipID & "' AND Format([Survey Date],""ddmmyyyy"") = " & Format(sdate, "ddmmyyyy")
            
            If Not IsNull(DLookup("[ID]", "[Results_Hutt_Dig]", testString)) Then msgString = msgString & "- Message1" & vbCrLf
        End If
    Last edited by Markb384; 01-22-2014 at 05:52 AM. Reason: Solved

  2. #2
    amrut is offline Expert
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2012
    Location
    Dubai
    Posts
    614
    Access calculations are always based on US date format of mm/dd/yyyy irrespective of your PC setting. Hence, it is always a good practice to format the date as
    Format(sdate,"mm\/dd\/yyyy") and with "#" delimeters on either side of date field.
    The testring should be like -
    Code:
    testString = "[Equipment ID] = '" & equipID & "' AND [Survey Date]= #" & Format(sdate, "mm\/dd\/yyyy") & "#"
    Further, access experts advise to avoid spaces in filed names like [Equipment ID] should be [EquipmentID]
    See here for further information about date format in access - http://allenbrowne.com/ser-36.html

  3. #3
    Markb384 is offline Advanced Beginner
    Windows XP Access 2010 32bit
    Join Date
    Jan 2014
    Posts
    44
    Thank you Amrut, very interesting reading.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Using DLookup with multiple criteria
    By halt4814 in forum Access
    Replies: 2
    Last Post: 04-08-2013, 12:26 PM
  2. Replies: 1
    Last Post: 01-31-2013, 04:56 AM
  3. Replies: 11
    Last Post: 04-30-2012, 07:22 PM
  4. Query Criteria with Multiple Dates
    By Jojojo in forum Queries
    Replies: 3
    Last Post: 10-08-2011, 05:07 PM
  5. Multiple criteria in DLookup
    By crowegreg in forum Forms
    Replies: 7
    Last Post: 06-22-2011, 01:47 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums