Results 1 to 3 of 3
  1. #1
    uoghk is offline Competent Performer
    Windows 10 Office 365
    Join Date
    Jan 2022
    Posts
    149

    cannot query sql server table with smalldatetime type

    I have a Invoice table in SQL server. It has a TxDate field in SmallDateTime type.



    I get error : type mismatch in below code.
    Code:
    Set Rst = Db.OpenRecordset("SELECT * FROM Invoice WHERE TxDate='" & Format(Me.TxDate,'yyyy-mm-dd') & "'", dbOpenDynaset, dbSeeChanges)
    If I change the condition as below
    Code:
    Set Rst = Db.OpenRecordset("SELECT * FROM Invoice WHERE CAST(TxDate AS CHAR(10))='" & Format(Me.TxDate,'yyyy-mm-dd') & "'", dbOpenDynaset, dbSeeChanges)
    Then I get error : missing operator in query expression.

  2. #2
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,250
    The Format function returns a string so I think that is the root of your problem. And you cannot use CAST in Access, you would use CStr to convert to string. If your TxDate in SQL includes minutes and you want to ignore those you should use the DateValue() function:
    Code:
    Set Rst = Db.OpenRecordset("SELECT * FROM Invoice WHERE DateValue(TxDate)=#" & CDate(Me.TxDate) "#", dbOpenDynaset, dbSeeChanges)
    or even:
    Code:
    Set Rst = Db.OpenRecordset("SELECT * FROM Invoice WHERE DateValue(TxDate)=#" & Me.TxDate "#", dbOpenDynaset, dbSeeChanges)
    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  3. #3
    uoghk is offline Competent Performer
    Windows 10 Office 365
    Join Date
    Jan 2022
    Posts
    149
    Thanks Gicu.

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

Similar Threads

  1. sql server query to local table
    By SteveApa in forum SQL Server
    Replies: 3
    Last Post: 09-08-2016, 01:31 PM
  2. attachment data type in SQL Server
    By Esmatullaharifi in forum SQL Server
    Replies: 3
    Last Post: 05-15-2015, 12:07 AM
  3. Replies: 14
    Last Post: 05-08-2014, 12:51 PM
  4. How to Define a Hyperlink Data Type in SQL Server
    By EddieN1 in forum SQL Server
    Replies: 3
    Last Post: 02-07-2013, 11:26 AM
  5. Access and SQL Server data type problem
    By rayc in forum Access
    Replies: 1
    Last Post: 09-10-2010, 07:27 PM

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