Solved

Displaying QueryDef parameters entered by user

Posted on 2006-11-02
4
287 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

623 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