Results 1 to 7 of 7
  1. #1
    jcright is offline Novice
    Windows 7 64bit Access 2016
    Join Date
    Feb 2022
    Posts
    21

    User input as date, passing to SQL, return is wrong date???

    Hello all,

    I have a database with a table called RawData. I have a query that takes a parameter called "Date of Comm" and the query appends the filtered results into a table called Comms2. The parameter obtained by the query is passed as a formatted date mmmm yyyy to my table. I only need the month and year because this is a monthly report. Anyhow, this query works no problem. The report that works off of Comms2 works as well.

    The report I mentioned has a few subreports each reading from a different table for various information. I'd like to automate how these tables are appended with info from the different queries by putting it all into one vba module. I'm testing this out with one query because if I can get one to work then I'm sure I can copy the same pattern and get them all to work. The queries mainly use the same parameter of Month Year.

    I started by going to my query that appends to Comms2 and I copied the SQL code. I started a new sub which is below. I've broken the SQL into multiple lines so that I can read/follow it easier.

    If I run the code below as is, and enter February 2022 as my date, then it posts to Comms2 as December 1899. I tried a different month/year combination and got the same result.

    Within the SQL string, I tried using Format(commDate, "mmmm yyyy"). When I run it I get the inputbox to fill in, but then I get an error message: Run-time error '3075': Syntax error (missing operator) in query expression 'February 2022'.

    Private Sub TestingQuerySQL()
    Dim commDate As Date
    commDate = InputBox("Enter Date of Commission", "Determine Commission Period", "Month Year")

    Dim strQuery As String

    strQuery = "INSERT INTO Comms2 ( DateofComm, [Last Name], [N/U], Instrument, Manufacturer," & _
    "Model, SerialNumber, [Trade-In], Cost, CreditCardCost, ServiceCost, GoodsCost, Sale, TotalCost," & _
    "GP, GPPercent, SP1, Store, [Store%], SP2, Store2, [Store%2] )" & _
    "SELECT " & commDate & " AS DateofComm, RawData.[Last Name], RawData.[N/U], RawData.Instrument," & _
    "RawData.Manufacturer, RawData.Model, RawData.SerialNumber, RawData.[Trade-In], RawData.Cost," & _
    "RawData.CreditCardCost, RawData.ServiceCost, RawData.GoodsCost, RawData.Sale," & _
    "Nz([Cost],0)+Nz([CreditCardCost],0)+Nz([ServiceCost],0)+Nz([GoodsCost],0) AS TotalCost," & _
    "[Sale]-[TotalCost] AS GP, [GP]/[Sale] AS GPPercent, RawData.SP1, RawData.Store," & _
    "RawData.[Store%], RawData.SP2, RawData.Store2, RawData.[Store%2]" & _
    "FROM RawData " & _
    "GROUP BY " & commDate & ", RawData.[Last Name], RawData.[N/U], RawData.Instrument, RawData.Manufacturer," & _
    "RawData.Model, RawData.SerialNumber, RawData.[Trade-In], RawData.Cost, RawData.CreditCardCost," & _


    "RawData.ServiceCost, RawData.GoodsCost, RawData.Sale, RawData.SP1, RawData.Store, RawData.[Store%], RawData.SP2, RawData.Store2, RawData.[Store%2];"


    DoCmd.RunSQL strQuery
    End Sub

    So the question is how do I get vba to format my input response into MMMM YYYY and append it to my table?
    Thanks,
    John

  2. #2
    ranman256's Avatar
    ranman256 is online now VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,521
    did you try # delimiters for dates?

    ...#" & dateVar & "#...

  3. #3
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,518
    Presuming the target field has a text data type, you need delimiters:

    "SELECT '" & commDate & "' AS DateofComm...
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  4. #4
    jcright is offline Novice
    Windows 7 64bit Access 2016
    Join Date
    Feb 2022
    Posts
    21
    Yep, that worked. Knew there had to be something simple that I was missing. I did see that elsewhere when I was surfing around for answers, but, quite frankly, I didn't understand it. I still don't understand why that works. Those delimiters are within the text string, I would have thought that they would be out of the text string.

    Thank you

  5. #5
    ranman256's Avatar
    ranman256 is online now VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,521
    dates can work with string quotes or # sign. versitile.

  6. #6
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,518
    Quote Originally Posted by jcright View Post
    I still don't understand why that works. Those delimiters are within the text string, I would have thought that they would be out of the text string.
    Only the variable is outside the text string. What you're looking for is:

    SELECT 'Whatever' AS DateofComm...

    The delimiters have to be within the quoted text string to be concatenated properly to achieve the desired result. You'd get an error with them outside the quotes.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  7. #7
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,518
    Quote Originally Posted by ranman256 View Post
    dates can work with string quotes or # sign. versitile.
    I didn't know that, I've always used # for date values.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

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

Similar Threads

  1. Replies: 2
    Last Post: 01-21-2020, 07:51 PM
  2. Input date and return value
    By rayted in forum Queries
    Replies: 2
    Last Post: 12-11-2018, 07:47 AM
  3. Replies: 1
    Last Post: 12-05-2014, 01:06 PM
  4. MS Access Date filter basedon User Input
    By shanmugamgsn in forum Access
    Replies: 1
    Last Post: 12-12-2011, 04:15 PM
  5. Replies: 9
    Last Post: 10-01-2010, 05:50 PM

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