Results 1 to 9 of 9
  1. #1
    lookingforK is offline Advanced Beginner
    Windows 7 32bit Access 2007
    Join Date
    Oct 2012
    Posts
    31

    How to set Parameter values for a saved query via VBA?

    Hi,

    I am using MSAccess 2010 to export data into MS Excel 2010 spreadsheets.

    I am justwondering:
    Is there a wayto set the Parameter value for the query via VBA?

    For example:
    There are 10regional managers. When I click a button on a form, 10 sets of data per managerare going to be exported into Excel spreadsheets.

    I have createda saved query named [For exporting] with a parameter [Manager Name] for the field[Master Table].[Manager].

    The VBA for thebutton has 2 subs:
    1) Sub 1 forthe loop for 10 managers

    Code:
    ...
    strQuery = "SELECT DISTINCT [Master Table].[Manager] FROM [Master Table] WHERE ((([Master Table].[Manager]) Is Not Null));"
    Set rstStores = CurrentDb.OpenRecordset(strQuery)
    rstStores.MoveFirst
    Do Until rstStores.EOF
    {assign the value rstStores![Manager] to the Parameter of the saved query}
     
    GenerateReport strReportPath, Trim(rstStores![Manager])
    rstStores.MoveNext
    Loop
    End If
    ...
    2) Sub 2 (i.e. GenerateReport) for exportingdataset into Excel spreadsheet one by one for each manager.
    Here, I use thecommand 'TransferSpreadsheet'

    Code:
    ...
    DoCmd.TransferSpreadsheet acExport, , "For exporting", ReportPath & "\Temp1.xlsx"
    ...

    How to writethe code in the
    red part?



    Thank you in advance.

  2. #2
    Join Date
    May 2013
    Location
    Dallas TX
    Posts
    1,742
    I started to say "Use a temp variable", but I realized I don't understand the problem. Your code already is getting the value you want and passing it to the GenerateReport subroutine.

    It CAN be as simple as this: [TempVars]![Manager] = rstStores![Manager]
    and then using [TempVars]![Manager] in the saved query.

    You can review this site for some more about how to use tempvars
    http://blogs.office.com/b/microsoft-...-and-2010.aspx

  3. #3
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,626
    Options:

    1. query parameter references a textbox on form - use code looping through the rstStores recordset to populate the textbox
    Me.textboxname = rstStores!Manager

    2. modify the query definition through QueryDef collection

    Re Dal's suggestion, I've never used TempVars and don't know if query can reference one as a parameter.
    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.

  4. #4
    lookingforK is offline Advanced Beginner
    Windows 7 32bit Access 2007
    Join Date
    Oct 2012
    Posts
    31
    Thank you Dal Jeanis.

    I tried your method. It is simple and good.

    Thank you June7.

    I do use a Combo Box actually. I would like to check through QueryDef collection.

  5. #5
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,626
    Issue solved? I have marked thread solved.
    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
    Perceptus's Avatar
    Perceptus is offline Expert
    Windows 7 64bit Access 2003
    Join Date
    Nov 2012
    Location
    Knoxville, Tennessee
    Posts
    659
    I use the below code in part with running multiple update queries with parameters in code. Hope this helps. gdb = currentdb.
    Code:
    Public Function ExecuteParamterQuery(ByVal strQueryName As String, _
                                         ParamArray pArray() As Variant) As Recordset
        ' Written by alex miglin/perceptus 5/28/2012
        Dim Myqdf                   As DAO.QueryDef ' Query Definition
        Dim intCounter              As Integer      ' Used For loop
    On Error GoTo Err_ExecuteParamterQuery
        Set Myqdf = gdb.QueryDefs(strQueryName)
        If UBound(pArray) > LBound(pArray) Then
            For intCounter = LBound(pArray) To UBound(pArray) Step 2
                Myqdf.Parameters(pArray(intCounter)) = pArray(intCounter + 1)
            Next intCounter
        End If
        Set ExecuteParamterQuery = Myqdf.Execute
    Safe_ExecuteParamterQuery:
        Myqdf = Nothing
        Exit Function
    Err_ExecuteParamterQuery:
        WarningBox "This query did not work." & vbCrLf & vbCrLf & Myqdf.SQL
    End Function

  7. #7
    Join Date
    May 2013
    Location
    Dallas TX
    Posts
    1,742
    lookingforK - If you could post your code that worked, that might be helpful for the next person that googles this problem.

    June7 - I'm not very confident with temp variables yet, but I know that they're basically stored as any other table, so Jet has to be able to handle them, given the right syntax. Of course, I could be wrong, and they could be pseudo-controls on some other kind of object. It looks like lookingforK got them to work, though, so I must be close to right.

    perceptus - cool little widget there. I've actually never thought of using "& vbCrLf &" to make my messages more readable. Nice.

    I love things that are intuitively obvious in retrospect.

  8. #8
    lookingforK is offline Advanced Beginner
    Windows 7 32bit Access 2007
    Join Date
    Oct 2012
    Posts
    31
    The code is simple:
    ...
    Dim tManager As String
    ...
    ...
    ' Write the following for the red part
    tManager = rstStores![Manager]
    [TempVars]![tempManager] = tManager
    ...
    ...
    ...

    Remember to set up the parameter [TempVars]![tempManager] for the saved query.

  9. #9
    Join Date
    May 2013
    Location
    Dallas TX
    Posts
    1,742
    Good enough! Thanks for posting it up here.

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

Similar Threads

  1. Replies: 13
    Last Post: 05-14-2013, 06:01 PM
  2. How to run saved query in background
    By shabar in forum Programming
    Replies: 3
    Last Post: 03-03-2013, 11:49 PM
  3. Replies: 3
    Last Post: 08-16-2012, 03:02 PM
  4. Parameter Values
    By Darkladymelz in forum Queries
    Replies: 12
    Last Post: 01-19-2012, 08:45 AM
  5. Saved Import in Query
    By Kimbertha in forum Queries
    Replies: 1
    Last Post: 10-21-2010, 07:21 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