De excell a access
Publicado por krlos (10 intervenciones) el 07/06/2005 21:56:34
Este codigo puede ser de ayuda aunque agradezco su co laboracion haber si alguien sabe como hacer para que me permita seguir ejecutandolo ya que si repite una hora no se quiere ejecutar.
Mi idea es de que agrege datos a una tabla donde tengo 10,000 registros...
Private Sub cmdExcel_Click()
Dim Conexion As ADODB.Connection, _
rstExcel As ADODB.Recordset, _
rstaccess As Recordset, _
strArchivo As String, _
strTabla As String, _
strSQL As String
'Variables para usar DAO
Dim dbs As Database
Set dbs = CurrentDb
'rstAccess As ADODB.Recordset
On Error GoTo cmdExcel_Click_TratamientoErrores
Set rstaccess = dbs.OpenRecordset("PREDESPACHO2", dbOpenTable)
' asigno la ruta del libro Excel
strArchivo = Application.CurrentProject.Path & "\Prueba.xls"
' asigno el nombre de la hoja o rango a abrir,
' si quieres leer un rango con nombre, pon el nombre del rango en vez del de la hoja y borra-> & "$"
strTabla = "Predespacho" & "$"
Set Conexion = New ADODB.Connection
Set rstExcel = New ADODB.Recordset
' abro la conexión con la hoja de calculo
Conexion.Provider = "Microsoft.Jet.OLEDB.4.0"
Conexion.ConnectionString = "Data Source=" & strArchivo & ";Extended Properties=""Excel 8.0;HDR=Yes;"""
Conexion.CursorLocation = adUseClient
Conexion.Open
' abro el Recordset
With rstExcel
.ActiveConnection = Conexion
.CursorLocation = adUseClient
.CursorType = adOpenStatic
.LockType = adLockReadOnly
.Open "SELECT * FROM [" & strTabla & "]", , , , adCmdText
End With
' abro el recordset de la tabla
' si el recordset Excel no está vacío, inserto los datos en la tabla
If Not rstExcel.BOF And Not rstExcel.EOF Then
Do While Not rstExcel.EOF
' buscas el registro,
' si no existe lo insertas y si existe lo editas
If rstaccess.NoMatch Then
rstaccess.AddNew
Else
rstaccess.Edit
End If
rstaccess!hora = rstExcel!hora
rstaccess!FECHA = rstExcel!FECHA
rstaccess!CD = rstExcel!CD
rstaccess.Update
rstExcel.MoveNext
Loop
End If
' cierro los recordsets y la conexión con la hoja
If Not rstExcel Is Nothing Then
rstExcel.Close
Set rstExcel = Nothing
End If
If Not rstaccess Is Nothing Then
rstaccess.Close
Set rstaccess = Nothing
End If
Set Conexion = Nothing
Set dbs = Nothing
cmdExcel_Click_Salir:
On Error GoTo 0
Exit Sub
cmdExcel_Click_TratamientoErrores:
MsgBox "Error " & Err.Number & " en proc. cmdExcel_Click de Documento VBA Form_frmLeerExcelSinExcel (" & Err.Description & ")", vbOKOnly + vbCritical
GoTo cmdExcel_Click_Salir
End Sub
Agradezco su colaboracion...
Original por Rickhunters y emilio..
Mi idea es de que agrege datos a una tabla donde tengo 10,000 registros...
Private Sub cmdExcel_Click()
Dim Conexion As ADODB.Connection, _
rstExcel As ADODB.Recordset, _
rstaccess As Recordset, _
strArchivo As String, _
strTabla As String, _
strSQL As String
'Variables para usar DAO
Dim dbs As Database
Set dbs = CurrentDb
'rstAccess As ADODB.Recordset
On Error GoTo cmdExcel_Click_TratamientoErrores
Set rstaccess = dbs.OpenRecordset("PREDESPACHO2", dbOpenTable)
' asigno la ruta del libro Excel
strArchivo = Application.CurrentProject.Path & "\Prueba.xls"
' asigno el nombre de la hoja o rango a abrir,
' si quieres leer un rango con nombre, pon el nombre del rango en vez del de la hoja y borra-> & "$"
strTabla = "Predespacho" & "$"
Set Conexion = New ADODB.Connection
Set rstExcel = New ADODB.Recordset
' abro la conexión con la hoja de calculo
Conexion.Provider = "Microsoft.Jet.OLEDB.4.0"
Conexion.ConnectionString = "Data Source=" & strArchivo & ";Extended Properties=""Excel 8.0;HDR=Yes;"""
Conexion.CursorLocation = adUseClient
Conexion.Open
' abro el Recordset
With rstExcel
.ActiveConnection = Conexion
.CursorLocation = adUseClient
.CursorType = adOpenStatic
.LockType = adLockReadOnly
.Open "SELECT * FROM [" & strTabla & "]", , , , adCmdText
End With
' abro el recordset de la tabla
' si el recordset Excel no está vacío, inserto los datos en la tabla
If Not rstExcel.BOF And Not rstExcel.EOF Then
Do While Not rstExcel.EOF
' buscas el registro,
' si no existe lo insertas y si existe lo editas
If rstaccess.NoMatch Then
rstaccess.AddNew
Else
rstaccess.Edit
End If
rstaccess!hora = rstExcel!hora
rstaccess!FECHA = rstExcel!FECHA
rstaccess!CD = rstExcel!CD
rstaccess.Update
rstExcel.MoveNext
Loop
End If
' cierro los recordsets y la conexión con la hoja
If Not rstExcel Is Nothing Then
rstExcel.Close
Set rstExcel = Nothing
End If
If Not rstaccess Is Nothing Then
rstaccess.Close
Set rstaccess = Nothing
End If
Set Conexion = Nothing
Set dbs = Nothing
cmdExcel_Click_Salir:
On Error GoTo 0
Exit Sub
cmdExcel_Click_TratamientoErrores:
MsgBox "Error " & Err.Number & " en proc. cmdExcel_Click de Documento VBA Form_frmLeerExcelSinExcel (" & Err.Description & ")", vbOKOnly + vbCritical
GoTo cmdExcel_Click_Salir
End Sub
Agradezco su colaboracion...
Original por Rickhunters y emilio..
Valora esta pregunta


0