-1

How to name ADO connection with VBA? Using ADO method of the following VBA code adds new connection which is named by default "Connection" or "Connection2" etc.

Sub Make_ADO_Connection()
Sheets(2).Cells.ClearContents

Dim oCn As ADODB.Connection
Dim oRS As ADODB.Recordset
Dim ConnString As String
Dim SQL As String

strFile = ThisWorkbook.FullName
ConnString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & strFile & ";Extended Properties=""Excel 12.0;HDR=Yes;IMEX=1"";"
Debug.Print ConnString

Set oCn = New ADODB.Connection
oCn.ConnectionString = ConnString
oCn.Open

SQL = "select asdf1,asdf2,asdf5 from [Arkusz1$] where asdf1 is not null"

Set oRS = New ADODB.Recordset
oRS.Source = SQL
oRS.ActiveConnection = oCn
oRS.Open

'-------------------
'Here new connection is established with a name "Connection", I want to use a different name for it.
Set objListObject = ActiveWorkbook.Sheets(2).ListObjects.Add(SourceType:=xlSrcExternal, _
Source:=oRS, LinkSource:=True, _
TableStyleName:=xlGuess, Destination:=Sheets(2).Range("A1"))
objListObject.Refresh

If oRS.State <> adStateClosed Then
oRS.Close
End If

If Not oRS Is Nothing Then Set oRS = Nothing
If Not oCn Is Nothing Then Set oCn = Nothing

End Sub

I would like to name new ADO connection while creating it i.e. "MyConnectionName" checking earlier if such a connection exists.

Edit. I can loop through the connections:

Dim conn As WorkbookConnection
For Each conn In ActiveWorkbook.Connections
    Debug.Print conn.Name 'This name I want to set up
Next conn

I know that the connection with name "Connection" is created with this statements:

Set objListObject = ActiveWorkbook.Sheets(2).ListObjects.Add(SourceType:=xlSrcExternal, _
Source:=oRS, LinkSource:=True, _
TableStyleName:=xlGuess, Destination:=Sheets(2).Range("A1"))

objListObject.Refresh
3
  • What do you mean 'name ADO connection'? Do you mean how to name to object? Commented Nov 17, 2015 at 11:40
  • @Gareth probably yes, please check for Edit of my question. Commented Nov 17, 2015 at 11:55
  • I'm confused - I don't see how any of the answers in the linked thread could create a data connection in your workbook. Commented Nov 17, 2015 at 11:59

1 Answer 1

1

To Create a connection to a DB with ADO use something like this:

Public Sub TestQueryInvokation()

Dim oCnn As New ADODB.Connection
Dim oCmd As New ADODB.Command

oCnn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\TestDB.accdb;Persist Security Info=False;"

oCmd.ActiveConnection = oCnn
oCmd.CommandText = "Query1"
Call oCmd.Execute

End Sub

This is different from the connection stuff you're looking at there. If you import data using the Excel ribbon it creates a connection and maintains a link to the DB and puts it in that collection you've shown. I've experienced problems with this approach in the past and would recommend you create your own.

You need a reference to the ADO library. Alternatively type everything as Object and you'll not need the reference.

Sign up to request clarification or add additional context in comments.

9 Comments

(1) How to assign other parameters to your style connection? They are the same? I mean the SQL string, OpenRowset etc? (2) Can you please give an example of "typing everything as Object" - this is particularly interesting.
By the way, this is the same for your example of using Excel like a DB. Use an ADO class and no need to refresh any links etc.
The example shows how to connect for an update type query. You've already defined the connection string in your example for use the ACE driver. So in you're code, after you open the recordset use something like CopyFromRecordSet to copy to the range and then close the recordset. Don't use what you have after this w.r.t. Connection collections.
What about typing the Object?
Public Sub TestQueryInvokation() Dim oCnn As Object Dim oCmd As Object set oCnn = CreateObject("ADODB.Connection") set oCmd = CreateObject("ADODB.Command") oCnn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\TestDB.accdb;Persist Security Info=False;" oCmd.ActiveConnection = oCnn oCmd.CommandText = "Query1" Call oCmd.Execute End Sub
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.