Solved

Displaying QueryDef parameters entered by user

Posted on 2006-11-02
4
270 Views
Last Modified: 2008-02-01
I have a query that I export to a text file. The query has two parameters that ask the user to input a start date and an end date range. I would like to concatenate those two dates into the name of the exported file. The query is called from several forms in my application, so I can't just change the query to read these date parameters off of one form; everybody's happy with being prompted for the dates.

My code basically looks like this, which should give you the logic of what I'm trying to do:
************************************
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim rst As DAO.Recordset
Dim strFileLocation

Set db = CurrentDb
Set qdf = db.QueryDefs("MyQuery")

'(this is the part I can't figure out)
strFileLocation="C:\MyFileAsOf" & qdf.Parameters("[Start Date]") & "to" & qdf.Parameters("[End Date]")

DoCmd.TransferText acExportDelim, , "MyQuery", strFileLocation
*************************************

Basically, the issue is: how do I access the parameters that someone has passed to my query _after_ they've passed them?

Many thanks,
-Ed
0
Comment
Question by:eheraux
  • 2
4 Comments
 
LVL 39

Expert Comment

by:stevbe
ID: 17859895
qdf.Parameters("Start Date").Value
0
 
LVL 19

Expert Comment

by:Eric Sherman
ID: 17860042
stevbe & eheraux ...

Wouldn't you have to run the query first to populate the Start/End dates before you build the strFileLocation string???

You probably want to store the Start/End dates in variables from an Input Box then you can build the strFileLocation as well as modify the qdf to include the date parameters.

ET
0
 
LVL 44

Expert Comment

by:Leigh Purvis
ID: 17860739
FWIW I agree.

The querydef you create in code and the query called by name that you export are two different things.
You could use a function or two to provide the parameters (from global variables or table values).
Then they can be called at will - and the query can be exported again without prompt.
0
 
LVL 39

Accepted Solution

by:
stevbe earned 125 total points
ID: 17861640
and the long way around, without using temp tables ...

Public Function ExportQDF()

Dim dbs As DAO.Database
Dim qdf As DAO.QueryDef
Dim prm As DAO.Parameter
Dim rst As DAO.Recordset
Dim fld As DAO.Field
Dim strPath As String
Dim intFile As Integer
Dim strData As String

Set dbs = CurrentDb
Set qdf = dbs.QueryDefs("qselQDFParams")

Set prm = qdf.Parameters("Start Date")
prm.Value = InputBox("Start Date")
strPath = "C:\MyFileAsOf_" & Format(prm.Value, "yyyymmdd")

Set prm = qdf.Parameters("End Date")
prm.Value = InputBox("End Date")
strPath = strPath & "_to_" & Format(prm.Value, "yyyymmdd") & ".txt"

Set rst = qdf.OpenRecordset

intFile = VBA.FreeFile
Open strPath For Append Access Write As #intFile
For Each fld In rst.Fields
    strData = strData & fld.Name & ""","""
Next
strData = Left$(strData, Len(strData) - 1)
Print #intFile, strData

Do While Not rst.EOF
    strData = vbNullString
    For Each fld In rst.Fields
        strData = strData & fld.Value & vbNullString & ""","""
    Next
    Print #intFile, strData
    rst.MoveNext
Loop

Close #intFile

'cleanup
Set fld = Nothing
rst.Close
Set rst = Nothing
Set prm = Nothing
Set qdf = Nothing
Set dbs = Nothing

End Function
0

Featured Post

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In the previous article, Using a Critera Form to Filter Records (http://www.experts-exchange.com/A_6069.html), the form was basically a data container storing user input, which queries and other database objects could read. The form had to remain op…
Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question