Aquí les dejo un ejemplo de como importar las tablas de una base de datos de SQL server, este ejemplo el nombre las tablas y el servidor lo coloque en una tabla de Access entonces accedo a esa tabla atravez de ADO.Recordset y recorre los registros hasta terminar.
Sub SQLImport()
On Error GoTo error
Dim NombreTabla As String ‘Nombre de la tabla
Dim Base As String ‘base de datos
Dim registros As Integer
Dim contador As Integer
Dim TABLAS_RPM_T4M As ADODB.Recordset
Dim Importar As ADODB.Command
Dim DropTable As ADODB.Command
Dim origen As String ‘origen de la tabla
Set TABLAS_RPM_T4M = New ADODB.Recordset
With TABLAS_RPM_T4M
.ActiveConnection = CurrentProject.Connection
.CursorType = adOpenKeyset
.LockType = adLockOptimistic
.Open «SELECT TABLA,BASE FROM TABLAS_RPM_T4M»
End With
registros = TABLAS_RPM_T4M.RecordCount
For contador = 1 To registros
Set Importar = New ADODB.Command
Set DropTable = New ADODB.Command
NombreTabla = TABLAS_RPM_T4M.Fields(0)
Base = TABLAS_RPM_T4M.Fields(1)
With DropTable
.ActiveConnection = CurrentProject.Connection
.CommandText = «drop table » & NombreTabla
.Execute
End With
origen = «[ODBC;Driver=SQL Server;» & _
«SERVER=CME042;» & _
«DATABASE=» & Base & «;» & _
«Integrated Security=SSPI;].[» & NombreTabla & «]»
With Importar
.ActiveConnection = CurrentProject.Connection
.CommandText = «SELECT * INTO » & NombreTabla & » FROM » & origen
.Execute
End With
If TABLAS_RPM_T4M.EOF And TABLAS_RPM_T4M.BOF Then
Exit Sub
Else
TABLAS_RPM_T4M.MoveNext
End If
Set Importar = Nothing
Set DropTable = Nothing
Next
DoCmd.Echo True
Set Importar = Nothing
Set DropTable = Nothing
Set TABLAS_RPM_T4M = Nothing
Exit Sub
error:
MsgBox error, vbCritical
MsgBox «Problema al crear imagen de » & NombreTabla, vbCritical
DoCmd.Echo True
Set Importar = Nothing
Set DropTable = Nothing
Set TABLAS_RPM_T4M = Nothing
End Sub