Results 1 to 5 of 5
  1. #1
    merebag is offline Novice
    Windows 10 Access 2013 64bit
    Join Date
    Sep 2016
    Posts
    19

    DATEPART not working in VBA recordset

    I am moving code from my Access ADP (Data Project) to Access 2013. It's been a slow, tedious process. One thing that I am finding when running SQL code in my vba recordsets are Date handling commands.



    I have the following code that gives me a 'Too Few Parameters, Expected 1' error. This error is related to the DATEPART section of the code shown below.

    Dim rsYTD2 As DAO.Recordset
    Dim sqlYTD2 As String
    Dim PriorYear As Integer

    PriorYear = Year(Now) - 1

    sqlYTD2 = "SELECT SUM(pymt_Amount) AS YTDTotal2 FROM tblPaymentMaster WHERE pymt_AcctID = " & Me.acct_ID & " AND " _
    & "DATEPART(yyyy,pymt_Date) = " & PriorYear


    Set rsYTD2 = CurrentDb.OpenRecordset(sqlYTD2, dbOpenDynaset, dbSeeChanges)


    Me.txtYTDPaymentsPriorYear.Value = rsYTD2.Fields("YTDTotal2")
    Me.lblPriorYear.Caption = "for " & PriorYear

    If I take the DATEPART clause out, it works fine. Any Clues? This used to work fine in Access 2010 in my data project.


    Thanks

  2. #2
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 10 Access 2013 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Have you done a Debug.Print or MsgBox on sqlYTD2 to see what you are actually getting?

  3. #3
    aytee111 is offline Competent At Times
    Windows 7 32bit Access 2010 32bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    I think that yyyy must be in quotes, single ones in this case

  4. #4
    merebag is offline Novice
    Windows 10 Access 2013 64bit
    Join Date
    Sep 2016
    Posts
    19
    It is the single quotes around YYYY. Thanks

  5. #5
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 10 Access 2013 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    I marked this thread as *Solved* for you using the *Thread Tools* at the top of the thread.
    https://www.accessforums.net/showthread.php?t=1828

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

Similar Threads

  1. Replies: 7
    Last Post: 09-28-2023, 08:41 AM
  2. How to check recordset working properly or not?
    By rmayur in forum Programming
    Replies: 1
    Last Post: 02-26-2015, 09:47 PM
  3. Recordset FindFirst Not Working
    By ShoresJohn in forum Programming
    Replies: 5
    Last Post: 03-01-2012, 06:59 PM
  4. Replies: 5
    Last Post: 07-18-2011, 06:07 PM
  5. Working with ADO Recordset & Excel
    By jgelpi16 in forum Programming
    Replies: 7
    Last Post: 03-15-2011, 01:58 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