Solved

Displaying QueryDef parameters entered by user

Posted on 2006-11-02
4
274 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

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

In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

809 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