Results 1 to 8 of 8
  1. #1
    kleaverjr is offline Advanced Beginner
    Windows 7 64bit Access 2013 64bit
    Join Date
    Apr 2022
    Posts
    53

    Why is SQL search for Date not finding recordset

    This really puzzles me.

    I have a date field in a table. The date that is being searched for changes so the SQL language uses a variable for the date. When I put in an actual date in the SQL language, Access finds the record, but when I replace the date with a formatted variable (and the format matches what is in the table) no recordset is found. So here is the code. I hope someone can tell me what I am doing wrong. I have searched and searched and every solution I find online, does not work. Thanks.

    Code:
    ShowWeekend = Format(ShowWeekend, "M/D/YYYY"
    
    sqlFindCurrentShow = " SELECT * FROM Show_SalesSummaryLog WHERE Show_Date_Start = " & ShowDateString 
    
    Set dbFindCurrentShow = CurrentDb
    Set rsRindCurrentShow = dbFindCurrentShow.OpenRecordset(sqlFindCurrentShow)
    And ShowWeekend= 9/2/2023

    The above code does not find the record. But the code BELOW does:




    Code:
    sqlFindCurrentShow = " SELECT * FROM Show_SalesSummaryLog WHERE Show_Date_Start = 9/2/2023" 
    
    Set dbFindCurrentShow = CurrentDb
    Set rsRindCurrentShow = dbFindCurrentShow.OpenRecordset(sqlFindCurrentShow)
    What am I misunderstanding.

    And thanks again for all who have been helping me with this program. I have been programming in BASIC since I was 5 years old when my grandfather started teaching me on his own kit built Apple II computer, and eventually learning Quick Basic then Visual Basic, and now VBA (since I really don't like VB.net) When it comes to VBA I am "self taught" along with those who have been helping me on forums like this so though I understand the Logic, things like this stump me and drive me crazy as logically it should work but it isn't. So thanks for your patience!

    Ken L

  2. #2
    orange's Avatar
    orange is online now Moderator
    Windows 10 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    Ken,

    It would be helpful if you showed all the code involved.
    Hopefully you have dimmed dbFindCurrentShow?
    What is ShowDateString in your first example?

  3. #3
    kleaverjr is offline Advanced Beginner
    Windows 7 64bit Access 2013 64bit
    Join Date
    Apr 2022
    Posts
    53
    I deleted all references to ShowDateString (forgot to update the code when I typed it up)

    Here's the rest of the relevant code.

    Code:
    Dim ShowWeekend As date
    dim dbFindCurrentShow as DAO.Database
    Dim rsFindCurrentShow as DAO.Recordset
    dim sqlFindCurrentShow as string 
    
    ShowWeekend = Format(ShowWeekend, "M/D/YYYY"
    
    sqlFindCurrentShow = " SELECT * FROM Show_SalesSummaryLog WHERE Show_Date_Start = " & ShowWeekend 
    
    Set dbFindCurrentShow = CurrentDb
    Set rsRindCurrentShow = dbFindCurrentShow.OpenRecordset(sqlFindCurrentShow)
    The actual program is on a different computer that is not connected to the internet, and transfering the file via USB Drive is a pain, so I am manually typing the code into the forums to find solutions to the problem.

    And there is a whole bunch of code that is used to determine the value of ShowWeekend. But that is irrelevant to the issue because the value of ShowWeekend in this case IS 9/2/2023 which matches what is in the Record. When I put 9/2/2023 into the WHERE clause, it finds the record, but when it's a value from a variable it doesn't. WHY?!


    Thanks.

    Ken L.

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    In the code above you never set ShowWeekend to a date value, you only try to format that value. If it's set elsewhere, you may be dealing with two different variables with the same name, and you don't have Option Explicit set.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    orange's Avatar
    orange is online now Moderator
    Windows 10 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    Ken,

    This statement is missing a closing ")"
    ShowWeekend = Format(ShowWeekend, "M/D/YYYY" <=====this results in a string data type NOT a Date

    However, a Date data type requires # separators.

    For example,
    Code:
    Dim mdate as Date
    mdate =#8/29/23#
    You do not have a value assigned to ShowWeekend??

    When you Dim a variable as a Date, then it will require the # before and after when you use it.

    Code:
    sqlFindCurrentShow = " SELECT * FROM Show_SalesSummaryLog WHERE Show_Date_Start = #" & ShowWeekend & "#"
    Last edited by orange; 08-29-2023 at 06:13 PM. Reason: spelling/clarification

  6. #6
    kleaverjr is offline Advanced Beginner
    Windows 7 64bit Access 2013 64bit
    Join Date
    Apr 2022
    Posts
    53
    ShowWeekend is set in an entirely separate part. But even when I had set ShowWeekend = "9/02/2023" though the SQLString still showed the text in the string correctly, it could not find the date. When I inserted the # characters though, that solved the problem. I was not aware that I had to insert those characters when searching for a date. Thanks for the assist.

    Ken L

  7. #7
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    I used to use this constant in some of my DBs

    Code:
    Public Const strcJetDate = "\#mm\/dd\/yyyy\#"  'Needed for dates in queries as Access expects USA format.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  8. #8
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    I was going to mention the delimiters but you said this worked:

    sqlFindCurrentShow = " SELECT * FROM Show_SalesSummaryLog WHERE Show_Date_Start = 9/2/2023"

    which surprised me.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

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

Similar Threads

  1. Text Box Search Having Issue Finding Name of Clinic
    By CharissaBelle in forum Forms
    Replies: 7
    Last Post: 04-17-2017, 03:50 PM
  2. finding record in recordset
    By mik in forum Programming
    Replies: 1
    Last Post: 10-16-2014, 06:02 AM
  3. Replies: 2
    Last Post: 12-08-2012, 10:01 AM
  4. Replies: 3
    Last Post: 07-18-2012, 10:13 PM
  5. finding a value in a recordset
    By TheShabz in forum Programming
    Replies: 9
    Last Post: 04-23-2010, 02:44 PM

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