SQL Query reference cell

Dharris144

Board Regular
Joined
Jul 22, 2009
Messages
97
I have a sheet with a query that i need to change the date every month. This is ok for me but not an average end user
Is it possible to change DECLARE @D Date Set @D = '05/31/2017' to DECLARE @D Date Set @D = Cell reference here ?
Or something to that affect

Excel 2016
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
See if this VBA example helps:


Code:
Sub Macro2()
Dim d$
d = Format([p55], "yyyy-mm-dd")                                                     ' from cell P55
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:=Array(Array( _
"ODBC;DSN=MS Access Database;DBQ=C:\Users\Eddie\Desktop\Campaign_Template13.mdb;" & _
"DefaultDir=C:\Users\Eddie\Desktop;DriverId=25;FIL=MS " _
), Array("Access;MaxBufferSize=2048;PageTimeout=5;")), Destination:=Range("$v$61")).QueryTable
.CommandText = Array( _
"SELECT Campaign_Table.ProductID, Campaign_Table.Date_Added" & Chr(13) & "" & Chr(10) & _
"FROM `C:\Users\Eddie\Desktop\Campaign_Template13.mdb`.Campaign_Table Campaign_Table" & Chr(13) & _
"" & Chr(10) & "WHERE (Campaign_Table.Date_Added>{ts '" & d & " 00:00:", "00'})" & Chr(13) & "" & _
Chr(10) & "ORDER BY Campaign_Table.Date_Added")
    .RowNumbers = False
    .FillAdjacentFormulas = False
    .PreserveFormatting = True
    .RefreshOnFileOpen = False:             .BackgroundQuery = True
    .RefreshStyle = xlInsertDeleteCells
    .SavePassword = False:                  .SaveData = True
    .AdjustColumnWidth = True
    .RefreshPeriod = 0:                     .PreserveColumnInfo = True
    .ListObject.DisplayName = "Table5"
    .Refresh BackgroundQuery:=False
End With
End Sub
 
Upvote 0
See if this VBA example helps:


Code:
Sub Macro2()
Dim d$
d = Format([p55], "yyyy-mm-dd")                                                     ' from cell P55
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:=Array(Array( _
"ODBC;DSN=MS Access Database;DBQ=C:\Users\Eddie\Desktop\Campaign_Template13.mdb;" & _
"DefaultDir=C:\Users\Eddie\Desktop;DriverId=25;FIL=MS " _
), Array("Access;MaxBufferSize=2048;PageTimeout=5;")), Destination:=Range("$v$61")).QueryTable
.CommandText = Array( _
"SELECT Campaign_Table.ProductID, Campaign_Table.Date_Added" & Chr(13) & "" & Chr(10) & _
"FROM `C:\Users\Eddie\Desktop\Campaign_Template13.mdb`.Campaign_Table Campaign_Table" & Chr(13) & _
"" & Chr(10) & "WHERE (Campaign_Table.Date_Added>{ts '" & d & " 00:00:", "00'})" & Chr(13) & "" & _
Chr(10) & "ORDER BY Campaign_Table.Date_Added")
    .RowNumbers = False
    .FillAdjacentFormulas = False
    .PreserveFormatting = True
    .RefreshOnFileOpen = False:             .BackgroundQuery = True
    .RefreshStyle = xlInsertDeleteCells
    .SavePassword = False:                  .SaveData = True
    .AdjustColumnWidth = True
    .RefreshPeriod = 0:                     .PreserveColumnInfo = True
    .ListObject.DisplayName = "Table5"
    .Refresh BackgroundQuery:=False
End With
End Sub
So I just run my SQL query within a macro? I never thought of that. I guess if that is the case I could create a pop up window prompting for the date and set the variable that way?
 
Upvote 0
Like this:

Code:
Sub Macro2()
Dim d$
d = Application.InputBox("Enter date as yyyy-mm-dd", , Format(Now(), "yyyy-mm-dd"), , , , , 2)
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:=Array(Array( _
"ODBC;DSN=MS Access Database;DBQ=C:\Users\Eddie\Desktop\Campaign_Template13.mdb;" & _
"DefaultDir=C:\Users\Eddie\Desktop;DriverId=25;FIL=MS " _
), Array("Access;MaxBufferSize=2048;PageTimeout=5;")), Destination:=Range("$a$15")).QueryTable
    .CommandText = Array( _
    "SELECT Campaign_Table.ProductID, Campaign_Table.Date_Added" & Chr(13) & "" & Chr(10) & _
    "FROM `C:\Users\Eddie\Desktop\Campaign_Template13.mdb`.Campaign_Table Campaign_Table" & Chr(13) & _
    "" & Chr(10) & "WHERE (Campaign_Table.Date_Added>{ts '" & d & " 00:00:", "00'})" & Chr(13) & "" & _
    Chr(10) & "ORDER BY Campaign_Table.Date_Added")
    .RowNumbers = False:                    .FillAdjacentFormulas = False
    .PreserveFormatting = True
    .RefreshOnFileOpen = False:             .BackgroundQuery = True
    .RefreshStyle = xlInsertDeleteCells
    .SavePassword = False:                  .SaveData = True
    .AdjustColumnWidth = True:              .RefreshPeriod = 0
    .ListObject.DisplayName = "Table7"
    .Refresh BackgroundQuery:=False
End With
End Sub
 
Upvote 0
Like this:

Code:
Sub Macro2()
Dim d$
d = Application.InputBox("Enter date as yyyy-mm-dd", , Format(Now(), "yyyy-mm-dd"), , , , , 2)
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:=Array(Array( _
"ODBC;DSN=MS Access Database;DBQ=C:\Users\Eddie\Desktop\Campaign_Template13.mdb;" & _
"DefaultDir=C:\Users\Eddie\Desktop;DriverId=25;FIL=MS " _
), Array("Access;MaxBufferSize=2048;PageTimeout=5;")), Destination:=Range("$a$15")).QueryTable
    .CommandText = Array( _
    "SELECT Campaign_Table.ProductID, Campaign_Table.Date_Added" & Chr(13) & "" & Chr(10) & _
    "FROM `C:\Users\Eddie\Desktop\Campaign_Template13.mdb`.Campaign_Table Campaign_Table" & Chr(13) & _
    "" & Chr(10) & "WHERE (Campaign_Table.Date_Added>{ts '" & d & " 00:00:", "00'})" & Chr(13) & "" & _
    Chr(10) & "ORDER BY Campaign_Table.Date_Added")
    .RowNumbers = False:                    .FillAdjacentFormulas = False
    .PreserveFormatting = True
    .RefreshOnFileOpen = False:             .BackgroundQuery = True
    .RefreshStyle = xlInsertDeleteCells
    .SavePassword = False:                  .SaveData = True
    .AdjustColumnWidth = True:              .RefreshPeriod = 0
    .ListObject.DisplayName = "Table7"
    .Refresh BackgroundQuery:=False
End With
End Sub
Perfect thank you very much
 
Upvote 0

Forum statistics

Threads
1,214,585
Messages
6,120,394
Members
448,957
Latest member
Hat4Life

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top