Results 1 to 5 of 5
  1. #1
    George is offline Competent Performer
    Windows 7 64bit Access 2013 64bit
    Join Date
    Feb 2012
    Posts
    295

    Finding error in query vba code

    Good Day Everyone.

    I am trying to create a query Using VBA code but it is failing. Strangely, thought, the code used in the VBA module was cut and pasted from an actual query where it worked, but yet it wouldn't work when placed in the VBA code.

    Here is the code:



    Code:
     Public Sub IncomeStatementSummed(ByVal Period As String)Dim db As DAO.Database
        Set db = CurrentDb
        Dim qdf As DAO.QueryDef
        Dim newSQL As String
        
        IncomeStatementDetailed (Period)
        
        On Error Resume Next
        
        DoCmd.DeleteObject acQuery, "IncomeStatementSummed"
        On Error GoTo 0
    
    
        newSQL = "SELECT    DatePart('yyyy',[Journal Date]) AS [years] "
            
        Set qdf = db.CreateQueryDef("IncomeStatementSummed", newSQL)
    
    
    End Sub
    The Error message is: The select statement includes a reserved word or an argument name that is missed spelled or missing, or the punctuation is incorrect.

    Would be grateful for some assistance.

  2. #2
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,793
    Go back to the query that worked and check the sql (sql view). I think you missed some of it - there is no FROM clause. Also, [Journal Date] may be part of the query's fields but once you migrate the sql over to vba, it has no exposure there. It needs to be a form control reference or a variable. Also would advise to step through the code and report which line the error is raised on, just in case it has anyting to do with the function you're calling: IncomeStatementDetailed.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  3. #3
    George is offline Competent Performer
    Windows 7 64bit Access 2013 64bit
    Join Date
    Feb 2012
    Posts
    295
    Thank for your response Micron.

    I did relook my Code and there was an error. So it's working now.

    Thanks

  4. #4
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,793
    Glad you solved it!
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  5. #5
    ssanfu is offline Master of Nothing
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    You should really add two lines:
    Code:
         'Clean up
          Set db = Nothing     '<<-- Add
          Set qdf = Nothing   '<<-- Add
    
    End Sub

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

Similar Threads

  1. Writing code for finding Null value field
    By Raveen1609 in forum Access
    Replies: 5
    Last Post: 12-12-2020, 10:05 PM
  2. Replies: 1
    Last Post: 05-22-2018, 07:47 AM
  3. Error in VBA Code of Exporting Query Data to Excel
    By kirky in forum Import/Export Data
    Replies: 3
    Last Post: 02-09-2017, 08:47 PM
  4. Replies: 9
    Last Post: 03-26-2015, 03:51 PM
  5. Syntax Error in my Query Code
    By PPat in forum Queries
    Replies: 5
    Last Post: 04-19-2013, 03:12 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