How to List CommandText from Connections

mgoebel97

New Member
Joined
Oct 23, 2014
Messages
10
I have about 30 connections connected to different excel sheets within 1 workbook. I want to know how to get the command text for each connection using VBA code.

This is what I have thus far:
Function Connections()


Dim conn As WorkbookConnection
Dim celltxt As String


For Each conn In ActiveWorkbook.Connections
Range("A" & ActiveCell.Row) = conn.Name
Range("B" & ActiveCell.Row) = ActiveWorkbook.Connections(conn).ODBCConnection.CommandText
Selection.Offset(1, 1).Select


Next conn


End Function

The conn.name works great and I get the connection name but the command text is not working. I keep getting an error message. I dont want to go through each connection to find out what table it is connecting to in Access. I want to see if there is code within VBA to do it for me.
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
try:
ActiveWorkbook.Connections(conn).ConnectionString

Hi,

Thank you for the reply. I changed my code to this:
Function Connections()

Dim conn As WorkbookConnection
Dim celltxt As String

For Each conn In ActiveWorkbook.Connections
Range("A" & ActiveCell.Row) = conn.Name
Range("B" & ActiveCell.Row) = ActiveWorkbook.Connections(conn).ConnectionString
Selection.Offset(1, 1).Select

Next conn

End Function


but now I "Subscript out of range"
 
Upvote 0
i missed that...
either:
conn.connectionstring
or
ActiveWorkbook.Connections(1).ConnectionString

Note:
Connections(N) ,the n is numeric, 1..N
but Conn is an object so:
conn.Connectionstring

 
Last edited:
Upvote 0
HI
Tried with activeworkbook and without. Same error message: Object does not support this property or method

Function Connections()
Dim conn As WorkbookConnection
Dim celltxt As String
For Each conn In ActiveWorkbook.Connections
Range("A" & ActiveCell.Row) = conn.Name
Range("B" & ActiveCell.Row) = conn.ConnectionString OR Range("B" & ActiveCell.Row) = activeworkbook.conn.ConnectionString
Selection.Offset(1, 1).Select
Next conn
End Function
 
Upvote 0

Forum statistics

Threads
1,223,237
Messages
6,170,924
Members
452,366
Latest member
TePunaBloke

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