Results 1 to 12 of 12
  1. #1
    EHittner is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Nov 2009
    Posts
    32

    "Data Type Mismatch in Criteria" Error When Attempting to Use Append Query

    I have a query that converts a date in text format to a short date prior to appending it into another table where the destination field is in Date/Time format. The criteria in this field is: ">Date()-45" When I view the results, everything is in place. I have all the desired results -- the text string is converted to a short date and has data from the previous 45 days. But when I run the query, I receive the following error message: "Data Type Mismatch in Criteria"

    The "dteRegularStyle" in the code below is a VBA module that converts a date string in text format (YYYYMMDD) to a short date format (MM/DD/YYYY).

    I've checked the properties of both the source and destination -- and both line up. Not sure what to do from here.

    Query SQL:

    INSERT INTO tblPending ( receipt, cob, a_file, MailDate, SortCode )
    SELECT dbo_NSC_PetApp.Receipt_Number, dbo_NSC_PetApp.Ben_Country_Of_Birth, dbo_NSC_PetApp.Ben_A_Number, dteRegularStyle([RECEIVED_DATE]) AS Expr2, fSortcode([form_number],[part_2_1],[part_2_2],[class_preference],[ben_country_of_birth],[ben_current_class]) AS SortCode
    FROM dbo_NSC_PetApp INNER JOIN dbo_NSC_DateIn ON dbo_NSC_PetApp.Receipt_Number = dbo_NSC_DateIn.Receipt_Number
    WHERE (((dbo_NSC_PetApp.Receipt_Number)<"z") AND ((dteRegularStyle([RECEIVED_DATE]))>Date()-45));

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,515
    It may not like your math,
    DATEADD("d",-45,[datefield]) will produce a date field.

    or convert it to a date,
    CVDATE(format(dateFld,"mm/dd/yyyy"))

  3. #3
    EHittner is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Nov 2009
    Posts
    32
    You'll have to forgive me. I'm not a coder - and am doing most of this through the query design screen. Would this appear on the criteria line? Or as a separate field in the query design? In the query as designed, I am producing a result that is in short date format. The date shows fine in the results screen. It's only when I run the query that it comes back with a "Data type mismatch in criteria expression." error.

  4. #4
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,795
    Date value is stored in table as a string? Why?

    Does the dteRegularStyle function return a string or date value? Post its code.
    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.

  5. #5
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,387
    since your dteRegularStyle function is returning a formatted date, it will be a string. Dates are stored as numbers, not as you seem them formatted. Todays date is 42929 for example

    you need to convert the string to a date datatype using cdate as ranman suggests

    cdate(dteRegularStyle([RECEIVED_DATE])) AS Expr2

    or use the # to tell sql that the value between the # is to be interpreted as a date

    "#" & dteRegularStyle([RECEIVED_DATE]) & "#" AS Expr2

  6. #6
    EHittner is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Nov 2009
    Posts
    32
    The dteRegularStyle code was built by another developer. Because we have multiple date styles in our systems, it's a bit complex, and is built to convert each type to a regular short date format. It's as follows:

    Option Compare Database
    Option Explicit
    Public Function dteRegularStyle(dteCLAIMS As String) As Date
    Dim dteRegularYear As Integer
    Dim dteRegularMonth As Integer
    Dim dteRegularDay As Integer
    Dim datecount As Integer

    datecount = datecount + 1
    On Error GoTo dteregularstyle_err
    restart:
    If IsNull(dteCLAIMS) = True Or dteCLAIMS = "" Then Exit Function
    If Len(dteCLAIMS) = 8 Then
    If Left(dteCLAIMS, 2) > 12 Then
    dteRegularYear = Left(dteCLAIMS, 4)
    dteRegularMonth = Mid(dteCLAIMS, 5, 2)
    dteRegularDay = Right(dteCLAIMS, 2)
    Else
    dteRegularYear = Right(dteCLAIMS, 4)
    dteRegularMonth = Left(dteCLAIMS, 2)
    dteRegularDay = Mid(dteCLAIMS, 3, 2)
    End If
    Else
    If Len(dteCLAIMS) = 4 Then dteCLAIMS = "010101"
    If Left(dteCLAIMS, 1) = "A" Then
    dteRegularYear = 2000 + Int(Mid(dteCLAIMS, 2, 1))
    dteRegularMonth = Mid(dteCLAIMS, 3, 2)
    dteRegularDay = Right(dteCLAIMS, 2)
    Else
    If Left(dteCLAIMS, 1) = "B" Then
    dteRegularYear = 2010 + Int(Mid(dteCLAIMS, 2, 1))
    dteRegularMonth = Mid(dteCLAIMS, 3, 2)
    dteRegularDay = Right(dteCLAIMS, 2)
    Else
    If Mid(dteCLAIMS, 5, 1) = "A" Then
    dteRegularYear = 2000 + Int(Right(dteCLAIMS, 1))
    dteRegularMonth = Left(dteCLAIMS, 2)
    dteRegularDay = Mid(dteCLAIMS, 3, 2)
    Else
    If Mid(dteCLAIMS, 5, 1) = "B" Then
    dteRegularYear = 2010 + Int(Right(dteCLAIMS, 1))
    dteRegularMonth = Left(dteCLAIMS, 2)
    dteRegularDay = Mid(dteCLAIMS, 3, 2)
    Else
    dteRegularYear = 1900 + Int(Mid(dteCLAIMS, 1, 2))
    dteRegularMonth = Mid(dteCLAIMS, 3, 2)
    dteRegularDay = Right(dteCLAIMS, 2)
    End If
    End If
    End If
    End If
    End If
    RegDate:
    If dteRegularMonth = 0 Then dteRegularMonth = 1
    If dteRegularMonth > 12 Then dteRegularMonth = 1
    dteRegularStyle = dteRegularMonth & "/" & dteRegularDay & "/" & dteRegularYear: Exit Function
    dteregularstyle_err:
    'Stop
    'If dteRegularDay > 28 Then dteRegularDay = 1: GoTo RegDate
    'If dteCLAIMS = "00010000" Or dteCLAIMS = "00000000" Then
    dteCLAIMS = "18000101"
    GoTo restart
    MsgBox Error$
    End Function
    Public Function dteRegularStyleIN(dteCLAIMSIn As String) As Date
    If dteCLAIMSIn = "00010000" Or dteCLAIMSIn = "00000000" Then dteCLAIMSIn = "18000101"
    dteRegularStyleIN = Mid(dteCLAIMSIn, 5, 2) & "/" & Right(dteCLAIMSIn, 2) & "/" & Left(dteCLAIMSIn, 4)
    End Function
    Public Function dteRegularStyleRAFACS(dteRAFACSIn As String) As Date
    On Error GoTo Default
    Retry:
    'If dteRAFACSIn = "010000" Or dteRAFACSIn = "000000" Then dteRAFACSIn = "18000101"
    dteRegularStyleRAFACS = Mid(dteRAFACSIn, 3, 2) & "/" & Right(dteRAFACSIn, 2) & "/" & Left(dteRAFACSIn, 2)
    Exit Function
    Default:
    dteRAFACSIn = "18000101"
    GoTo Retry
    End Function

  7. #7
    EHittner is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Nov 2009
    Posts
    32
    I ran the query again using the CDate function as suggested by Ajax. Again, I can view the results and all dates come out perfect. Only when I RUN the query do I get the "Data Type Mismatch in Criteria" error. I've tried isolating the query to just the date field to make sure it wasn't referring to another data field in the query. I've tried switching it to a Make Table query to see if it might have anything to do with the destination table. I've looked at the properties on both sides to make sure they otherwise match. Can view the dataset -- just can't run it.

  8. #8
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,795
    All functions are declared to return a date value. As far as I can tell they should work as used in the query.

    If you want to provide db for analysis, follow instructions at bottom of my post.

    Please post code between CODE tags to retain indentation and readability.
    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.

  9. #9
    Preston is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2017
    Posts
    55
    I sometimes get this error when I try to run a function against a non-conforming date input, meaning there are data that aren't dates. Are you sure all your data passed to the function are dates?

  10. #10
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,795
    Data passed to the function is a string, a date value is supposed to be returned by the 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.

  11. #11
    Preston is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2017
    Posts
    55
    Quote Originally Posted by June7 View Post
    Data passed to the function is a string, a date value is supposed to be returned by the function.
    If that string doesn't conform to the format of a date in any record, you will see the error you describe.

  12. #12
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,795
    The input string can be anything (except Null because string variables cannot hold Null, only variants can) as long as it is a structure expected by the function. For instance, several of the conditions look for the presence of letters "A" or "B". Therefore not all inputs are required to be in "YYYYMMDD" format.

    I tested the dteRegularStyle() function by calling it in the VBA immediate window:

    ?dteRegularStyle("0615A7")

    returns 6/15/2007

    ?dteRegularStyle("")

    returns 12:00:00 AM

    ?dteRegularStyle(Null)

    errors 'invalid use of Null'

    Then I tested in a simple INSERT SELECT query. Both function calls work fine. New records are created


    Receipt_Number is a text type field?
    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.

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

Similar Threads

  1. Replies: 2
    Last Post: 05-04-2017, 01:10 AM
  2. Run time error 13 "type mismatch"
    By CHEECO in forum Access
    Replies: 6
    Last Post: 07-02-2016, 02:59 PM
  3. Replies: 3
    Last Post: 05-31-2013, 04:32 PM
  4. Replies: 2
    Last Post: 05-17-2011, 02:40 PM
  5. "Type Mismatch" Error
    By elmousa68 in forum Access
    Replies: 2
    Last Post: 12-05-2006, 08: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