BradleyS
Active Member
- Joined
- Oct 28, 2006
- Messages
- 348
- Office Version
- 2010
- Platform
- Windows
I have been struggling with this for over a week now and just can't understand why I can't get the date section of code to work.
I'm using a data source and trying to enter a date range
This code works fine as it uses a predefined date range i.e. WHERE (slotapp.slotdate='20131010'):
I would like to use a cell in xl that I can enter a date, so to try and get this code to work I am first using a variable instead of a cell, but it doesn't recognise the date in the variable
All I have done is added the variable: Dim sDate As String sDate = 20131010
..And entered it in the code: WHERE (slotapp.slotdate='sDate')
Does anybody know what I am doing wrong or why it won't work and what it is I need to do to get it to work?
I'm using a data source and trying to enter a date range
This code works fine as it uses a predefined date range i.e. WHERE (slotapp.slotdate='20131010'):
Code:
Sub Macro1()
'
' Macro1 Macro
'
'
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:=Array(Array( _
"ODBC;DSN=Visual FoxPro Database;UID=;;SourceDB=p:\;SourceType=DBF;Exclusive=No;BackgroundFetch=Yes;Collate=Machine;Null=Yes;Deleted=Y" _
), Array("es;")), Destination:=Range("$A$1")).QueryTable
.CommandText = Array( _
"SELECT slotapp.slotdate" & Chr(13) & "" & Chr(10) & "FROM slotapp slotapp" & Chr(13) & "" & Chr(10) & "WHERE (slotapp.slotdate='20131010')" _
)
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.ListObject.DisplayName = "Table_Query_from_Visual_FoxPro_Database"
.Refresh BackgroundQuery:=False
End With
End Sub
I would like to use a cell in xl that I can enter a date, so to try and get this code to work I am first using a variable instead of a cell, but it doesn't recognise the date in the variable
All I have done is added the variable: Dim sDate As String sDate = 20131010
..And entered it in the code: WHERE (slotapp.slotdate='sDate')
Code:
Sub Macro1()
'
' Macro1 Macro
'
'
'Get the date range
Dim sDate As String
sDate = 20131010
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:=Array(Array( _
"ODBC;DSN=Visual FoxPro Database;UID=;;SourceDB=p:\;SourceType=DBF;Exclusive=No;BackgroundFetch=Yes;Collate=Machine;Null=Yes;Deleted=Y" _
), Array("es;")), Destination:=Range("$A$1")).QueryTable
.CommandText = Array( _
"SELECT slotapp.slotdate" & Chr(13) & "" & Chr(10) & "FROM slotapp slotapp" & Chr(13) & "" & Chr(10) & "WHERE (slotapp.slotdate='sDate')" _
)
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.ListObject.DisplayName = "Table_Query_from_Visual_FoxPro_Database"
.Refresh BackgroundQuery:=False
End With
End Sub
Does anybody know what I am doing wrong or why it won't work and what it is I need to do to get it to work?