Results 1 to 4 of 4
  1. #1
    antioch is offline Novice
    Windows 8 Access 2010 64bit
    Join Date
    Jul 2017
    Posts
    8

    Post Lookup a query field with the latest date

    Hi,



    I'm struggling with the bold statements (Access VBA) below. I'm trying to store the most recent query data from the field name [HM Test] based on the latest date. For Step 2, I want to write the result "testSQL" in a table named [FundList] and in the field named [ID]. Does anyone know how I can fix the bold statement and add the Step 2? I would appreciate any help on this...thanks in advance!

    Do Until rs.EOF

    i = 1
    DoCmd.OpenTable ("FundList")
    strSQL = DLookup("[QueryID]", "FundList", "[num] = 1")

    DoCmd.OpenQuery (strSQL)
    testSQL = DLookup("[HM Test]", "strSQL", "[date] = latest date")

    [STEP 2 GOES HERE]


    i = i + 1

    rs.MoveNext
    Loop

  2. #2
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    date is a reserved word

    and you should be getting a very clear error message when the code fails to run.

    where does 'latest date' come from?

  3. #3
    antioch is offline Novice
    Windows 8 Access 2010 64bit
    Join Date
    Jul 2017
    Posts
    8
    latest date should just be the row with the latest date. Since my data is quarterly, latest date should be the row with 6/30/17. i tried Dmax, but that didn't work either. Thanks.

  4. #4
    ssanfu is offline Master of Nothing
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    I kinda understand what you are trying to do, but there are lots of errors in this code snippet...
    Code:
        Do Until rs.EOF
            i = 1          '<<<--- i will ALWAYS = 1 because the variable i is inside the Do Loop. If rs loops through 1000 records, i will still be 1.
    
            DoCmd.OpenTable ("FundList")   '<<<-- If rs loops through 1000 records, table "FundList" will be opened 1000 times. (never closed??)
            
            strSQL = DLookup("[QueryID]", "FundList", "[num] = 1")  '<<-- strSQL will always be the same value, because 
                                                                          the DLookup criteria will always find the record where num = 1. 
    
            DoCmd.OpenQuery (strSQL)   '<<-- the variable strSQL is a value. Openquery requires an SQL string, not a value.  
                                             And if rs loops through 1000 records, the Openquery command would be executed 1000 times
            
            testSQL = DLookup("[HM Test]", "strSQL", "[date] = latest date")  '<<-- as stated by Ajax, "Date" is a reserved word and shouldn't be used as an
                                                                                    object name. Somehow you need to get the value for latest date". 
                                                                                    And the syntax would be wrong: if there is a variable for "LatestDate",
                                                                                    syntax would be DLookup("[HM Test]", "strSQL", "[date] = #" & LatestDate & "#")
    
            [STEP 2 GOES HERE]
    
            i = i + 1
    
            rs.MoveNext
        Loop

    STEP 2 depends on how you add a new record to a table. If there is a record set opened, would use "rst.AddNew".
    Or could use an append query.

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

Similar Threads

  1. Replies: 8
    Last Post: 01-10-2016, 11:51 AM
  2. Latest Date/Project Query
    By justtrentjohnson in forum Queries
    Replies: 6
    Last Post: 08-11-2014, 11:50 AM
  3. Query to Extract records with latest date
    By rkalapura in forum Queries
    Replies: 2
    Last Post: 03-13-2013, 05:48 AM
  4. Query for latest date? what am I doing wrong?
    By Overdive in forum Queries
    Replies: 1
    Last Post: 12-18-2009, 06:04 AM
  5. Query to find latest date
    By Lockrin in forum Access
    Replies: 2
    Last Post: 12-16-2009, 10:00 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