Results 1 to 7 of 7
  1. #1
    Coffee is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Jul 2011
    Location
    Australia
    Posts
    31

    Correct Formatting of SQL in VBA?

    Does anyone know how i would get this to work. Searching between 2 input dates. having trouble sorting out the commas and ## etc. ;/

    strSQL = "SELECT dbo_HistoricalReport.MessageNo, dbo_HistoricalReport.DateTime, dbo_HistoricalReport.UserName, dbo_HistoricalReport.FCS, dbo_HistoricalReport.KKSCode, dbo_HistoricalReport.Message " & _
    "FROM dbo_HistoricalReport " & _


    "WHERE dbo_HistoricalReport.DateTime" >= & strStartDate & <= & strEndDate & _
    ";"

  2. #2
    pbaldy's Avatar
    pbaldy is online now Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Try

    "WHERE dbo_HistoricalReport.DateTime Between #" & strStartDate & "# And #" & strEndDate & "#"
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    Coffee is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Jul 2011
    Location
    Australia
    Posts
    31
    thanks pbaldy. i now have the following code:

    "WHERE dbo_HistoricalReport2.DateTime BETWEEN #" & Format(strStartDate, "dd/mm/yyyy") & "# AND #" & Format(strEndDate, "dd/mm/yyyy") & "#" & _

    this is currently working, but for some reason it will only return results between the 7th and 30th of each month. and wont display any results if you search for the 1st of any month or 2nd, 3rd, 4th, 5th, 6th. i am stumped ;/

  4. #4
    pbaldy's Avatar
    pbaldy is online now Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    VBA assumes dates are in US format, so try formatting them that way (mm/dd/yyyy). All kinds of functions and stuff on this:

    http://access.mvps.org/access/datetime/date0005.htm

    http://allenbrowne.com/ser-36.html
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    Coffee is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Jul 2011
    Location
    Australia
    Posts
    31
    hmm tried all that its the correct format and still wont produce the correct result.

    the date is imported as dd/mm/yyyy
    the date is stored in table as dd/mm/yyyy
    the excel default for date format for date is dd/mm/yyyy

    the sql query 'between dd/mm/yyyy and dd/mm/yyyy' works fine

    that same query put in a sql string in vba and submitted returns incorrect results..

    go figure? o.0

  6. #6
    pbaldy's Avatar
    pbaldy is online now Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    From the first link:

    VBA assumes automatically that any dates being used follow US Settings

    from the second:

    In VBA code...Access expects these literal dates to be in the American format

    In other words, none of what you mentioned matters. When you build a string in VBA, you need to use US format (or another, the specifics of which elude me right now).
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  7. #7
    Coffee is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Jul 2011
    Location
    Australia
    Posts
    31
    yep you were right i had to convert the input into American format and then search the database to produce the right results.

    cheers mate!

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Help with getting correct set of records
    By cvegas in forum Queries
    Replies: 3
    Last Post: 07-29-2011, 08:47 AM
  2. Replies: 9
    Last Post: 06-26-2011, 09:14 PM
  3. Creating the correct tables
    By Dewey1128 in forum Access
    Replies: 1
    Last Post: 12-21-2010, 05:56 AM
  4. Subsets (not sure if that is the correct phrase)
    By LifeIsBeautiful in forum Queries
    Replies: 1
    Last Post: 10-07-2010, 11:16 PM
  5. What is the correct syntax for
    By giladweil in forum Access
    Replies: 1
    Last Post: 07-29-2010, 04:56 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums