Page 1 of 3 123 LastLast
Results 1 to 15 of 32
  1. #1
    Sgt_Utz is offline Novice
    Windows 7 64bit Access 2013 64bit
    Join Date
    Aug 2017
    Posts
    25

    How to pass results from mid function to datediff?

    I have been trying to figure this out for 2 days now. I keep getting the error "data type mismatch".



    What I'm trying to do is use the date at the end of a file name to see what documents are over 1 year old. I'm trying to pass the results from the mid function to date diff. I've tried many different things but here's my current code with no success. I've tried adding # to the beginning and end but still no luck.

    Btw the date in the document is using the format "2017-09-09" and have verified the mid function below pulls just this.

    Datediff('d',Date(),mid(tblinfo.attachments.filena me,instrrev(tblinfo.attachments.filename," ")+1,instrrev(tblinfo.attachments.filename,"." )-instrrev(tblinfo.attachments.filename," ")-1))

  2. #2
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 10 Access 2013 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Simplify your code by making it multiple steps. Move the value you need into a variable first and then use the variable in your DateDiff() function. You can then MsgBox or Debug.Print the values and see what they are. Single step would work too.

  3. #3
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    try:

    Datediff('d',Date(),cdate(mid(tblinfo.attachments. filename,instrrev(tblinfo.attachments.filename," ")+1,instrrev(tblinfo.attachments.filename,"." )-instrrev(tblinfo.attachments.filename," ")-1)))

  4. #4
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    You could potentially also do this:

    assuming this statement gets you the correct date string

    mid(tblinfo.attachments. filename,instrrev(tblinfo.attachments.filename," ")+1,instrrev(tblinfo.attachments.filename,"." )-instrrev(tblinfo.attachments.filename," ")-1)

    substitute it into a date converter

    dateserial(yearpart, monthpart, daypart)

    where yearpart would be

    left(
    mid(tblinfo.attachments. filename,instrrev(tblinfo.attachments.filename," ")+1,instrrev(tblinfo.attachments.filename,"." )-instrrev(tblinfo.attachments.filename," ")-1), 4)

    monthpart would be

    mid(
    mid(tblinfo.attachments. filename,instrrev(tblinfo.attachments.filename," ")+1,instrrev(tblinfo.attachments.filename,"." )-instrrev(tblinfo.attachments.filename," ")-1),6,2)

    daypart would be

    right(
    mid(tblinfo.attachments. filename,instrrev(tblinfo.attachments.filename," ")+1,instrrev(tblinfo.attachments.filename,"." )-instrrev(tblinfo.attachments.filename," ")-1))

    then substitute the dateserial statmeent into your calculation

    datediff("d", date(), <EXPRESSION HERE>)

  5. #5
    Sgt_Utz is offline Novice
    Windows 7 64bit Access 2013 64bit
    Join Date
    Aug 2017
    Posts
    25
    Quote Originally Posted by RuralGuy View Post
    Simplify your code by making it multiple steps. Move the value you need into a variable first and then use the variable in your DateDiff() function. You can then MsgBox or Debug.Print the values and see what they are. Single step would work too.
    How do I do this? is this putting all that code "as" something?

  6. #6
    Sgt_Utz is offline Novice
    Windows 7 64bit Access 2013 64bit
    Join Date
    Aug 2017
    Posts
    25
    Thanks for the help. The issue was that I was putting it under criteria instead of as an expression. Sorry I'm a noob.

    Now I'm having the problem that when I do >365 (datediff is days) and try to run the query I get data type mismatch. Without the criteria it gives me a number like I want.

    Anyone know what I'm doing wrong?

  7. #7
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 10 Access 2013 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Are you using this in a query? If so then create a Public Function and invoke it in your query. Your function will look something like:

    Code:
    Public Function Foo(InVal As String) As Integer
    '-- returns the number of days between incoming date string
    '-- in the format Year-Month-Day and today.
    
    Dim MyArray() As String
    Dim MyDate
    
    MyArray = Split(InVal, "-")
    
    MyDate = DateSerial(MyArray(0), MyArray(1), MyArray(2))
    
    Foo = DateDiff("d", Date, MyDate)
    
    End Function

  8. #8
    Sgt_Utz is offline Novice
    Windows 7 64bit Access 2013 64bit
    Join Date
    Aug 2017
    Posts
    25
    Thank you I'll try this. It helps to understand what is happening to understand why this doesn't work. Why can't I just use the expression as I did above? It returns a number so why can't I simply say ">365"?

  9. #9
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 10 Access 2013 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Does it return a numerical value or a text string that contain the numbers?

  10. #10
    Sgt_Utz is offline Novice
    Windows 7 64bit Access 2013 64bit
    Join Date
    Aug 2017
    Posts
    25
    not sure. The number column is a result of using datediff which works, so I would think it would return a number and not text. I'm guessing since it is not working however that it's returning text.

    Is there a way to turn the result from the datediff in the expression into a number? Will wrapping the datediff in "Val" fix this?

  11. #11
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,915
    Result from DateDiff is a number. Rural Guy's function returns a number. But it is returning a negative because MyDate is older than current Date. If that is same in your case, use:

    Foo = DateDiff("d", MyDate, Date)

    And criteria >365 works for me.
    Last edited by June7; 08-04-2017 at 08:47 PM.
    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.

  12. #12
    Sgt_Utz is offline Novice
    Windows 7 64bit Access 2013 64bit
    Join Date
    Aug 2017
    Posts
    25
    Ok I got some progress... I think.

    There's one line missing from the function above, which is the mid function to split the date from the rest of the filename, but other than that it's the same:
    Code:
    Public Function Foo(InVal As String) As IntegerDim MyArray() As String
    Dim MyDate
    Dim FileDate
    FileDate = Mid(InVal, InStrRev(InVal, " ") + 1, InStrRev(InVal, ".") - InStrRev(InVal, " ") - 1)
    MyArray = Split(FileDate, "-")
    MyDate = DateSerial(MyArray(0), MyArray(1), MyArray(2))
    Foo = DateDiff("d", MyDate, Date)
    End Function
    When going back to design view for the query, I have "Expr1: Foo([Attachments.FileName])". I'm getting the exact same output. I get numbers, but when I put ">2" under Criteria I get a data type mismatch. What am I missing?

  13. #13
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,915
    Function returns a number value except when the field is empty then it returns #Error because the function expects a string input and string variables cannot hold Null. That causes datatype mismatch error for the criteria. So, handle Null:

    Public Function Foo(InVal As Variant) As Integer
    ...
    If Not IsNull(InVal) Then
    ...
    End If
    End Function
    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.

  14. #14
    Sgt_Utz is offline Novice
    Windows 7 64bit Access 2013 64bit
    Join Date
    Aug 2017
    Posts
    25
    Quote Originally Posted by June7 View Post
    Function returns a number value except when the field is empty then it returns #Error because the function expects a string input and string variables cannot hold Null. That causes datatype mismatch error for the criteria. So, handle Null:

    Public Function Foo(InVal As Variant) As Integer
    ...
    If Not IsNull(InVal) Then
    ...
    End If
    End Function
    Well sir you found the issue, however your code isn't working. I created dummy documents to ensure their were no null records and the function worked, however as soon as I deleted the only record on a user (creating a null record) I got the data type mismatch, and this is using your "isnull" code. Why wouldn't that work on it?

  15. #15
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,915
    Then something else is going on because it works for me. I tested with text type field as well as attachment type.

    If you want to provide db for analysis, follow instructions at bottom of my post.
    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.

Page 1 of 3 123 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. DateDiff function
    By joym in forum Forms
    Replies: 10
    Last Post: 02-15-2017, 03:29 AM
  2. datediff function in query
    By LeesKeys in forum Queries
    Replies: 3
    Last Post: 06-23-2016, 08:54 AM
  3. DateDiff function
    By stavros in forum Access
    Replies: 18
    Last Post: 06-01-2013, 07:16 PM
  4. DateDiff Function
    By Nixx1401 in forum Access
    Replies: 4
    Last Post: 04-14-2011, 09:27 AM
  5. DateDiff function
    By Scott R in forum Reports
    Replies: 5
    Last Post: 12-03-2008, 07:32 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