Question : VBA/Excel - How to replace date value with date variable from text box?

In the code below, I am currently using string values from 2 textboxes from a form to replace the username and password in the ODBC connection string.  While this works, I am trying to replace the date value in the SQL string which is not working.  When I step through the macro, it is fails at .Refresh BackgroundQuery:=False.   I listed the code that currently works with the original date value and the code with what I thought would work with the date variable from the textbox.  

Thank you.
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:
70:
Code that does work:
 
Sub Automation()
 
Dim strPass As String
Dim strUID As String
 
UserForm1.Show
 
strUID = UserName
strPass = Password
 
With ActiveSheet.QueryTables.Add(Connection:= _
        "ODBC;DSN=Prodbank;Pwd=" & strPass & ";DB=repository;UID=" & strUID & ";", Destination:=Sheets("sheet1").Range("A24"))
        .CommandText = Array( _
                "SELECT pdp_swap_dv01.val_name, pdp_swap_dv01.amount, pdp_swap_dv01.npv_0, pdp_swap_dv01.dv01, pdp_swap_dv01.prod_date" & Chr(13) & "" & Chr(10) & "FROM repository.dbo.pdp_swap_dv01 pdp_swap_dv01" & Chr(13) & "" & Chr(10) & "WHERE (pdp_swap_dv01.prod_date={" _
        , "ts '2009-11-30 00:00:00'})")
        .Name = "DV_02"
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .BackgroundQuery = True
        .RefreshStyle = xlOverwriteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .PreserveColumnInfo = True
        .Refresh BackgroundQuery:=False
    End With
End Sub
 
Code that does not work:
 
Sub Automation()
 
Dim strPass As String
Dim strUID As String
Dim strEOM As String
 
UserForm1.Show
 
strUID = UserName
strPass = Password
strEOM = EOM
strEOM = Format(Date, "yyyy-mm-dd h:mm")
 
With ActiveSheet.QueryTables.Add(Connection:= _
        "ODBC;DSN=Prodbank;Pwd=" & strPass & ";DB=repository;UID=" & strUID & ";", Destination:=Sheets("sheet1").Range("A24"))
        .CommandText = Array( _
        "SELECT pdp_swap_dv01.val_name, pdp_swap_dv01.amount, pdp_swap_dv01.npv_0, pdp_swap_dv01.dv01, pdp_swap_dv01.prod_date" & Chr(13) & "" & Chr(10) & "FROM repository.dbo.pdp_swap_dv01 pdp_swap_dv01" & Chr(13) & "" & Chr(10) & "WHERE (pdp_swap_dv01.prod_date=(" & strEOM & ")")
 
        .Name = "DV_02"
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .BackgroundQuery = True
        .RefreshStyle = xlOverwriteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .PreserveColumnInfo = True
        .Refresh BackgroundQuery:=False
    End With
End Sub

Answer : VBA/Excel - How to replace date value with date variable from text box?

Your date variable is prefaced with "str" implying it is a string. If it is a date type then this should resolve the problem:

"WHERE (pdp_swap_dv01.prod_date={ts '" & Format(strEOM, "yyyy-mm-dd hh:mm:ss") & "'})"

or, if the time is supposed to always be midnight:

"WHERE (pdp_swap_dv01.prod_date={ts '" & Format(strEOM, "yyyy-mm-dd") & "' 00:00:00})"

Kevin
Random Solutions  
 
programming4us programming4us