Results 1 to 4 of 4
  1. #1
    inverted is offline Novice
    Windows 10 Access 2016
    Join Date
    Dec 2017
    Posts
    7

    query result into a variable

    Hi,


    I am trying to do the following:
    Code:
    Public Sub calc_how_many_flares_converted()
    
    Dim db As Database
    Dim rs As Recordset
    
    
    Set db = CurrentDb
    Set rs = db.OpenRecordset("SELECT * FROM Hep_converted_new ORDER BY PAT_SERIAL_NO")
    
    
    Dim temp_patient As Double
    Dim temp_date_diff As Double
    
    
    rs.MoveLast
    rs.MoveFirst
    
    
    Do While (Not rs.BOF And Not rs.EOF)
        temp_patient = rs.Fields("PAT_SERIAL_NO")
    
    
        temp_date_diff = ("SELECT HepB_datediff.Expr1 FROM .... HAVING (HepB_datediff.PAT_SERIAL_NO)=temp_patient))")
    Is it possible to assign/convert a query result into a variable?
    I want to use this value. Is there an easier way to do so?

    My second question is: I use temp_patient which is in the scope of VBA, is there a way to use it in the query?

    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,652
    Not like that, no. You could use a DLookup (or DSum), or open a recordset on your SQL. To use the variable you concatenate it into your string:

    "SELECT...WHERE Blah = " & VariableName
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    inverted is offline Novice
    Windows 10 Access 2016
    Join Date
    Dec 2017
    Posts
    7
    For future reference here is the solution:
    Code:
        temp_date_diff = DLookup("[Expr1]", "HepB_datediff", "[PAT_SERIAL_NO] =" & temp_patient)
    Thanks again!

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    Happy to help!
    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. Replies: 1
    Last Post: 09-14-2017, 10:19 AM
  2. Replies: 3
    Last Post: 07-28-2016, 07:39 PM
  3. Replies: 2
    Last Post: 08-11-2015, 01:47 AM
  4. Replies: 5
    Last Post: 04-27-2015, 02:40 PM
  5. Passing SQL result into variable
    By jonny in forum Access
    Replies: 3
    Last Post: 10-18-2009, 07:46 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