Results 1 to 8 of 8
  1. #1
    m3atball is offline Novice
    Windows XP Access 2007
    Join Date
    Mar 2011
    Location
    Los Angeles, CA
    Posts
    9

    Use the result of a recordset without moving through it line by line?


    I have a query based on Table1. I wanted to use the result of this query plus certain constants and append them all to Table2. I know how to do it by moving through the recordset from Table1 line by line to create the INSERT INTO statement for Table2.

    The question is: is it possible to do it without having to go line by line? i.e. Can I using the recordset from Table1 and apply it as a whole to Table2 without using .MoveFirst/.MoveNext, etc.

    Thanks!

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    You can use your query directly:

    INSERT INTO TableName(...)
    SELECT ...
    FROM YourQueryName

    If you mean VBA constants, you can create functions that return their values and use the functions in the above query.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    jgelpi16 is offline Expert
    Windows XP Access 2010 32bit
    Join Date
    Mar 2010
    Location
    Charlotte, NC
    Posts
    544
    pbaldy - I sure would like to know how to reference functions in queries. Can you share your knowledge on this topic?

  4. #4
    m3atball is offline Novice
    Windows XP Access 2007
    Join Date
    Mar 2011
    Location
    Los Angeles, CA
    Posts
    9
    Hi Paul -

    Thanks for the quick reply! I tried that but because not all the fields in TABLE1 goes into TABLE2, I have to use

    INSERT INTO TABLE2() VALUES()

    This version does not allow subqueries inside the VALUES() part. Ideally, this is something I'm hoping to get in one step:

    INSERT INTO SCHEDULE (USER_ID, PROJECT_ID, PHOURS, RHOURS, NOTES)
    VALUES (
    SELECT PREPARER FROM PROJECTS WHERE PREPARER = "USER1",
    SELECT PROJECT_ID FROM PROJECTS, WHERE PREPARER = "USER1",
    NULL, NULL, NULL)

    I'm doing it using the line-move with the recordset for now. Thanks again!

  5. #5
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    You just call it:

    SELECT TableName.FieldName, FunctionName() AS ValueFromFunction
    FROM TableName
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  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,521
    Whether or not all fields from 1 go into 2, you should be able to use a SELECT clause instead of VALUES. You just include the appropriate fields in both clauses. If those subqueries only return a single value, you might get away with a DLookup() in there. Otherwise, perhaps joins? If you could post some sample data and the expected result, it might be easier.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  7. #7
    m3atball is offline Novice
    Windows XP Access 2007
    Join Date
    Mar 2011
    Location
    Los Angeles, CA
    Posts
    9
    Paul -

    I got it!

    This SQL works. Since the other three fields do not require a default value, I'm just skipping them in the append process.

    INSERT INTO SCHEDULE (USER_ID, PROJECT_ID)
    SELECT PREPARER, PROJECT_ID FROM PROJECTS WHERE PREPARER = "USER1"

    Thanks a lot for waking me up!

  8. #8
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Ah good, glad you got it sorted out.
    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. How to do line by line compare of VB code?
    By Buakaw in forum Access
    Replies: 2
    Last Post: 02-14-2011, 11:46 PM
  2. Replies: 5
    Last Post: 12-06-2010, 10:15 AM
  3. Wrap line
    By devcon in forum Reports
    Replies: 1
    Last Post: 10-22-2010, 09:46 AM
  4. numbering line entries
    By tgavin in forum Access
    Replies: 6
    Last Post: 07-27-2010, 11:53 AM
  5. line skip in bookmarks
    By emilylu3 in forum Programming
    Replies: 0
    Last Post: 03-21-2006, 02: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