Results 1 to 10 of 10
  1. #1
    mcmad is offline Novice
    Windows 11 Office 365
    Join Date
    Feb 2025
    Posts
    7

    Question DLookUp with one text criteria and one date criteria - syntax error

    I would appreciate help with the following:



    I am trying to perform a DLookUp using a text value from a field in a table that is used in my form along with a date field stored in a table and also used in my form. The idea is to lookup a particular price at a particular date in my Shares table but I am at wits end trying to get the syntax correct. I was able to successfully perform a DLookUp from a Test table using a text field on its own and a date field on its own, but the multiple criteria has me lost. The following is my attempt.

    Private Sub ClosePrice_Click()


    Dim VarX As String
    If IsNull(VarX = DLookup("Close", "Shares", "[ASX Code] = " & Forms!WatchList_Notes!ASX _
    & " AND [CloseDate] = #" & Format(Forms!WatchList_Notes!NoteDate, "yyyy-mm-dd") & "#")) Then


    MsgBox "No Value Found"
    MsgBox Forms![WatchList_Notes]![ASX]
    MsgBox Format(Forms![WatchList_Notes]![NoteDate], "yyyy-mm-dd")
    Else


    VarX = DLookup("Close", "Shares", "[ASX Code] = " & Forms!WatchList_Notes!ASX _
    & " AND Format(CloseDate, ""yyyy-mm-dd") = "#" & Format(Forms!WatchList_Notes!NoteDate, "yyyy-mm-dd") & "#)"


    Forms![WatchList_Notes].[ClosePrice] = Price


    End If


    End Sub

    The following is the error message:

    Click image for larger version. 

Name:	AccessError.jpg 
Views:	19 
Size:	8.2 KB 
ID:	52690

    I will appreciate any help on the subject.

    Thanks, David

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 11 Access 2021
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,771
    Format returns a string value, not a true date.
    And you have some concatenation errors.

    Try:

    VarX = DLookup("Close", "Shares", "[ASX Code] = " & Forms!WatchList_Notes!ASX _
    & " AND Format(CloseDate, 'yyyy-mm-dd') = '" & Format(Forms!WatchList_Notes!NoteDate, "yyyy-mm-dd") & "'")



    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    jojowhite's Avatar
    jojowhite is offline Competent Performer
    Windows 11 Access 2021
    Join Date
    Jan 2025
    Posts
    434
    Code:
    Private Sub ClosePrice_Click()
    
    
    Dim VarX As String
    VarX = DLookup("Close", "Shares", "[ASX Code] = " & Forms!WatchList_Notes!ASX _
    & " AND [CloseDate] = #" & Format(Forms!WatchList_Notes!NoteDate, "yyyy-mm-dd") & "#")) 
    
    If IsNull(VarX) Then
    
    
    MsgBox "No Value Found"
    MsgBox Forms![WatchList_Notes]![ASX]
    MsgBox Format(Forms![WatchList_Notes]![NoteDate], "yyyy-mm-dd")
    Else
    
    
    
    Forms![WatchList_Notes].[ClosePrice] = VarX
    
    
    End If
    
    
    End Sub

  4. #4
    mcmad is offline Novice
    Windows 11 Office 365
    Join Date
    Feb 2025
    Posts
    7
    Quote Originally Posted by June7 View Post
    Format returns a string value, not a true date.
    And you have some concatenation errors.

    Try:

    VarX = DLookup("Close", "Shares", "[ASX Code] = " & Forms!WatchList_Notes!ASX _
    & " AND Format(CloseDate, 'yyyy-mm-dd') = '" & Format(Forms!WatchList_Notes!NoteDate, "yyyy-mm-dd") & "'")



    Many thanks for your help, I do however now get another error, see below:

    Click image for larger version. 

Name:	AccessError2.jpg 
Views:	17 
Size:	10.0 KB 
ID:	52692

  5. #5
    mcmad is offline Novice
    Windows 11 Office 365
    Join Date
    Feb 2025
    Posts
    7
    Quote Originally Posted by jojowhite View Post
    Code:
    Private Sub ClosePrice_Click()
    
    
    Dim VarX As String
    VarX = DLookup("Close", "Shares", "[ASX Code] = " & Forms!WatchList_Notes!ASX _
    & " AND [CloseDate] = #" & Format(Forms!WatchList_Notes!NoteDate, "yyyy-mm-dd") & "#")) 
    
    If IsNull(VarX) Then
    
    
    MsgBox "No Value Found"
    MsgBox Forms![WatchList_Notes]![ASX]
    MsgBox Format(Forms![WatchList_Notes]![NoteDate], "yyyy-mm-dd")
    Else
    
    
    
    Forms![WatchList_Notes].[ClosePrice] = VarX
    
    
    End If
    
    
    End Sub
    Thank you for helping, I still get an error as below:
    Click image for larger version. 

Name:	AccessError2.jpg 
Views:	16 
Size:	10.0 KB 
ID:	52693

    On other records I get the following:
    Click image for larger version. 

Name:	AccessError3.jpg 
Views:	16 
Size:	11.9 KB 
ID:	52694

    I have made certain the records are in both tables with the correct dates.

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows 11 Access 2021
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,771
    [ASX Code] has text value so need apostrophe delimiters.

    VarX = DLookup("Close", "Shares", "[ASX Code] = '" & Forms!WatchList_Notes!ASX _
    & "' AND Format(CloseDate, 'yyyy-mm-dd') = '" & Format(Forms!WatchList_Notes!NoteDate, "yyyy-mm-dd") & "'")
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  7. #7
    mcmad is offline Novice
    Windows 11 Office 365
    Join Date
    Feb 2025
    Posts
    7
    I cant thank you enough, problem solved and works perfectly. This is my first go at using MS-Access and I must admit I have been trying for days to get this working.

    Thank you again.

  8. #8
    mcmad is offline Novice
    Windows 11 Office 365
    Join Date
    Feb 2025
    Posts
    7
    Thank you for your help.

  9. #9
    mcmad is offline Novice
    Windows 11 Office 365
    Join Date
    Feb 2025
    Posts
    7
    I cant thank you enough, problem solved and works perfectly. This is my first go at using MS-Access and I must admit I have been trying for days to get this working.

    Thank you again.

  10. #10
    mcmad is offline Novice
    Windows 11 Office 365
    Join Date
    Feb 2025
    Posts
    7
    Quote Originally Posted by June7 View Post
    [ASX Code] has text value so need apostrophe delimiters.

    VarX = DLookup("Close", "Shares", "[ASX Code] = '" & Forms!WatchList_Notes!ASX _
    & "' AND Format(CloseDate, 'yyyy-mm-dd') = '" & Format(Forms!WatchList_Notes!NoteDate, "yyyy-mm-dd") & "'")
    Sorry I keep posting with a reply instead of quote (I have lost the plot today )

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

Similar Threads

  1. Replies: 5
    Last Post: 09-16-2018, 01:18 PM
  2. Replies: 2
    Last Post: 01-04-2016, 09:40 AM
  3. Replies: 4
    Last Post: 04-21-2015, 07:12 PM
  4. Replies: 9
    Last Post: 04-18-2014, 08:51 PM
  5. Incomplete Syntax Clause (syntax error)
    By ajetrumpet in forum Programming
    Replies: 4
    Last Post: 09-11-2010, 10: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