I am sure this is bad practice, but this is my syntax (would love pointers and re-writes on how to more efficiently perform this task) - what I do in the 1st method is query a database to get fullpathnames - then in the second method I again connect to SQL Server to query a database and get table names. Then I print the table names (in this example). My thought is, I am sure it has to be performance hog opening a connection twice, is there a way to re-write or alter this to make it more efficient?
Code:
Public Function GetDatabases()
Dim c As ADODB.Connection,r As ADODB.Recordset,conn As String
Set c = New ADODB.Connection
Dim accapp As Database.Application
Set accapp = New Database.Application
With c
.Provider = "sqloledb.1"
With .Properties
.Item("Data Source") = "Server"
.Item("Initial Catalog") = "Database"
.Item("PassWord") = "password"
.Item("User ID") = "username"
End With
.Open
Set r = c.Execute("SELECT Top 5 fullpath from dblocs")
r.MoveFirst
Do Until r.EOF
Debug.Print r.Fields(0)
accapp.OpenCurrentDatabase (r.Fields(0))
accapp.Visible = True
'Calling method here with open connection and same variables
CheckTabs
Set accapp = Nothing
r.MoveNext
Loop
r.Close
Set rst = Nothing
c.Close
End With
End Function
Public Function CheckTabs()
Dim c As ADODB.Connection, r As ADODB.Recordset, f As ADODB.Field, conn As String
Set c = New ADODB.Connection
With c
.Provider = "sqloledb.1"
With .Properties
.Item("Data Source") = "Server"
.Item("Initial Catalog") = "Database"
.Item("PassWord") = "password"
.Item("User ID") = "username"
End With
.Open
Set r = .OpenSchema(adSchemaTables, Array(Empty, Empty, Empty, "TABLE"))
With r
While Not .EOF
If (.Fields("TABLE_TYPE") = "TABLE" And .Fields("TABLE_NAME") Like "BlueBell*") Then
Debug.Print Mid(.Fields("Table_Name")
End If
.MoveNext
Wend
End With
End With
End Function