我的电子表格与源数据的SQL连接。由于这是新的一年,我需要查询的年份部分读取1/1/14而不是1/1/13。但是,查询的标识部分对于每个工作簿都是唯一的。我找不到一种方法来更新连接字符串中写入日期的部分,而不更新整个连接字符串 - 这将使所有其他工作簿的唯一标识与我在宏中记录的工作簿相同。
在下面的示例中,我希望宏将RVU_all.Post_date>=
行更改为1/1/2014,这就是全部。没有wrvu_all.NPI=
在宏中(因此我的宏在其他工作簿中对于不同的人而言是无用的但是相同的任务)。
Workbooks.Open Filename:= _
"G:\(Where I store my file)
, UpdateLinks:=0
With ActiveWorkbook.Connections("Query from XXXXXX").ODBCConnection
.BackgroundQuery = False
.CommandText = Array( _
"SELECT (data I'm looking for)
"FROM (My Sql Table)" & Chr(13) & "" & Chr(10) & "WHERE (w" _
, _
"RVU_all.Post_date>={ts *'2014-01-01 00:00:00'*}) AND (wRVU_all.NPI='UniqueNumber')" & Chr(13) & "" & Chr(10) & "ORDER BY wRVU_all.Post_date" _
)
.CommandType = xlCmdSql
.Connection = Array(Array( _
"My Connection string to the table" _
), Array("BASE=XXXXXX"))
.RefreshOnFileOpen = False
.SavePassword = True
.SourceConnectionFile = ""
.SourceDataFile = ""
.ServerCredentialsMethod = xlCredentialsMethodIntegrated
.AlwaysUseConnectionFile = False
End With
With ActiveWorkbook.Connections("Query from XXXXXXX")
.Name = "Query from XXXXXXXX"
.Description = ""
End With
ActiveWorkbook.Connections("Query from XXXXXXX").Refresh
ActiveWorkbook.Save
ActiveWindow.Close
End Sub
答案 0 :(得分:1)
我会做一些像:
.commandtext(1)=replace(.commandtext(1),"your original date","your new date")
那应该做的工作!