Results 1 to 13 of 13
  1. #1
    CharissaBelle is offline Advanced Beginner
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2016
    Posts
    81

    Text Box Search Using FindFirst For Date

    I have an unbound textbox (txtFilter) in the header of a form in which I want the user to be able to type in a date and use the findfirst method to find the first record with that date. The following is actually the code I used to findfirst Employee, I just changed it from Employee to StartDate and need help with the Like section.

    Code:
    Set rs = Me.Recordset.Clone
            rs.FindFirst "([StartDate] Like """ & Me.txtFilter & "*"")"
            If Not rs.EOF Then Me.Bookmark = rs.Bookmark


  2. #2
    andy49's Avatar
    andy49 is offline VIP
    Windows 10 Access 2007
    Join Date
    Nov 2016
    Location
    London
    Posts
    1,051
    Don't use like with date.

    Are you looking for the specific date?
    [startdate] = cdate(me.txtfilter)

    Or similar (no pc here)


    Sent from my iPhone using Tapatalk

  3. #3
    CharissaBelle is offline Advanced Beginner
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2016
    Posts
    81
    Quote Originally Posted by andy49 View Post
    Are you looking for the specific date?
    I am looking to go to the closest date to the one typed in.


    I changed my code to look like this:

    Code:
    Set rs = Me.Recordset.Clone        
    rs.FindFirst "([StartDate] cDate(Me.txtFilter))"
    If Not rs.EOF Then Me.Bookmark = rs.Bookmark
    Now when I type in an exact date I know is there, I get a Run Time error 3077 syntax error (missing operator) in expression.

  4. #4
    CharissaBelle is offline Advanced Beginner
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2016
    Posts
    81
    Oh wait, I found what I missed.

    Now my code looks like this

    Code:
    Set rs = Me.Recordset.Clone
    rs.FindFirst "([StartDate] = cDate(Me.txtFilter))"
    If Not rs.EOF Then Me.Bookmark = rs.Bookmark
    And I'm getting a different error 3345 unknown or invalid field reference 'Me.txtFilter'

  5. #5
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows 7 32bit Access 2013
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,614
    You will need to use the # date delimiters and I think you may need to use format on the date.
    Take a look at: https://msdn.microsoft.com/en-us/lib...or=-2147217396
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  6. #6
    jwhite is offline Competent Performer
    Windows 10 Access 2013 32bit
    Join Date
    Dec 2012
    Location
    North Carolina
    Posts
    349
    @Charissa: Your first post stated "...find the first record with that date", but then in a later post you stated "...go to the closest date to the one typed in.". Please clarify for those already helping, which is it?

    And going by what Bob suggested, your statement would be more like: rs.FindFirst "[StartDate] = #" & Me.txtFilter & "#"

  7. #7
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows 7 32bit Access 2013
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,614
    From the link I posted earlier:
    You should use the U.S. date format (month-day-year) when you search for fields containing dates, even if you're not using the U.S. version of the Microsoft Access database engine; otherwise, the data may not be found. Use the Visual Basic Format function to convert the date. For example:VBA



    rstEmployees.FindFirst "HireDate > #" _
    & Format(mydate, 'm-d-yy' ) & "#"





    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  8. #8
    CharissaBelle is offline Advanced Beginner
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2016
    Posts
    81
    Quote Originally Posted by jwhite View Post
    @Charissa: Your first post stated "...find the first record with that date", but then in a later post you stated "...go to the closest date to the one typed in.". Please clarify for those already helping, which is it?
    I would like to go to the closest date to the one typed in.

    Here's my code now. I'm getting close. The error I'm getting now is run time error 6 overflow.

    Code:
    Set rs = Me.Recordset.Clone
    rs.FindFirst "StartDate > #" & Format(Me.txtFilter, mm / dd / yyyy) & "#"
    If Not rs.EOF Then Me.Bookmark = rs.Bookmark

  9. #9
    jwhite is offline Competent Performer
    Windows 10 Access 2013 32bit
    Join Date
    Dec 2012
    Location
    North Carolina
    Posts
    349
    Yes, Bob, but she also had the control name within the quotes which wouldn't work anyway, and thought it was worth pointing out.

  10. #10
    CharissaBelle is offline Advanced Beginner
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2016
    Posts
    81
    Well, I've almost got this working.

    Here is my code currently. StartDate is formatted as a Short Date. And it works if I type the date exactly like this mm/dd/yyyy. However, I don't want to have to input the '/' between mmddyyyy. I tried adding an input mask to the txtFilter for short date, but then I get a run time error 6 overflow. Any ideas what I can do so the user only has to enter the mmddyyyy without having to add the '/'s?

    Code:
    Set rs = Me.Recordset.Clone
    rs.FindFirst "[StartDate] > #" & Format(Me.txtFilter, "mm\/dd\/yyyy") & "#"
    If Not rs.EOF Then Me.Bookmark = rs.Bookmark

  11. #11
    CharissaBelle is offline Advanced Beginner
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2016
    Posts
    81
    Solved it! Instead of using an input mask, I formatted txtFilter as a short date. Woohoo!!! Thanks everyone!!!

  12. #12
    andy49's Avatar
    andy49 is offline VIP
    Windows 10 Access 2007
    Join Date
    Nov 2016
    Location
    London
    Posts
    1,051
    What about setting the input mask of the textbox?

  13. #13
    CharissaBelle is offline Advanced Beginner
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2016
    Posts
    81
    Quote Originally Posted by andy49 View Post
    What about setting the input mask of the textbox?
    That's what I tried to do first, but I would get a runtime error 6 overflow. I don't know why, but it seems to work if I set the actual format property of txtFilter to Short Date.

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

Similar Threads

  1. Replies: 7
    Last Post: 05-12-2016, 06:02 PM
  2. Replies: 5
    Last Post: 08-20-2014, 10:18 PM
  3. Replies: 3
    Last Post: 09-11-2013, 09:49 AM
  4. using FindFirst with a Date Criteria (syntax error)
    By ajetrumpet in forum Programming
    Replies: 2
    Last Post: 09-10-2010, 11:01 PM
  5. RST Findfirst date problem
    By ColPat in forum Programming
    Replies: 3
    Last Post: 08-09-2010, 07:28 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