Ver Mensaje Individual
  #1 (permalink)  
Antiguo 10/02/2012, 16:47
Crack_x01
 
Fecha de Ingreso: agosto-2011
Ubicación: Edo. de Mexico
Mensajes: 12
Antigüedad: 12 años, 9 meses
Puntos: 0
Pregunta Macro Excel Consulta a SQL con intervalos de tiempo

Hola buen dia...

Tengo un problema con una macro que cree, no me manda error en la ejecucion, pero, como ejemplo, en lugar de regresarme 1600 registros de la consulta, solo me regresa de 45 a 300 registros y esta cantidad cambia con cada ejecucion de la macro.

================================================== =======

Sub Switch_Selling(sql As String, consulta As String)

Sheets("Switch_Selling").Select
Cells.Select
Selection.ClearContents
Range("A1").Select

Dim Connection1 As Object
Dim rst As Object
Dim rst1 As Object
Dim sql1 As String
HoraInicio = Time()

Set Connection1 = CreateObject("ADODB.Connection")
Connection1.CommandTimeout = 0

**** AQUI VA LA CADENA DE CONEXION A LA DB****

If sql <> vbNullString And consulta <> vbNullString Then

Connection1.Open
Set rst = CreateObject("ADODB.Recordset")
rst.Open sql, Connection1, , 1, 0

AQUI IRIA EL PRIMER INTERVALO DE 1.5 MIN

sql1 = "SELECT CicloVentas, RTrim(Territorio) Territorio, CodigoCliente, NombreCliente, Estado, Municipio, Canal, Subcanal, PotencialCliente, Subject, MKSOLICITADA, MKCOMPRADA, LEALTADSS, EDADSS, GENERO, ISNULL(CONVERT(VARCHAR(16), VisitEndDateTime, 120), '') FROM ##Reporte ORDER BY 1, 2, 3"

Set rst1 = CreateObject("ADODB.Recordset")
rst1.Open sql1, Connection1, , 1, 0

AQUI IRIA EL SEGUNDO INTERVALO DE 4 MIN

C = 0
F = 0

Sheets("Switch_Selling").Select

'recorre las columnas, añade el nombre del campo al encabezado
For i = 0 To rst1.Fields.Count - 1
'Si se nos acaba el Alfabeto incluimos este if O_o
If i >= 26 Then
ext = 0
ext = i - 26
Sheets("Switch_Selling").Range((Chr(65) & Chr(ext + 65)) & F + 1).Value = rst1.Fields(i).Name
Else
Sheets("Switch_Selling").Range(Chr(i + 65) & F + 1).Value = rst1.Fields(i).Name
End If
Next

F = F + 1

' recorre todo el recordset hasta el final
Do While Not rst1.EOF

' recorre los campos en el registro actual del recordset para recuperar el dato
For i = 0 To rst1.Fields.Count - 1
'Si se nos acaba el Alfabeto incluimos este if O_o
If C >= 26 Then
ext = 0
ext = C - 26
Sheets("Switch_Selling").Range((Chr(65) & Chr(ext + 65)) & F + 1).Value = rst1.Fields(C)
Else
' añade el valor a la celda
Sheets("Switch_Selling").Range(Chr(C + 65) & F + 1).Value = rst1.Fields(C)
End If
C = C + 1
Next

' resetea el indice de las columnas
C = 0
' Referencia al registro actual (incrementa )
F = F + 1
' Siguiente registro
rst1.MoveNext
Loop
' cierra y descarga las referencias
On Error Resume Next
rst.Close
rst1.Close
Connection1.Close
Set Connection1 = Nothing
Set rst1 = Nothing
Set rst = Nothing

End If

MsgBox "El proceso ha concluido" & vbCrLf & vbTab & _
DateDiff("s", HoraInicio, Time()) & " Segundos", vbInformation, "PROCESO CONCLUIDO"

Sheets("Sheet1").Select
Range("A1").Select

End Sub

================================================== =====

Estaba pensando que tal vez la macro corre mas rapido que lo que tarda la consulta de SQL en extraer la informacion, por lo que pensaba meterle intervalos de tiempo despues de cada ejecucion de consultas, pero no se que tan eficiente me resulte esto ó si el error este en el codigo, aparte de que no se como crear esos intervalos de tiempo; los intervalos irian en los textos rojos.

***NOTA: La consulta directa en SQL tarda 4.5 min en regresarme el resultado***

Les agradeceria mucho su ayuda.