sql syntax in visual basic

Posted on 2006-06-01
Last Modified: 2011-09-20
i am developing a custom calendar/daytimer type thing, that stores appointments in an access database, so i got it to access the database and display the data ok, but the trouble is i am trying to get it to filter the query so that it only displays appointments that are going to happen within the next week so that the list isnt 3 miles long, and if the date has passed it wont be displayed to, once again conserve space.

so, here's what i have so far, this displays all the data in the database:

queryString = "SELECT * FROM toDoData"

this is what i thought it would look like with the filter:

Dim futureDate, todaysDate As Date
todaysDate = Today()
futureDate = DateAdd(DateInterval.Day, 7, Today())
queryString = "SELECT * FROM toDoData WHERE toDoDate BETWEEN " & todaysDate & " AND "  & futureDate

the queryString should end up being something like:
SELECT * FROM toDoData WHERE toDoDate BETWEEN 06/05/31 AND 06/06/06

even when i try to hard code the dates without the variables, it does not seem to work out as planned, so i am thinking it has something to do with my syntax. any ideas?
Question by:Servo
    LVL 34

    Accepted Solution


    queryString = "SELECT * FROM toDoData WHERE toDoDate BETWEEN #" & todaysDate & "# AND #" & futureDate & "#"

    For MS Access, dates have to be enclosed in '#', and that applies equally when VB.NET passes a query string to Access.


    LVL 12

    Expert Comment

    queryString = "SELECT * FROM toDoData WHERE toDoDate BETWEEN #" & todaysDate & "# AND #"  & futureDate & "#"
    LVL 12

    Expert Comment

    ah, seconds too late ... :-)
    LVL 69

    Expert Comment

    by:Éric Moreau
    Hi Servo,

    You should always format your date not to have problems with localisation:

    queryString = "SELECT * FROM toDoData WHERE toDoDate BETWEEN #" & todaysDate.tostring("yyyy/MM/dd") & "# AND #"  & futureDate.tostring("yyyy/MM/dd") & "#"

    LVL 1

    Author Comment

    fabulous! man, i barely had time to sneeze, and there in the email was my answer... thanks for your quick help! better luck next time vb_jonas :)

    yea, i dont even think seconds...

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Join & Write a Comment

    Article by: Kraeven
    Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
    Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now