Boa tarde Raphael, obrigado pela resposta tão rapida.
Este é meu Modulo 2, modulo que gostaria que ficasse a condição. A partir daqui poderia continuar a exemplificação?!
VLw
Public Function ConectarSQL() As Boolean
Dim timeout As Integer
On Error GoTo trataErro
timeout = 0
Set connSQLSERVER = New ADODB.Connection
strDataSoure = "STVSAWN1441"
strDataBase = "UKPRD1418"
strUser = "CEDATABASE"
strPWD = "Dsa98765"
'Aqui é montada a string de conexão
strConexao = "Provider=SQLOLEDB.1;SERVER=" & strDataSoure
strConexao = strConexao & ";DATABASE=" & strDataBase
strConexao = strConexao & ";UID=" & strUser
strConexao = strConexao & ";PWD=" & strPWD
'Criando um objeto de conexão com a base de dados e executa a Query
connSQLSERVER.Open strConexao
' Cria o objeto Recordset.
Dim rsstrConexao As ADODB.Recordset
Set rsstrConexao = New ADODB.Recordset
With rsstrConexao
' Associa a conexão.
.ActiveConnection = strConexao
' Extrai os dados.
.Open "SELECT BEL_DIVISAO.NomeDivisao, BEL_REPRESENTANTE.Territorio, BEL_REPRESENTANTE.NomeRepresentante, BEL_REPRESENTANTE.MudId, BEL_REPRESENTANTE.Logradouro, BEL_REPRESENTANTE.Complemento, BEL_REPRESENTANTE.Cidade, BEL_REPRESENTANTE.UF, BEL_REPRESENTANTE.Telefone, BEL_REPRESENTANTE.Celular, BEL_REPRESENTANTE.Email FROM UKPRD1418.dbo.BEL_DIVISAO BEL_DIVISAO, UKPRD1418.dbo.BEL_REPRESENTANTE BEL_REPRESENTANTE WHERE BEL_DIVISAO.IdDivisao = BEL_REPRESENTANTE.IdDivisao"
' Coloca os dados na planilha.
Range("A5").Select
ActiveCell.FormulaR1C1 = "Divisão"
Range("B5").Select
ActiveCell.FormulaR1C1 = "Território"
Range("C5").Select
ActiveCell.FormulaR1C1 = "Representante"
Range("D5").Select
ActiveCell.FormulaR1C1 = "MUDID"
Range("E5").Select
ActiveCell.FormulaR1C1 = "Logradouro"
Range("F5").Select
ActiveCell.FormulaR1C1 = "Complemento"
Range("G5").Select
ActiveCell.FormulaR1C1 = "Cidade"
Range("H5").Select
ActiveCell.FormulaR1C1 = "UF"
Range("I5").Select
ActiveCell.FormulaR1C1 = "Telefone"
Range("J5").Select
ActiveCell.FormulaR1C1 = "Celular"
Range("L5").Select
ActiveCell.FormulaR1C1 = "E-mail"
Range("K5").Select
Range("A6").CopyFromRecordset rsstrConexao
End With
' Fecha conexão
'ConectarSQL = True
Exit Function
trataErro:
'timeout
If Err.Number = -2147467259 And timeout < 5 Then
timeout = timeout + 1
Resume
Else
MsgBox Err.Number & " - " & Err.Description
Debug.Print Err.Number
ConectarSQL = False
Resume Next
End If