Macro to update a section of the connection string but not the whole string

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>
 
Last edited:

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
please re-post your code, it is unreadable the way it is presented

click "go-advanced" then click # to get the code tags, then paste the code between the tags
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,219
Members
452,619
Latest member
Shiv1198

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top