Date value won't work in xl vb code

BradleyS

Active Member
Joined
Oct 28, 2006
Messages
348
Office Version
  1. 2010
Platform
  1. 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'):
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?
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Hi Bradley,

See if this works

....& "WHERE (slotapp.slotdate=" & sDate & ")")

M.
 
Upvote 0
Hi No that doesn't work either.

What I have figured out is the comment symbols are nessasary here, because even in the first example that does work i.e. WHERE (slotapp.slotdate='20131010')
If I remove the comments i.e. WHERE (slotapp.slotdate=20131010) it doesn't work.

I have also tried: WHERE (slotapp.slotdate= Chr(39) & 20131010 & Chr(39)) but this doesn't work either.

The field it is entering the date value in within XL is a combo box, if that makes a difference.

It's so frustrating, because it seems so simple, but nothing seems to work as a replacement and I don't have the skill set to figure out any alternatives.
 
Upvote 0
Maybe this will work?

Code:
Sub Macro1()
'
' Macro1 Macro
'

'
    'Get the date range
    Dim sDate As String
        sDate = "20131010"
    Dim sSQL As String
        sSQL = "SELECT slotapp.slotdate FROM slotapp slotapp WHERE (slotapp.slotdate='" & sDate & "')"
    
    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 = sSQL
        .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
 
Upvote 0
THATS IT!!!!!! :)

OMG

I'm so happy. Thank you Thank you Thank you
 
Upvote 0

Forum statistics

Threads
1,223,277
Messages
6,171,148
Members
452,382
Latest member
RonChand

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