Change date to range

BradleyS

Active Member
Joined
Oct 28, 2006
Messages
333
Office Version
  1. 2010
Platform
  1. Windows
I have the following code created in an Excel macro and I would like to change the date range to a rolling date i.e. from today in the last year.

WHERE (slotapp.slotdate>='20001006' And slotapp.slotdate<='20131006')

Therefore 20131006 needs to reference today() and 20001006 needs to be 1 year back from today.

Could somebody help me create this in the code?
Thank you


Code:
    Range("A1").Select
    Application.CommandBars("Task Pane").Visible = True
    With ActiveSheet.QueryTables.Add(Connection:=Array(Array( _
        "ODBC;DSN=Visual FoxPro Tables;UID=;;SourceDB=P:\WizAppt;SourceType=DBF;Exclusive=No;BackgroundFetch=Yes;Collate=Machine;Null=Yes;Deleted=Yes" _
        ), Array(";")), Destination:=Range("A1"))
        .CommandText = Array( _
        "SELECT slotapp.slotdate, slotapp.staff_id, slotapp.type, slotapp.session_id, slotapp.count, slotapp.time, slotapp.pat_id, slotapp.pat_label, slotapp.arrived, slotapp.in, slotapp.out, slotapp.expdur, s" _
        , _
        "lotapp.actdur, slotapp.comment, slotapp.date_made, slotapp.time_made, slotapp.who_made, slotapp.method" & Chr(13) & "" & Chr(10) & "FROM slotapp slotapp" & Chr(13) & "" & Chr(10) & "WHERE (slotapp.slotdate>='20001006' And slotapp.slotdate<='20131006') AND " _
        , "(slotapp.count=$1)" & Chr(13) & "" & Chr(10) & "ORDER BY slotapp.slotdate")
        .Name = "Query from Visual FoxPro Tables"
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .BackgroundQuery = True
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .PreserveColumnInfo = True
        .Refresh BackgroundQuery:=False
    End With
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
OK

I have now decided to use a start and end date from 2 cells on a spreadsheet.
However, it isn't working in the code. If I use a msgbox it shows me the varibles sDate and fDate are correct, so could somebody tell me why it's not working in the code?

WHERE (slotapp.slotdate>='sDate' And slotapp.slotdate<='fdate')


Code:
 'Get the date range from the spreadsheet
    Dim sDate As String
    Sheets("Date_Range").Select
        sDate = Application.WorksheetFunction.Text(Cells(7, 2), "yyyymmdd")
    
    Dim fDate As String
        fDate = Application.WorksheetFunction.Text(Cells(7, 3), "yyyymmdd")
    
    Sheets("Data").Select
    Range("A1").Select
    Application.CommandBars("Task Pane").Visible = True
    With ActiveSheet.QueryTables.Add(Connection:=Array(Array( _
        "ODBC;DSN=Visual FoxPro Tables;UID=;;SourceDB=P:\WizAppt;SourceType=DBF;Exclusive=No;BackgroundFetch=Yes;Collate=Machine;Null=Yes;Deleted=Yes" _
        ), Array(";")), Destination:=Range("A1"))
        .CommandText = Array( _
        "SELECT slotapp.slotdate, slotapp.staff_id, slotapp.type, slotapp.session_id, slotapp.count, slotapp.time, slotapp.pat_id, slotapp.pat_label, slotapp.arrived, slotapp.in, slotapp.out, slotapp.expdur, s" _
        , _
        "lotapp.actdur, slotapp.comment, slotapp.date_made, slotapp.time_made, slotapp.who_made, slotapp.method" & Chr(13) & "" & Chr(10) & "FROM slotapp slotapp" & Chr(13) & "" & Chr(10) & "WHERE (slotapp.slotdate>='sDate' And slotapp.slotdate<='fdate') AND " _
        , "(slotapp.count=$1)" & Chr(13) & "" & Chr(10) & "ORDER BY slotapp.slotdate")
        .Name = "Query from Visual FoxPro Tables"
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .BackgroundQuery = True
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .PreserveColumnInfo = True
        .Refresh BackgroundQuery:=False
    End With
 
Upvote 0

Forum statistics

Threads
1,216,096
Messages
6,128,807
Members
449,468
Latest member
AGreen17

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