Question : Add querytable with parameters

I have a problem in relation to using parameters passed from excel to vba, then recalculated in vba into a querytable.

My problem is that i simply dont understand the syntax in regards to adding a querytable and parameters well enough.

The code below, works well (allthough i know it probably could be done in a more efficient way) except for the part about the parameters.
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
Dim Fromdate As Date
Dim Todate As Date
 
Dim Employee As String
Dim Row As Integer
Dim RowNext As Integer
 
Dim Pm1 As Parameter
Dim Pm2 As Parameter
' Recalculation of dates from Excel sheet (date entered as (2008-06-01) 
' displayed as jun-2008 in excel to make things easier
' i need the Fromdate to be 2008-05-21 and the Todate 2008-06-02
Fromdate = DateAdd("m", -1, Range("b50").Value)
Fromdate = DateAdd("d", 20, Fromdate)
Fromdate = Format(Fromdate, "yyyy-mm-dd")
Todate = DateAdd("d", 19, Range("b50").Value)
Todate = Format(Todate, "yyyy-mm-dd")
 
RowNext = 3
 
Do Until Range("A" & RowNext).Value = "Timeløn"
 
Row = RowNext
Employee = Range("A" & Row).Value
Employee = StrConv(Employee, vbLowerCase)
 
 
    CnString = "ODBC;DATABASE=achievo_1_2;DESCRIPTION=Achievo;DSN=AchievoDB;OPTION=0;;PORT=0;SERVER=achievo.itq.dk;UID=dbacker;CHARSET=latin1"
    SqlString = " SELECT SUM(CASE hours.activityid WHEN '4' THEN convert(timediff(hours.endtime,hours.starttime),decimal)+convert(minute(timediff(hours.endtime,hours.starttime)),decimal)/60 ELSE 0 END)/7.5 AS 'sygedage', " & _
                " SUM(CASE hours.activityid WHEN '6' THEN convert(timediff(hours.endtime,hours.starttime),decimal)+convert(minute(timediff(hours.endtime,hours.starttime)),decimal)/60 ELSE 0 END)/7.5 AS 'BarnSyg', " & _
                " SUM(CASE hours.activityid WHEN '5' THEN convert(timediff(hours.endtime,hours.starttime),decimal)+convert(minute(timediff(hours.endtime,hours.starttime)),decimal)/60 ELSE 0 END)/7.5 AS 'Ferie' " & _
                " FROM  achievo_1_2.hours hours " & _
                " inner join achievo_1_2.person on hours.userid=person.id " & _
                " WHERE   hours.activitydate>=? and hours.activitydate<=? and person.userid='" & Employee & "'"
 
    
    
     With ActiveSheet.QueryTables.Add(Connection:=CnString, Destination:=Range("J" & Row), _
        Sql:=SqlString)
    .Name = "Test"
    .FieldNames = False
    .RowNumbers = False
    .FillAdjacentFormulas = False
    .PreserveFormatting = True
    .RefreshOnFileOpen = False
    .BackgroundQuery = True
    .RefreshStyle = xlOverwriteCells
    .SavePassword = True
    .SaveData = False
    .AdjustColumnWidth = False
    .RefreshPeriod = 0
    .PreserveColumnInfo = True
    End With
  
' where and how should i add these lines ? There is also a problem in  relation to naming the Querytables
    Set Pm1 = ActiveSheet.QueryTables.Parameters.Add("Fromdate", xlParamTypeDate)
    Pm1.SetParam xlConstant, Fromdate
    Set Pm2 = Activesheets.QueryTables.Parameters.Add("Todate", xlParamTypeDate)
    Pm2.SetParam xlConstant, Todate
 
    
    ActiveSheet.QueryTables.Add(Connection:=CnString, Destination:=Range("J" & Row), _
        Sql:=SqlString).Refresh BackgroundQuery:=False
    
 
    
RowNext = Row + 1
 
Loop

Answer : Add querytable with parameters

Try something like this:

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
Dim Fromdate As Date
Dim Todate As Date
 
Dim Employee As String
Dim Row As Integer
Dim RowNext As Integer
Dim qt As QueryTable
Dim Pm1 As Parameter
Dim Pm2 As Parameter
' Recalculation of dates from Excel sheet (date entered as (2008-06-01)
' displayed as jun-2008 in excel to make things easier
' i need the Fromdate to be 2008-05-21 and the Todate 2008-06-02
Fromdate = DateAdd("m", -1, Range("b50").Value)
Fromdate = DateAdd("d", 20, Fromdate)
Fromdate = Format(Fromdate, "yyyy-mm-dd")
Todate = DateAdd("d", 19, Range("b50").Value)
Todate = Format(Todate, "yyyy-mm-dd")
 
RowNext = 3
 
Do Until Range("A" & RowNext).Value = "Timeløn"
 
Row = RowNext
Employee = Range("A" & Row).Value
Employee = StrConv(Employee, vbLowerCase)
 
 
    CnString = "ODBC;DATABASE=achievo_1_2;DESCRIPTION=Achievo;DSN=AchievoDB;OPTION=0;;PORT=0;SERVER=achievo.itq.dk;UID=dbacker;CHARSET=latin1"
    SqlString = " SELECT SUM(CASE hours.activityid WHEN '4' THEN convert(timediff(hours.endtime,hours.starttime),decimal)+convert(minute(timediff(hours.endtime,hours.starttime)),decimal)/60 ELSE 0 END)/7.5 AS 'sygedage', " & _
                " SUM(CASE hours.activityid WHEN '6' THEN convert(timediff(hours.endtime,hours.starttime),decimal)+convert(minute(timediff(hours.endtime,hours.starttime)),decimal)/60 ELSE 0 END)/7.5 AS 'BarnSyg', " & _
                " SUM(CASE hours.activityid WHEN '5' THEN convert(timediff(hours.endtime,hours.starttime),decimal)+convert(minute(timediff(hours.endtime,hours.starttime)),decimal)/60 ELSE 0 END)/7.5 AS 'Ferie' " & _
                " FROM  achievo_1_2.hours hours " & _
                " inner join achievo_1_2.person on hours.userid=person.id " & _
                " WHERE   hours.activitydate>=? and hours.activitydate<=? and person.userid='" & Employee & "'"
 
    
    
     Set qt = ActiveSheet.QueryTables.Add(Connection:=CnString, Destination:=Range("J" & Row), _
        Sql:=SqlString)
      With qt
    .Name = "Test"
    .FieldNames = False
    .RowNumbers = False
    .FillAdjacentFormulas = False
    .PreserveFormatting = True
    .RefreshOnFileOpen = False
    .BackgroundQuery = True
    .RefreshStyle = xlOverwriteCells
    .SavePassword = True
    .SaveData = False
    .AdjustColumnWidth = False
    .RefreshPeriod = 0
    .PreserveColumnInfo = True
' where and how should i add these lines ? There is also a problem in  relation to naming the Querytables
      With .Parameters
         Set Pm1 = .Add("Fromdate", xlParamTypeDate)
         Pm1.SetParam xlConstant, Fromdate
         Set Pm2 = .Add("Todate", xlParamTypeDate)
         Pm2.SetParam xlConstant, Todate
      End With
   .Refresh
    End With
RowNext = Row + 1
 
Loop
Random Solutions  
 
programming4us programming4us