Results 1 to 5 of 5
  1. #1
    Joakim N is offline Advanced Beginner
    Windows 7 32bit Access 2016
    Join Date
    Oct 2016
    Posts
    79

    Action Query Update - SQL with Date

    Hi,

    I have issue including a Date in SQL beneath.

    Following works but gives wrong date (expected) in database:
    UPDATE TStaffAssignedToJob SET JobReport_Sent = 2018-11-14 WHERE ID_StaffAssignedToJob = 7359

    Following doesn't work. I thought it should work. Error message "Incorrect syntax near '#'":


    UPDATE TStaffAssignedToJob SET JobReport_Sent = #2018-11-14# WHERE ID_StaffAssignedToJob = 7359


    Code:
    Public Const gsConn As String = "Provider=SQLOLEDB;" _
                                  & "Data Source=ksds0816b;" _
                                  & "Initial Catalog=IP-Planning;" _
                                  & "Integrated Security=SSPI;"
    
    Sub ActionQuery_Update()
    
        Dim cIP As ADODB.Connection
        Dim sSQL As String
        
        Set cIP = New ADODB.Connection
        cIP.Open gsConn
    
        'Works at "cIP.Execute (sSQL)" but gives wrong date
        'UPDATE TStaffAssignedToJob SET JobReport_Sent = 2018-11-14 WHERE ID_StaffAssignedToJob = 7359
        sSQL = "UPDATE TStaffAssignedToJob SET JobReport_Sent = " & Date _
        & " WHERE ID_StaffAssignedToJob = " & 7359
        '...
        cIP.Execute (sSQL)
        
        'Create Error at "cIP.Execute (sSQL)"
        'UPDATE TStaffAssignedToJob SET JobReport_Sent = #2018-11-14# WHERE ID_StaffAssignedToJob = 7359
        sSQL = "UPDATE TStaffAssignedToJob SET JobReport_Sent = #" & Date & "#" _
        & " WHERE ID_StaffAssignedToJob = " & 7359
        '...
        cIP.Execute (sSQL)
    
        cIP.Close
        Set cIP = Nothing
    End Sub

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,522
    y-m-d , thats not really a date format,
    use: m/d/y

    and sql wants it surrounded by # . (like in your second sql)

  3. #3
    Minty is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,002
    If you are sending this as a SQL Server command then the date would need to be sent as

    Code:
    '2018-11-14'
    In Access you can safely use this format
    Code:
    #2018/11/14#
    in any environment / locale
    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 ↓↓

  4. #4
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,973
    In fact you can also use the yyyy-mm-dd format safely in Access SQL.
    One advantage of that format is that dates can be easily sorted visually.
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

  5. #5
    Joakim N is offline Advanced Beginner
    Windows 7 32bit Access 2016
    Join Date
    Oct 2016
    Posts
    79
    Thanks all,

    This worked. I had to change " # " to " ' ":
    sSQL = "UPDATE TStaffAssignedToJob SET JobReport_Sent = '" & Date & "'" & " WHERE ID_StaffAssignedToJob = " & 7359

    (Maybe I wasn't clear enough in my original info. My Front End is in Access. My databases are stored on SQL Server. However, the code above shall be used from VBA Excel.)

    Thanks again!

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

Similar Threads

  1. Date action
    By Perfac in forum Programming
    Replies: 2
    Last Post: 05-13-2018, 07:02 PM
  2. Show Date Action First Appeared
    By ecoman007 in forum Queries
    Replies: 3
    Last Post: 08-17-2017, 09:16 AM
  3. Replies: 3
    Last Post: 09-02-2014, 06:45 PM
  4. query to update a date based on another date
    By tkendell in forum Access
    Replies: 3
    Last Post: 06-29-2011, 09:32 PM
  5. Replies: 2
    Last Post: 10-01-2009, 03:39 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