2014-01-17 225 views
0

我有一個問題,我一直無法回答,我的問題是,我有一個使用SQL查找來訪問SQL數據庫並提取信息的宏。在excel中它總是強制日期到DD/MM/YYYY(你可以強制格式化,但是當它傳遞到SQL的一面時,它仍然以DD/MM/YYYY出現,即使格式看起來正確)。通過VBA更改日期格式爲SQL查詢

我試過了一些對我的代碼的更改,試圖「強制」它,但是我沒有運氣,我是在複雜化它還是很難做到這一點?大聲笑。

我將提供我的VB代碼和「屬性」,以表明使用宏創建「連接」的外觀。

作爲一個註釋,格式需要是YYYY-MM-DD,因爲這是它存儲在數據庫中的方式。目前,解決這個問題的唯一方法是使用日期的「'」infront作爲例如'2013-12-01'來強制它,否則它會到2013年12月1日。

任何想法?我一直在絞盡腦汁,因爲太久了。

問候傑米

Server是一個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 

這是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'}) 

輸入表如下所示:

Excel

+0

你是說當單元格更新時,單元格上的任何格式都被刪除,表格恢復爲默認的excel日期? – user3056839

回答

6

它看起來像你的問題是在StartDate/EndDate的格式中。這裏是你的代碼:

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

以下是我假設你想要做的:

StartDate = Format(StartDate, "yyyy-mm-dd") 
EndDate = Format(EndDate, "yyyy-mm-dd") 

我相信你也可以告訴訪問字符串是一個日期在包裝#它。如

"and DocumentsRead.timeDownloaded >= #" & StartDate & "#" & vbcrlf & _ 
"and DocumentsRead.timeDownloaded < #" & EndDate & "#" 
+0

這工作,只需要另一套眼睛:)。非常感謝Pal。 – JamieB