View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Jamie Collins Jamie Collins is offline
external usenet poster
 
Posts: 593
Default Return MS-SQL database names to excel?

<M.Streeks wrote ...

I have a workbook to which I want to return the database names residing in a
specific SQL database. I need the names in order to allow a user to choose a
database name.


I assume you mean, 'return the database names residing on a specific
SQL Server.'

You can use ADO's OpenSchema method to do this e.g.

Option Explicit

Sub TestLateBound()

Const CONN_STRING As String = "" & _
"Provider=SQLOLEDB.1;" & _
"Data Source=MYSERVER;" & _
"User Id=MYLOGON;password=MYPASSWORD;"

Dim oConn As Object
Dim oRs As Object

Set oConn = CreateObject("ADODB.Connection")

With oConn
.CursorLocation = 3 ' adUseClient
.ConnectionString = CONN_STRING
.Open
Set oRs = .OpenSchema(1) ' adSchemaCatalogs
End With

Dim lngRows As Long
Dim lngCounter As Long
With oRs
lngRows = .RecordCount
For lngCounter = 0 To lngRows - 1
Debug.Print !CATALOG_NAME
.MoveNext
Next
.Close
End With
oConn.Close

End Sub

If you meant, 'return the table names residing in a specific SQL
Server database', you can also use OpenSchema (using adSchemaTables).
For details, see:

http://msdn.microsoft.com/library/de...schemaenum.asp

If you additionally need to enumerate SQL Servers, take a look at this
thread from the archive:

http://groups.google.com/groups?thre...ing.google.com

Jamie.

--