本示範將利用 OpenSchema 取得資料表的名稱及類別
Public Sub OpenSchemaX()
On Error GoTo ErrorHandler
Dim Conn As ADODB.Connection
Dim rstSchema As ADODB.Recordset
Dim strConn As String
Set Conn = New ADODB.Connection
strConn = "Provider='sqloledb';Data Source='MySqlServer';" & _
"Initial Catalog='Pubs';Integrated Security='SSPI';"
Conn.Open strConn
Set rstSchema = Conn.OpenSchema(adSchemaTables)
Do Until rstSchema.EOF
Debug.Print "名稱: " & rstSchema!TABLE_NAME & vbCr & _
"類別: " & rstSchema!TABLE_TYPE & vbCr
rstSchema.MoveNext
Loop
rstSchema.Close
Conn.Close
Set rstSchema = Nothing
Set Conn = Nothing
Exit Sub
ErrorHandler:
If Not rstSchema Is Nothing Then
If rstSchema.State = adStateOpen Then rstSchema.Close
End If
Set rstSchema = Nothing
If Not Conn Is Nothing Then
If Conn.State = adStateOpen Then Conn.Close
End If
Set Conn = Nothing
If Err <> 0 Then
MsgBox Err.Source & "-->" & Err.Description, , "Error"
End If
End Sub