Change SQL with VBA

sh1pley

Board Regular
Joined
Dec 22, 2006
Messages
160
I have to change the SQL on hundreds of queries. This involves changing the server, doing a find and replace on the command text which I have managed to do with the below code.
However I also need to add a line onto the command text.... but if this line already says TRANTYPE='SEP06' then I need to leave it as it is.

New line to add:
AND (D_DETAILS.TRANTYPE='DEFAULT'))

Sub ChangeSQL()

Dim OldConn As String
Dim NewConn As String

OldConn = "ODBC;DRIVER=SQL Server;SERVER=BLISS001;UID=report user;PWD=reportuser;APP=Microsoft Office XP;WSID=BLISW016;DATABASE=live;Network=DBNMPNTW;"
NewConn = "ODBC;DRIVER=SQL Server;SERVER=BLISSSQL01;UID=report user;;APP=Microsoft Office XP;WSID=BLISW016;DATABASE=DREAM;"

Sheet5.QueryTables.Item("Query3").Connection = Replace(Sheet5.QueryTables.Item("Query3").Connection, OldConn, NewConn)

Sheet5.QueryTables.Item("Query3").CommandText = Replace(Sheet5.QueryTables.Item("Query3").CommandText, "live", "DREAM")
Sheet5.QueryTables.Item("Query3").CommandText = Replace(Sheet5.QueryTables.Item("Query3").CommandText, "M_APBUDGET", "M_BALANCE")
Sheet5.QueryTables.Item("Query3").CommandText = Replace(Sheet5.QueryTables.Item("Query3").CommandText, "BUDGETCODE", "TRANTYPE")
Sheet5.QueryTables.Item("Query3").CommandText = Replace(Sheet5.QueryTables.Item("Query3").CommandText, "M_BALANCE2", "M_BALANCE")

Sheet5.QueryTables.Item("Query3").Refresh

End Sub
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Hello,

How about testing the CommandText Property to see if contains your target String with the Instr() function? See the following:

http://msdn2.microsoft.com/en-us/library/8460tsh1(VS.80).aspx

You could apply this test with an If Statement. ;)
 
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,552
Members
449,088
Latest member
davidcom

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