Results 1 to 4 of 4
  1. #1
    davidvorob is offline Novice
    Windows 7 64bit Access 2013 32bit
    Join Date
    Mar 2021
    Posts
    1

    Passing Parameters into a subquery

    Hello,



    I have a saved query that aggregates a field based on filtering out dates (where condition) and grouping by ID. The dates are entered by the user as parameter variables (UserStarDate and UserEndDate). I also need to code up a query that inserts the output of this query into my Target table, and occasionally needing to do it by ID that will be set in the VBA code. Is there a way to pass in the parameters into a query that looks something like "INESRT INTO targetTable FROM myQuery WHERE UserStartDate = #variableInVBA# AND UserEndDate = #variableInVBA#". I am able to usethe docmd.SetParameter if i'm doing a simple call of an already saved query, but not sure how to do it for a sub-query (the Insert query calls the original SELECT query). I don't necessarily want to save out the INSERT query as a standalone query as it will 1) dirty up the list of queries i have (already have too many) and 2) provide ability for someone to alter data by double clicking on it (would like to control it in a better way). Any help would be appreciated.

    Thanks

  2. #2
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,123
    Have you lookup into using tempvars to store the two dates? Another way would be to use a couple of global variables for the two dates along with a couple of public functions in a standard module to retrieve them in your queries:
    Code:
    Option Database
    Option Explicit
    
    Public datUserStartDate as Date
    Public datUserEndDate as Date
    
    Public Function fnUserStartDate() as Date
        fnUserStartDate=datUserStartDate 
    End Function
    
    Public Function fnUserEndDate() as Date
       fnUserEndDate=datUserEndDate 
    End Function
    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  3. #3
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    5,020
    You should not be exposing queries to users?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  4. #4
    kd2017 is offline Well, I tried at least.
    Windows 10 Access 2016
    Join Date
    Jul 2017
    Posts
    1,142
    Quote Originally Posted by davidvorob View Post
    I don't necessarily want to save out the INSERT query as a standalone query as it will 1) dirty up the list of queries i have (already have too many) and 2) provide ability for someone to alter data by double clicking on it (would like to control it in a better way).
    Just hard code all of it?

    Code:
    Public Sub example()
    On Error GoTo ErrHandler
        Dim db As DAO.Database
        Dim sql As String
        
        Dim startDate As Date
        Dim endDate As Date
        
        'search criterea
        startDate = #1/1/2021#
        endDate = #12/31/2021#
        
        'build the sql code
        sql = ""
        sql = sql & "INSERT INTO targetTable " & vbCrLf
        sql = sql & "SELECT field1, " & vbCrLf
        sql = sql & "       field2, " & vbCrLf
        sql = sql & "       field3 " & vbCrLf
        sql = sql & "FROM   sourceTable " & vbCrLf
        sql = sql & "WHERE  dateField >= #" & startDate & "#" & vbCrLf
        sql = sql & "       AND datefield <= #" & endDate & "#;"
        
        'print the sql string in the immediate window (Ctrl+G) make sure it looks correct
        Debug.Print sql
        
        'execute the sql code
        Set db = CurrentDb
        db.Execute sql, dbFailOnError
        
    ExitHandler:
        'clean up objects
        Set db = Nothing
        Exit Sub
        
    ErrHandler:
        MsgBox Err.Description, , "Error #" & Err.Number
        Resume ExitHandler
    End Sub

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

Similar Threads

  1. Passing a value to subquery?
    By udigold1 in forum Queries
    Replies: 2
    Last Post: 06-16-2019, 11:44 PM
  2. Passing parameters
    By reburton in forum Programming
    Replies: 3
    Last Post: 10-31-2013, 01:35 PM
  3. passing parameters to a query
    By PatrickCairns in forum Programming
    Replies: 1
    Last Post: 12-11-2012, 08:47 PM
  4. Passing parameters to a query
    By stephenaa5 in forum Queries
    Replies: 3
    Last Post: 09-10-2010, 05:32 PM
  5. Passing parameters to a report
    By elmousa68 in forum Access
    Replies: 0
    Last Post: 12-07-2006, 01:38 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