[vba] Charging connection commandtext in many connections

excelnoob123456

New Member
Joined
Mar 7, 2015
Messages
39
Hi,

I have posted this on another thread but as it it is a different question to the one I started thread for I thought I would start another - if this is not allowed please let me know

Please see below the code that I have so far:

Code:
Dim oldvaluestring As String
Dim newvaluestring As String
Dim num As Integer
Dim connection As String
 
connection = "Connection"
For num = 1 To 9
Name = connection & num
 
'below replaces command text with contents of text boxes
With ActiveWorkbook.Connections(Name).ODBCConnection
   .CommandText = _
      Replace(.CommandText, Worksheets("Update").oldvalue.Value, Worksheets("Update").newvalue.Value)
End With

 oldvaluestring = Worksheets("Update").oldvalue.Value
newvaluestring = Worksheets("Update").newvalue.Value

Next num

'prints messagebox showing items replaced
MsgBox ("Process complete.  Old text """ & oldvaluestring & """ change to """ & newvaluestring & """ in database connection text requested, please check results")
 
End Sub

What I'm trying to do with this code is to get the connection text changed on all of the database connections that are named "Connection#number#' i.e. Connection1, Connection2 etc. Most of these numbers are between 1-100 however there are other numbers going up to the thousands and in total there are around 50-60 connections whose text needs changing.

Now - I've checked the code above and it does work if all of the database connections exist in the string - i.e if I make num just be 1 to 9 it works fine because there exists a connection named Connection1, Connection2 etc. up to connection 9. However, if I try to do 1 to 99 or 1 to 9999 it will come up with an error saying connection not found because not every number in that range is a connection, i.e. there is not a Connection67.

What I can't work out is how to get around this - ideally there would be a way to ask it to skip any that it can't connect to and just do those it can, however if this is not possible I was thinking that I could maybe use an array and manually type all of the connection names into this array then get the macro to cycle through all of these - I have created the array but I can't work out how to get it to cycle through all of the values within it.

By the way, I had to change OLEDBconnection to ODBCconnection because the oledb would not work. Also, if you could work out how to display a different message if the text is not found then that would be greatly appreciated but this is not vital.

Any further help greatly appreciated.

Many thanks
 
Hi sijpie

Yes I have, it comes up with the errors given above unfortunately same as other solution offered. The only thing I can think of is typing the connection names manually and putting them in an array then getting the replace function to cycle through the array. I have typed them up and created the array but I can't work out how to get the replace function to cycle through it unfortunately

Anyone have any ideas regarding this? It's just annoying because I'm so close to having it working
 
Upvote 0

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
This piece of code shows how you could use an arrray with the connection names.

<font face=Calibri><SPAN style="color:#00007F">Option</SPAN> <SPAN style="color:#00007F">Explicit</SPAN><br><br><SPAN style="color:#00007F">Sub</SPAN> ArrayRead()<br>    <SPAN style="color:#00007F">Dim</SPAN> v<br>    <SPAN style="color:#00007F">Dim</SPAN> i <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> rR <SPAN style="color:#00007F">As</SPAN> Range, rC <SPAN style="color:#00007F">As</SPAN> Range<br>    <br>    <SPAN style="color:#007F00">'load the connection names from range in spreadsheet</SPAN><br>    <br>    <SPAN style="color:#00007F">Set</SPAN> rR = Range("R2").CurrentRegion<br>    <br>    <SPAN style="color:#007F00">' set array size to number of rows</SPAN><br>    <SPAN style="color:#00007F">ReDim</SPAN> v(1 <SPAN style="color:#00007F">To</SPAN> rR.Rows.Count)<br>    i = 1<br>    <SPAN style="color:#007F00">'copy each cell into the array</SPAN><br>    <SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Each</SPAN> rC <SPAN style="color:#00007F">In</SPAN> rR<br>        v(i) = rC.Value<br>        i = i + 1<br>    <SPAN style="color:#00007F">Next</SPAN> rC<br>    <br>    <SPAN style="color:#007F00">' Use the array</SPAN><br>    <SPAN style="color:#00007F">For</SPAN> i = 1 <SPAN style="color:#00007F">To</SPAN> <SPAN style="color:#00007F">UBound</SPAN>(v)<br>        MakeConnection (v(i))<br>    <SPAN style="color:#00007F">Next</SPAN> i<br>    <br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br><br><br><SPAN style="color:#00007F">Sub</SPAN> MakeConnection(sConnName <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>)<br>    <SPAN style="color:#00007F">Dim</SPAN> sCommandT <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN><br>    <br>    sCommandT = "Some initial string"<br>    <br>    <SPAN style="color:#00007F">With</SPAN> ActiveWorkbook.Connections(sConnName).ODBCConnection<br>       .CommandText = _<br>          Replace(sCommandT, Worksheets("Update").oldValue.Value, Worksheets("Update").newValue.Value)<br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br>    <br>    <br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br></FONT>


I noticed that your commandtext is empty (unless you initialise it somewhere) you need to check this first
 
Upvote 0
This piece of code shows how you could use an arrray with the connection names.

Option Explicit

Sub ArrayRead()
****Dim v
****Dim i As Integer
****Dim rR As Range, rC As Range
****
****'load the connection names from range in spreadsheet
****
****Set rR = Range("R2").CurrentRegion
****
****' set array size to number of rows
****ReDim v(1 To rR.Rows.Count)
****i = 1
****'copy each cell into the array
****For Each rC In rR
********v(i) = rC.Value
********i = i + 1
****Next rC
****
****' Use the array
****For i = 1 To UBound(v)
********MakeConnection (v(i))
****Next i
****
End Sub


Sub MakeConnection(sConnName As String)
****Dim sCommandT As String
****
****sCommandT = "Some initial string"
****
****With ActiveWorkbook.Connections(sConnName).ODBCConnection
****** .CommandText = _
**********Replace(sCommandT, Worksheets("Update").oldValue.Value, Worksheets("Update").newValue.Value)
****End With
****
****
End Sub



I noticed that your commandtext is empty (unless you initialise it somewhere) you need to check this first

Thanks very much - however, this gives a 'subscript out of range' error for the with 'With ActiveWorkbook.Connections(sConnName).ODBCConnection' line.

Also, regarding the connection text - there are already values in there, and I have created two text boxes (oldvalue and newvalue) which the user can type the value they want replacing and the new value they would like it replacing with - as it is only some of the commandtext that needs replacing.
 
Upvote 0
Have you tried stepping through the code?

Put the cursor somewhere in ArrayRead. Then press the F key. The sub name will turn yellow. Keep pressing the F8 key. This runs each line in turn. The yellow line is the next line to be executed.
When you get to the loop and the line
Rich (BB code):
MakeConnection (v(i))
is yellow hover your mouse over v(i). It will show you which connection name it contains.

Continue pressing F8, it will go to the MakeConnection sub and it will try to make the connection. If you get the 'subscript out of range' error that means that it cannot find this connection. Are your connection names OK?
 
Last edited:
Upvote 0
Have you tried stepping through the code?

Put the cursor somewhere in ArrayRead. Then press the F key. The sub name will turn yellow. Keep pressing the F8 key. This runs each line in turn. The yellow line is the next line to be executed.
When you get to the loop and the line
Rich (BB code):
MakeConnection (v(i))
is yellow hover your mouse over v(i). It will show you which connection name it contains.

Continue pressing F8, it will go to the MakeConnection sub and it will try to make the connection. If you get the 'subscript out of range' error that means that it cannot find this connection. Are your connection names OK?

OK, so I think the problem I had is that I was not correctly selecting the cells that contained by connection names - however, now I have these but I am getting a different error (see attached)


I think the reason for this error is that we are defining what the commandtext is before it is being searched - i.e. when you have the line:
Rich (BB code):
sCommandT = "Some initial string"


All of the connections already have their own, different commandtexts which I want to keep mostly the same, only replacing a few numbers or phrases in them, but I think because we are declaring it in this way it will not work?

Thanks very much for your help I am still learning this so a bit slow at the minute.
 
Upvote 0
Hi sijpie

Yes I have, it comes up with the errors given above unfortunately same as other solution offered. The only thing I can think of is typing the connection names manually and putting them in an array then getting the replace function to cycle through the array. I have typed them up and created the array but I can't work out how to get the replace function to cycle through it unfortunately

sorry forgot error, it's run-time error 1004 with the database
 
Upvote 0
Yes, that is one of the parts​ I don't understand, why you are replacing something in a static text. The other part I don't know much about is this odbc connection and commandstring. So I can't help with that part
 
Upvote 0
Yes, that is one of the parts​ I don't understand, why you are replacing something in a static text. The other part I don't know much about is this odbc connection and commandstring. So I can't help with that part

The reason I am replacing it is because the commandtexts mostly do not need to change, there is only a very small part that needs to change but it needs to change in every single one.
 
Upvote 0
Yes, that is one of the parts​ I don't understand, why you are replacing something in a static text. The other part I don't know much about is this odbc connection and commandstring. So I can't help with that part

Anyone else have any ideas?

Just to clarify - the reason that it is static text is that the commandtext mostly needs to stay the same - there are only a few queries that need changing.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

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