Hi all, please tell me if this is the wrong forum for this. I have some code which I sort of understand and would be grateful if someone could help me break it down (explain the sections to me in simple terms pls)
I would also be grateful if you could tell me if the code is 'good' (meaning layout and memory usage etc) Could I improve it or am I re-inventing the wheel.
Thanks in advance
I would also be grateful if you could tell me if the code is 'good' (meaning layout and memory usage etc) Could I improve it or am I re-inventing the wheel.
Thanks in advance
Code:
Sub emailsConnection()
Dim cnn As ADODB.Connection
Dim MyRecordSet As ADODB.Recordset
Dim emailSqlString1 As String
Set cnn = New ADODB.Connection
dateFromSQL = Sheets("Update").Range("D4").Value
dateToSQL = Sheets("Update").Range("D4").Value + 7
' Set properties of the Connection.
cnn.ConnectionString = "Provider=xxxxxx;Data Source=xxxxxx;user ID=xxxxxx;password=xxxxxx;"
cnn.ConnectionTimeout = 90
' Open the connection.
cnn.Open
' Find out if the attempt to connect worked.
If cnn.State = adStateOpen Then
Else
MsgBox "Sorry. Connection Failed"
End If
'Sql String here dynamic
Dim emailSglString As String
emailSqlString1 = "SELECT TRUNC(creationdate) AS create_date, assignedto, COUNT(*) " & _
"FROM xxxxxx WHERE channel = 'outbound email' AND lower(status) ='closed' AND " & _
"creationdate BETWEEN TO_DATE('" & dateFromSQL & "','dd/mm/yyyy') AND TO_DATE('" & _
dateToSQL & "','dd/mm/yyyy') GROUP BY TRUNC(creationdate), assignedto"
'Sql string temp
'sqlString = emailSqlString1
'MsgBox (sqlString)
Set MyRecordSet = New ADODB.Recordset
MyRecordSet.Open emailSqlString1, cnn, adOpenDynamic, adLockOptimistic
'ClearPrevoiusData
Sheets("emailSQL").Select
Range("A3:M65536").Select
Selection.ClearContents
'Destination
Sheets("emailSQL").Cells(3, 1).CopyFromRecordset MyRecordSet
' Close the connection.
cnn.Close
Set cnn = Nothing
Set MyRecordSet = Nothing
Last edited by a moderator: