Dim sql As String
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim i As Integer
Set cn = New ADODB.Connection
'cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=d:/teste/Sistema_Metta_Shering2000.mdb"
'cn.Open
cn.ConnectionString = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;User ID=sa;Initial Catalog=Metta;Data Source=MAQ03"
cn.Open
'define um novo objeto recordset
Set rs = New ADODB.Recordset
Selection.HorizontalAlignment = xlCenter
Selection.VerticalAlignment = xlCenter
'Centraliza o texta da célula
Range("A1").HorizontalAlignment = xlCenter
Range("A1").VerticalAlignment = xlCenter
Range("B1").HorizontalAlignment = xlCenter
Range("B1").VerticalAlignment = xlCenter
'define cor de fundo na célula
'Range("A13").Select
'With Selection.Interior
' .ColorIndex = 15
' .Pattern = xlSolid
'End With
'define a cor da letra na célula
'Range("a1").Font.ColorIndex = 15
''define a instrução sql
sql = "SELECT Count(pedido.codigo_pedido) AS ContarDecodigo_pedido, pedido.eqz "
sql = sql & "FROM cadastrovendedor ,pedido "
sql = sql & "WHERE pedido.codigo_vendedor *= cadastrovendedor.codigo_vendedor "
sql = sql & "AND Pedido.Data2 >= " & Format$(Trim(cmbDiaI.text) & "/" & Trim(cmbMesI.text) & "/" & Trim(cmbAnoI.text), 103) & " "
sql = sql & "AND Pedido.Data2 <= " & Format$(Trim(cmbDiaF.text) & "/" & Trim(cmbMesF.text) & "/" & Trim(cmbAnoF.text), 103) & " "
sql = sql & "GROUP BY pedido.eqz, cadastrovendedor.divisao "
sql = sql & "ORDER BY cadastrovendedor.divisao "
'sql = "SELECT Count(Pedido.Codigo_Pedido) AS ContarDeCodigo_Pedido, Pedido.eqz "
'sql = sql & "FROM CadastroVendedor INNER JOIN Pedido "
'sql = sql & "ON Pedido.Codigo_Vendedor = CadastroVendedor.Codigo_Vendedor "
'sql = sql & "WHERE Pedido.Data2 >= " & Format$(Trim(cmbMesI.text) & "/" & Trim(cmbDiaI.text) & "/" & Trim(cmbAnoI.text), "dd/mm/yyyy") & " "
'sql = sql & "AND Pedido.Data2 <= " & Format$(Trim(cmbMesF.text) & "/" & Trim(cmbDiaF.text) & "/" & Trim(cmbAnoF.text), "dd/mm/yyyy") & " "
'sql = sql & "GROUP BY Pedido.eqz, cadastrovendedor.divisao "
'sql = sql & "ORDER BY CadastroVendedor.Divisao "
'MsgBox sql
'rs.Open sql, cn
'define o cabeçalho das células no excel
Range("A1").Value = "Código do pedido"
Range("B1").Value = "EQZ"
i = 2
If Not rs.EOF Then
Do While Not rs.EOF
Range("A" & i).Value = rs(0)
Range("B" & i).Value = rs(1)
rs.MoveNext
i = i + 1
Loop
End If
cn.Close