Results 1 to 13 of 13
  1. #1
    ExcessionOCP is offline Advanced Beginner
    Windows 11 Office 365
    Join Date
    May 2023
    Posts
    31

    Type mismatch on datetime2 datatype after migrating from Access to SQL

    Hi,

    I have just migrated the back end of my application from an Access DB to an Azure SQL instance. The transition has mostly gone fine but I am encountering a problem with a VB function stored in the front-end that I cannot fix.

    I have the following code in the function:



    Code:
    Dim StartYear as Int
    Dim Itemqry as String
    Dim itemRS as Recordset
    
    itemqry = "Select lineitemID, purchaseprice, dateofpurchase from lineitems WHERE ExcludeFromOH = False"
    
    
    Set itemsrs = dbs.OpenRecordset(itemqry, dbOpenDynaset, dbSeeChanges)
    
    
    itemsrs.MoveLast
    totalcount = itemsrs.RecordCount
    itemsrs.MoveFirst
    
    
    
    
    With itemsrs
      
        Do Until .EOF
    
    
        StartYear = Year(itemsrs.Fields(2))
        Do some more stuff
    
    End with
    The code is failing with a type mismatch error on the
    Code:
    StartYear = Year(itemsrs.Fields(2))
    line, where I take the year component of the "DateofPurchase" column in the LineItems table and store it in the StartYear variable for use later in the function.

    Doing some debugging, I have noticed that the datatype for this column in the SQL database has been set to "datetime2" by the migration tool, whereas it is just "date/time" in the Access DB. I have dumped the value of itemsrs.fields(2) into a debug window in both systems and in the Access DB it is returning, for example:

    Code:
    15/12/2022
    Whereas in the SQL DB it is returning:

    Code:
    15/12/2022 00:00:00.000000
    Which is where I suspect the problem is.

    Does anyone have any suggestions for how I overcome this? Is there a different way I should be using to extract the year component from the date field? Or do I somehow need to update all of the values in the SQL DB column to remove the trailing zeros?

    Hoping someone can help

  2. #2
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    Change the field to datetime?
    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

  3. #3
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    Year function should work with anything that resembles a date, albeit calculations should be in US date format.
    Or take the Left 10 of the field?
    Or wrap the value in date delimiters?
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  4. #4
    ExcessionOCP is offline Advanced Beginner
    Windows 11 Office 365
    Join Date
    May 2023
    Posts
    31
    Thanks for the reply. I'd already tried that, but the result was still the same (both in terms of what is returned in debug.print, and the type mismatch).

  5. #5
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 11 Office 365
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,654
    datetime2 stores the data differently than datetime.

    Unless you need the precision of datetime2, I'd stick with datetime.
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  6. #6
    ExcessionOCP is offline Advanced Beginner
    Windows 11 Office 365
    Join Date
    May 2023
    Posts
    31
    Thanks for the reply! I've tried switching it to datetime and also to just date but the trailing zeros are still present when I inspect the recordset entry, even though they aren't shown in the table - I'm really stumped!

  7. #7
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    So using Left is one thing you tried that didn't work?
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  8. #8
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 11 Office 365
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,654
    Did you update your ODBC Driver?

    Enabled Date/time extended in access?
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  9. #9
    madpiet is offline Competent Performer
    Windows 10 Office 365
    Join Date
    Feb 2023
    Posts
    154

    why not this way?

    Code:
    itemqry = "Select lineitemID, purchaseprice, YEAR(dateofpurchase) AS PurchaseYear from lineitems WHERE ExcludeFromOH = False"
    Since YEAR() returns an integer, no more date problem.

  10. #10
    ExcessionOCP is offline Advanced Beginner
    Windows 11 Office 365
    Join Date
    May 2023
    Posts
    31
    Quote Originally Posted by madpiet View Post
    Code:
    itemqry = "Select lineitemID, purchaseprice, YEAR(dateofpurchase) AS PurchaseYear from lineitems WHERE ExcludeFromOH = False"
    Since YEAR() returns an integer, no more date problem.

    Of course! Why didn't I think of that! Thanks very much, I'll give that a go and report back.

  11. #11
    ExcessionOCP is offline Advanced Beginner
    Windows 11 Office 365
    Join Date
    May 2023
    Posts
    31
    Quote Originally Posted by moke123 View Post
    Did you update your ODBC Driver?

    Enabled Date/time extended in access?
    I've enabled the extended Date/Time Access option, the ODBC driver is a good point though. I'll check to see how up-to-date it is then I'll try implementing madpiet's idea (don't want to change two things at once and all that!)

  12. #12
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    Avoid using the data type DateTime2 with Access like the plague.
    There are a lot of weird problems with it even with the most up to date ODBC drivers, and the latest version of Access.

    We have even seen things like a default value (Set in the SQL table) of Getdate() failing on precision issues.
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  13. #13
    ExcessionOCP is offline Advanced Beginner
    Windows 11 Office 365
    Join Date
    May 2023
    Posts
    31
    This turned out to be the answer! Thanks madpiet!

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

Similar Threads

  1. Replies: 5
    Last Post: 10-07-2020, 12:07 AM
  2. Replies: 2
    Last Post: 07-13-2016, 08:28 AM
  3. Replies: 2
    Last Post: 07-08-2015, 04:07 PM
  4. DLookUp DataType Mismatch Error
    By theosgood in forum Programming
    Replies: 2
    Last Post: 10-29-2013, 10:04 AM
  5. datatype mismatch in criteria expression
    By CyberSkillsz in forum Access
    Replies: 1
    Last Post: 06-14-2011, 10:56 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