Hi,
I have written the code below (fudged together!) to firstly look at a column in my excel sheet, fill the column into an array/string. Then run an SQL query using the array/string to pull through relevant records into excel.
The query works, however, it can be a long list of criteria that is in my excel column and it is really slow to run it or if there are too many in the list it times out.
Am just looking for some advice to see if there is a better way to do this or structure my code to make it run more efficiently???
Sub TestData()
Dim conn As Variant
Dim rs As Variant
Dim cs As String
Dim query As String
Dim row As Integer
Dim Rng As Range
Dim cnt As Long
Dim p As Long, e As Long
Dim Param As String
Sheets("Data_Selection_1").Range("A3:H65536").ClearContents
j = Sheets("Date_&_Skill_Selections").Range("D1000").End(xlUp).row
For i = 2 To j
cnt = cnt + 1
If cnt = 1 Then
Param = Sheets("Date_&_Skill_Selections").Cells(i, 4)
Else: Param = Param & "','" & Sheets("Date_&_Skill_Selections").Cells(i, 4)
End If
Next i
Param = "'" & Param & "'" & Sheets("Date_&_Skill_Selections").Cells(i, 4)
'MsgBox Param
Set conn = CreateObject("adodb.connection")
Set rs = CreateObject("adodb.recordset")
cs = "DRIVER=SQL Server;"
cs = cs & "DATABASE=TEST;"
cs = cs & "SERVER=1.223.56.79,9101"
conn.Open cs, "USERNAME", "PASSWORD"
Set cn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")
query = " select StartTime, sum(ACD_Calls), sum(ABN_Calls_ALL), sum(Calls_Offered), Skill, Date "
query = query & "from reporting.dbo.REPORT_AVAYA_SKILL "
query = query & "where convert(char(12),Date)+ convert(varchar(12),Skill) IN (" & Param & ")"
query = query & "group by StartTime, ACD_Calls, ABN_Calls_ALL, Calls_Offered, Skill, Date "
rs.Open query, conn
row = Sheets("Data_Selection_1").Range("A1").End(xlDown).row + 1
Sheets("Data_Selection_1").Cells(row, 1).CopyFromRecordset rs
rs.Close
Set rs = Nothing
Set conn = Nothing
Set cn = Nothing
End Sub
I have written the code below (fudged together!) to firstly look at a column in my excel sheet, fill the column into an array/string. Then run an SQL query using the array/string to pull through relevant records into excel.
The query works, however, it can be a long list of criteria that is in my excel column and it is really slow to run it or if there are too many in the list it times out.
Am just looking for some advice to see if there is a better way to do this or structure my code to make it run more efficiently???
Sub TestData()
Dim conn As Variant
Dim rs As Variant
Dim cs As String
Dim query As String
Dim row As Integer
Dim Rng As Range
Dim cnt As Long
Dim p As Long, e As Long
Dim Param As String
Sheets("Data_Selection_1").Range("A3:H65536").ClearContents
j = Sheets("Date_&_Skill_Selections").Range("D1000").End(xlUp).row
For i = 2 To j
cnt = cnt + 1
If cnt = 1 Then
Param = Sheets("Date_&_Skill_Selections").Cells(i, 4)
Else: Param = Param & "','" & Sheets("Date_&_Skill_Selections").Cells(i, 4)
End If
Next i
Param = "'" & Param & "'" & Sheets("Date_&_Skill_Selections").Cells(i, 4)
'MsgBox Param
Set conn = CreateObject("adodb.connection")
Set rs = CreateObject("adodb.recordset")
cs = "DRIVER=SQL Server;"
cs = cs & "DATABASE=TEST;"
cs = cs & "SERVER=1.223.56.79,9101"
conn.Open cs, "USERNAME", "PASSWORD"
Set cn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")
query = " select StartTime, sum(ACD_Calls), sum(ABN_Calls_ALL), sum(Calls_Offered), Skill, Date "
query = query & "from reporting.dbo.REPORT_AVAYA_SKILL "
query = query & "where convert(char(12),Date)+ convert(varchar(12),Skill) IN (" & Param & ")"
query = query & "group by StartTime, ACD_Calls, ABN_Calls_ALL, Calls_Offered, Skill, Date "
rs.Open query, conn
row = Sheets("Data_Selection_1").Range("A1").End(xlDown).row + 1
Sheets("Data_Selection_1").Cells(row, 1).CopyFromRecordset rs
rs.Close
Set rs = Nothing
Set conn = Nothing
Set cn = Nothing
End Sub