Results 1 to 3 of 3
  1. #1
    UPSDuder is offline Novice
    Windows 10 Access 2013 64bit
    Join Date
    Feb 2019
    Posts
    23

    Need help with date formatting when using SQL

    For some reason I am unable to get this part of my code correct. I am using the SQL Replace expression, and when doing so it changes the format of the date it pulls in from yyyy-mm-dd to mm/dd/yyyy. I do not want this to happen as my query will not run with this type of input. I formatted the column in my table as a Date yyyy-mm-dd format, but that does not make a difference.



    I have tried to used Date( dmgt![date_1]), "yyyy-mm-dd" but I get the error "expected: end of statement

    Anyone have advice on how I can keep my formatting when using replace ?

    Code:
    Function ReRun()
    DoCmd.SetWarnings False
        Dim db As Database
        Dim rs As Recordset
        
        Dim useSQL As QueryDef
        Dim srtdte As String
        Set db = CurrentDb
        Set dmgt = db.OpenRecordset("dateTable", dbOpenTable)
        
        Set useSQL = db.QueryDefs("Vol_baseline")
        
        DoCmd.runSQL "Delete*From [VolHistory]"
       
        
            'Asign the date for the query, using the field
            srtdte = dmgt![date_1]
            useSQL.SQL = Replace(useSQL.SQL, "[StartDate]", srtdte)
            'Run query here
            DoCmd.OpenQuery "Vol_baseline", acViewNormal, acEdit
            'Append query results into results table
            db.Execute " INSERT INTO VolHistory " & " SELECT * " & " FROM  Vol_baseline;"
            
               useSQL.SQL = Replace(useSQL.SQL, srtdte, "[StartDate]")
     
                For Each qry In CurrentDb.QueryDefs
                On Error Resume Next
                DoCmd.Close acQuery, qry.Name, acSaveYes
                Next
             
                      Call DoCmd.TransferSpreadsheet(acExport, _
                      acSpreadsheetTypeExcel12, "VolHistory", strExcelPath, _
                      False, "Vol_History")
       
        DoCmd.SetWarnings True
        
    End Function

  2. #2
    UPSDuder is offline Novice
    Windows 10 Access 2013 64bit
    Join Date
    Feb 2019
    Posts
    23
    I stumbled upon another person having the same issue, the line below solved my issue.

    srtdte = Format(dmgt![date_1], "yyyy-mm-dd")

  3. #3
    ssanfu is offline Master of Nothing
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Another option:
    Scroll down to Dates in Strings (by Allen Browne)

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

Similar Threads

  1. Formatting date in a query
    By accessmatt in forum Queries
    Replies: 1
    Last Post: 02-01-2016, 02:57 PM
  2. Replies: 3
    Last Post: 12-02-2014, 07:58 PM
  3. Have I missed something with date formatting?
    By Ruegen in forum Programming
    Replies: 8
    Last Post: 11-12-2013, 04:02 PM
  4. Date Conditional Formatting
    By f15avionics in forum Access
    Replies: 3
    Last Post: 09-21-2012, 04:45 AM
  5. Formatting a date
    By NISMOJim in forum Queries
    Replies: 7
    Last Post: 09-17-2010, 05:44 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