slipandfall99
New Member
- Joined
- Jan 6, 2014
- Messages
- 2
My spreadsheets have a connection to SQL for source data. Since it is a new year I need the year part of the query to read 1/1/14 instead of 1/1/13. However the identification part of the query is unique to each workbook. I cannot find a way to update just the section of the connection string where the date is written without updating the entire connection string - which would then make the unique identification of all other workbooks be the same as the one I record in the macro.
In the example below I want the macro to change the <CODE>RVU_all.Post_date>=</CODE> line to 1/1/2014 and that is all. Not have the <CODE>wrvu_all.NPI=</CODE> be in the macro (thus making my macro useless on other workbooks for different people but the same task).
Code:
<code>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 = FalseEnd WithWith ActiveWorkbook.Connections("Query from XXXXXXX") .Name = "Query from XXXXXXXX" .Description = ""End WithActiveWorkbook.Connections("Query from XXXXXXX").RefreshActiveWorkbook.SaveActiveWindow.CloseEnd Sub<Code>
</PRE>
In the example below I want the macro to change the <CODE>RVU_all.Post_date>=</CODE> line to 1/1/2014 and that is all. Not have the <CODE>wrvu_all.NPI=</CODE> be in the macro (thus making my macro useless on other workbooks for different people but the same task).
Code:
<code>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 = FalseEnd WithWith ActiveWorkbook.Connections("Query from XXXXXXX") .Name = "Query from XXXXXXXX" .Description = ""End WithActiveWorkbook.Connections("Query from XXXXXXX").RefreshActiveWorkbook.SaveActiveWindow.CloseEnd Sub<Code>
</PRE>
Last edited: