Results 1 to 7 of 7
  1. #1
    eluhnabroad is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Nov 2014
    Posts
    9

    Retreiving a value from one query to use in another query

    So here's my issue: I'm trying to run an SQL query to add a record to a table. One of the fields, however, is the autonum key from the record for another table that's just been generated with another query, which is being run on a recordset from yet another query on another table. (I'm trying to work with a system designed by someone else -- don't get me started.)



    In the code below, the commented line is the query I'm trying to run. ItemID needs to be the ID for the record in tblInventory just created by the query above it. I was hoping that since that query was just executed, I could just reference the tblInventory.ID field, but that didn't work.

    My first-born to whoever can tell me a way to just get the ID from the tblInventory record just created, without trying to set up nested recordsets and my brain and the database exploding.

    Code:
        Set db = CurrentDb()
        
        intPONUM = Me.PONUM
      
        strSQLItems = "select * from tblPOItems where ponum = " & intPONUM
        
        Set rsItem = db.OpenRecordset(strSQLItems)
        recCount = rsItem.RecordCount
        
        If rsItem.RecordCount > 0 Then
            Do While Not rsItem.EOF
                If Not IsNull(rsItem!ITEMNUM) Then
                    intItem = rsItem!ITEMNUM
                    intNewQty = rsItem!ReQty
               
                    DoCmd.RunSQL "UPDATE tblInventory SET tblInventory.QTY = tblInventory.QTY + " & intNewQty & " WHERE tblInventory.item = '" & intItem & "'"
    
    '                DoCmd.RunSQL "INSERT INTO tblInventoryByLocation ( ItemID, LocQTY, BinLocID ) SELECT " & tblInventory.ID & " AS Expr1, " & intNewQty & " AS Expr2, " & "71 AS Expr3"
    '
                    rsItem.MoveNext
                Else
                    rsItem.MoveNext
                End If
            Loop

  2. #2
    John_G is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    ItemID needs to be the ID for the record in tblInventory just created by the query above it.
    The query above it doesn't create a record - it updates an existing one identified by tblInventory.item = '" & intItem & "'"

    Is that the same record that contains the ItemID you want?

    The INSERT INTO statement is probably wrong - it desnt have a where clause to identify which records to select.

  3. #3
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Quote Originally Posted by John_G
    The INSERT INTO statement is probably wrong - it desnt have a where clause to identify which records to select.
    I see that the insert statement is commented out, but I would say it is definitely wrong - there is no FROM clause in the SELECT statement.


    What are the fields in table " tblPOItems" (recordset rsItem)?

  4. #4
    eluhnabroad is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Nov 2014
    Posts
    9
    Quote Originally Posted by John_G View Post
    The query above it doesn't create a record - it updates an existing one identified by tblInventory.item = '" & intItem & "'"

    Is that the same record that contains the ItemID you want?
    It's a very complicated setup. The tblInventory record is identified by an autonum field called ID. "Item" is the number tied to yet another table that is a parts list. The practical upshot of the UPDATE query is that it creates a new record in tblInventory, the key of which is the autonum just generated in the ID field. My INSERT INTO query (the one I've commented out in the example) is trying to create a new record in tblInventoryByLocation, which also has an autonum key called ID, but I need to fill the ItemID field with the ID from the tblInventory record created by the UPDATE query.

    (I want to emphasize again, I did NOT write this... system. I was just hired to deal with it.)

    And yes, the INSERT INTO statement doesn't have a WHERE clause; that's just the problem. I don't know how to get the ID value of the tblInventory record created by the UPDATE query.

    Please, let's not get into the tblPOItems mess. I just need to get tblInventory.ID into tblInventoryByLocation.ItemID.

    Let me also add: This database doesn't have any table relationships. Everything is being done through queries and VBA.

    I did NOT write this.

  5. #5
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    UPDATE does not create a record, it edits a record.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  6. #6
    eluhnabroad is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Nov 2014
    Posts
    9
    Okay, then.

  7. #7
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    This has been marked solved - is it?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

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

Similar Threads

  1. Best Practise Query a Query or Query a Table
    By Derrick T. Davidson in forum Access
    Replies: 4
    Last Post: 07-29-2014, 10:18 AM
  2. Retreiving a recordset from a function
    By nigelbloomy in forum Programming
    Replies: 9
    Last Post: 02-22-2014, 08:36 AM
  3. Replies: 7
    Last Post: 07-11-2013, 10:45 AM
  4. Replies: 7
    Last Post: 05-02-2012, 08:04 AM
  5. Entering & retreiving data with Forms
    By turnertj in forum Forms
    Replies: 7
    Last Post: 06-04-2010, 12:22 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