通过VBA将日期格式更改为SQL查询

Jam*_*ieB 0 sql sql-server excel vba date

我有一个问题,我一直无法回答,我的问题是我有一个宏使用SQL查找来访问SQL数据库并提取信息.在excel中,总是将日期强制为DD/MM/YYYY(你可以强制格式化,但是当它传递到SQL方面时它仍然以DD/MM/YYYY形式出现,即使格式在视觉上看起来正确).

我已尝试对我的代码进行一些更改以尝试"强制"它然而我没有运气我是否使其复杂化或是否应该更难做到这一点?大声笑.

我将提供我的VB代码和"属性",以了解使用宏创建的"连接".

需要注意的是,格式需要为YYYY-MM-DD,因为这是它存储在数据库中的方式.目前解决这个问题的唯一方法是使用日期前面的"'",例如'2013-12-01强制它,否则它将进入2013年12月1日.

有任何想法吗?我已经绞尽脑汁太久了哈哈.

关心杰米

如果需要该信息,则服务器是SQLEXPRESS服务器.

代码如下:

Sub CustomisedSQLQuery()
'
' SQL Query to allow user customisation. 
'

'
Dim FileName As String
Dim User As String
Dim StartDate As String
Dim EndDate As String
Dim Category As String


Dim Confirm As Variant
Confirm = MsgBox("Have you made sure that at least one of the search criteria's is populated? If so your excel may crash or you may kill the database.", vbQuestion + vbYesNo, "Wait....")
If Confirm = vbNo Then ActiveWorkbook.Sheets("Input Sheet").Activate
If Confirm = vbNo Then Exit Sub



FileName = Worksheets("Input Sheet").Cells(10, 1)
User = Worksheets("Master DATA").Cells(1, 1)
StartDate = Worksheets("Input Sheet").Cells(10, 3)
EndDate = Worksheets("Input Sheet").Cells(10, 4)
Category = Worksheets("Master DATA").Cells(1, 5)

MyStr = Format(StartDate, "yyyy/mm/dd")
MyStr = Format(EndDate, "yyyy/mm/dd")

    Sheets("Output Sheet").Select
    Cells.Select
    Selection.ClearContents
    Range("A1").Select
    With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
        "ODBC;DRIVER=SQL Server;SERVER=SERVERADDRESS;UID=USERNAME;PWD=PASSWORD;APP=Microsoft Office 2010;WSID=ID" _
        , Destination:=Range("$A$1")).QueryTable
        .CommandText = Array( _
        "SELECT DocumentsRead.userID, DocumentsRead.fileName, DocumentsRead.category, DocumentsRead.downloadedByUser, DocumentsRead.timeDownloaded, DocumentsRead.timeR" _
        , _
        "ead" & Chr(13) & "" & Chr(10) & "FROM EndUsers.dbo.DocumentsRead DocumentsRead" & Chr(13) & "" & Chr(10) & "WHERE (DocumentsRead.fileName Like '" & FileName & "') AND (DocumentsRead.category='" & Category & "') AND (DocumentsRead.timeRead Is Null) " _
        , "AND (DocumentsRead.timeDownloaded Between {ts '" & StartDate & " 00:00:01'} An" _
        , "d {ts '" & EndDate & " 00:00:01'})")
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .BackgroundQuery = True
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .PreserveColumnInfo = True
        .Refresh BackgroundQuery:=False
    End With

Sheets("Input Sheet").Select
End Sub
Run Code Online (Sandbox Code Playgroud)

这是SQL的连接属性

SELECT DocumentsRead.userID, DocumentsRead.fileName, DocumentsRead.category, DocumentsRead.downloadedByUser, DocumentsRead.timeDownloaded, DocumentsRead.timeRead
FROM EndUsers.dbo.DocumentsRead DocumentsRead
WHERE (DocumentsRead.fileName Like 'GB') AND (DocumentsRead.category='Notices') AND (DocumentsRead.timeRead Is Null) AND (DocumentsRead.timeDownloaded Between {ts '01/12/2013 00:00:01'} And {ts '08/11/2013 00:00:01'})
Run Code Online (Sandbox Code Playgroud)

输入表如下所示:

高强

小智 6

看起来您的问题在于StartDate/EndDate的格式.这是你的代码:

MyStr = Format(StartDate, "yyyy/mm/dd")
MyStr = Format(EndDate, "yyyy/mm/dd")
Run Code Online (Sandbox Code Playgroud)

以下是我假设您要做的事情:

StartDate = Format(StartDate, "yyyy-mm-dd")
EndDate = Format(EndDate, "yyyy-mm-dd")
Run Code Online (Sandbox Code Playgroud)

我相信你也可以通过将其包装在#中来告诉Access字符串是一个日期.如

"and DocumentsRead.timeDownloaded >= #" & StartDate & "#" & vbcrlf & _
"and DocumentsRead.timeDownloaded <  #" & EndDate & "#"
Run Code Online (Sandbox Code Playgroud)