Results 1 to 6 of 6

Storing SQL Select statement results in existing table field

  1. #1
    klnlsu is offline Advanced Beginner
    Windows 7 64bit Access 2013
    Join Date
    Apr 2015
    Posts
    52

    Storing SQL Select statement results in existing table field

    I am using the following code to return the Mode of a data selection:

    Dim StrSQL As String
    StrSQL = "SELECT TOP 1 tblOEOrderDetail.discount_pct FROM tblOEOrderDetail " & _
    "GROUP BY tblOEOrderDetail.OrderHeaderID, tblOEOrderDetail.discount_pct " & _
    "HAVING (((tblOEOrderDetail.OrderHeaderID) = " & Forms!frmOrderHeaderEntry!OrderHeaderIDTxt & ")) " & _
    "ORDER BY Count(tblOEOrderDetail.discount_pct) DESC"
    DoCmd.RunSQL StrSQL

    I want to store the result in the field of a record I am adding to an existing table. How do I do this?

    I simply tried:

    [fieldname]= DoCmd.RunSQL StrSQL

    but I get a syntax error.

  2. #2
    aytee111 is offline Competent At Times
    Windows 7 32bit Access 2013 32bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    1,583
    Use DLOOKUP instead of a query/SQL

  3. #3
    klnlsu is offline Advanced Beginner
    Windows 7 64bit Access 2013
    Join Date
    Apr 2015
    Posts
    52
    Quote Originally Posted by aytee111 View Post
    Use DLOOKUP instead of a query/SQL
    Is it possible to emulate an aggregate query with DLOOKUP or DCOUNT?

  4. #4
    aytee111 is offline Competent At Times
    Windows 7 32bit Access 2013 32bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    1,583
    What value are you trying to get? Dlookup says "I am looking for the value of this field, from this or query, where these conditions exist", a sort of mini sql statement. From the above it looks as though you are picking any random discount pct as long as the order id matches, is that what you want?

  5. #5
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    10,510
    Please tell readers in plain, simple English what you are trying to do.
    How about an example of
    the Mode of a data selection
    :

  6. #6
    klnlsu is offline Advanced Beginner
    Windows 7 64bit Access 2013
    Join Date
    Apr 2015
    Posts
    52
    Quote Originally Posted by aytee111 View Post
    What value are you trying to get? Dlookup says "I am looking for the value of this field, from this or query, where these conditions exist", a sort of mini sql statement. From the above it looks as though you are picking any random discount pct as long as the order id matches, is that what you want?
    OK. The light just went on. I can do a DLookup of the aggregate query. Duh! Thanks for pointing me in the right direction.

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

Similar Threads

  1. Replies: 2
    Last Post: 10-20-2015, 07:37 AM
  2. Replies: 29
    Last Post: 04-25-2014, 02:49 PM
  3. Replies: 3
    Last Post: 05-06-2013, 02:03 PM
  4. Replies: 11
    Last Post: 12-14-2010, 12:25 PM
  5. Replies: 4
    Last Post: 10-12-2010, 01:11 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
  •  
Tech Forums: Microsoft Office Forums