Conexión a base de datos

' Contenido del formulario:
'
' - 1 ComboBox
' - 1 commandButton
' - En caso de error agregar el componente adodc.

Option Explicit

Private cnn As ADODB.Connection
Private rst As ADODB.Recordset

Private Sub Command1_Click()
Dim tField As ADODB.Field
'
' Por si ya estaba abierta...
Set cnn = Nothing
Set rst = Nothing
'
' Crear los objetos
Set cnn = New ADODB.Connection
Set rst = New ADODB.Recordset
'
' abrir la base usando las credenciales de Windows

cnn.Open "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=True;Initial Catalog=pubs"
'
' abrir el recordset indicando la tabla a la que queremos acceder
rst.Open "SELECT * FROM authors", cnn, adOpenDynamic, adLockOptimistic
'
' Asignar los nombres de los campos al combo
With Combo1
.Clear
For Each tField In rst.Fields
.AddItem tField.Name
Next
.ListIndex = 0
End With
'
' Cerrar el recordset y la conexión
rst.Close
cnn.Close
'
End Sub