Consulting

Results 1 to 11 of 11

Thread: Using a date in a dlookup

  1. #1

    Using a date in a dlookup

    Hi All,

    I know this is a common question, and I have desperately been searching the forum for answers.

    I have used every possible format that I have found and cannot figure out what i'm missing.

    Here is my lookup:

    Me.Text68 = DLookup("ID", "Leave Database", "[StaffName]= '" & Me.Combo33 & "'" And "[StartDate]=#" & Format(Me.StartDate, "d/mm/yyyy") & "#")

    I'm in Australia and have tried multiple date formats to make this work but I keep getting the data type mismatch error.

    I tested the code without the date and it works fine, but I need the date to ensure that my users aren't submitting duplicate records.

    Please help me.

    Thank you,

    Kim

  2. #2
    VBAX Expert
    Joined
    Oct 2012
    Posts
    726
    Location
    Date literals are US format #5/17/2017#

  3. #3
    Thank you, I tried both:

    Me.Text68 = DLookup("ID", "Leave Database", "[StaffName]= '" & Me.Combo33 & "'" And "[StartDate]=#" & Format(Me.StartDate, "mm/dd/yyyy") & "#")

    and

    Me.Text68 = DLookup("ID", "Leave Database", "[StaffName]= '" & Me.Combo33 & "'" And "[StartDate]=#" & Format(Me.StartDate, "m/dd/yyyy") & "#")

    And both returned the type mismatch error.

    Is there maybe something else I am doing wrong?

  4. #4
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    FYI, Format(CDate("5/7/2017"), "m/dd/yyyy") = "5/07/2017"
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  5. #5
    Still returning the error for both:

    Me.Text68 = DLookup("ID", "Leave Database", "[StaffName]= '" & Me.Combo33 & "'" And "[StartDate]=#" & Format(CDate(Me.StartDate), "m/dd/yyyy") & "#")

    And

    Me.Text68 = DLookup("ID", "Leave Database", "[StaffName]= '" & Me.Combo33 & "'" And "[StartDate]=" & Format(CDate(Me.StartDate), "m/dd/yyyy"))

  6. #6
    Try:


    Format(Me.StartDate, "mm\/dd\/yyyy")

    This should help:

    International Dates in Access

    Boyd Trimmell aka HiTechCoach
    Microsoft Access MVP -2010-2015

    Programming: Nine different ways to do it right, a thousand ways to do it wrong.
    Binary--it's as easy as 1-10-11

  7. #7
    Thanks for that, I had previously read that article and followed the steps

    Unfortunately I still got the error for all of these attempts:

    Me.Text68 = DLookup("ID", "Leave Database", "[StaffName]= '" & Me.Combo33 & "'" And "[StartDate]=" & Format(Me.StartDate, "mm\/dd\/yyyy"))

    Me.Text68 = DLookup("ID", "Leave Database", "[StaffName]= '" & Me.Combo33 & "'" And "[StartDate]=#" & Format(Me.StartDate, "mm\/dd\/yyyy") & "#")

    Me.Text68 = DLookup("ID", "Leave Database", "[StaffName]= '" & Me.Combo33 & "'" And "#[StartDate]#=#" & Format(Me.StartDate, "mm\/dd\/yyyy") & "#")

    Me.Text68 = DLookup("ID", "Leave Database", "[StaffName]= '" & Me.Combo33 & "'" And "#" & Format([StartDate], "mm\/dd\/yyyy") & "#=#" & Format(Me.StartDate, "mm\/dd\/yyyy") & "#")

    I am quickly losing all hope

  8. #8
    VBAX Expert
    Joined
    Oct 2012
    Posts
    726
    Location
    Me.Text68 = DLookup("ID", "Leave Database", "[StaffName]='" & Me.Combo33 & "' And [StartDate]=#" & Format(Me.StartDate, "mm/dd/yyyy") & "#")

  9. #9
    I used that one above.

    But it's ok. I've solved the problem using indexed relationships and a non-date lookup about 10 mins ago.

    Thank you for all of your help.

  10. #10
    VBAX Expert
    Joined
    Oct 2012
    Posts
    726
    Location
    You used that date format, but I fixed your quote marks and put the field back in the correct place.

    Glad you got it working anyway

  11. #11
    Oh, I see the difference now. I've saved the formula for the future.

    Thank you again for your help.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •