PC Review


Reply
Thread Tools Rate Thread

Appending parameter from parameter query to exported filename

 
 
LauraB
Guest
Posts: n/a
 
      3rd Nov 2008
I have some code that runs a parameter query and then saves the results as an
xls file (see below). What also I'd like to do is capture the entered
parameter and append it to the filename. Can I programatically access the
user entered parameter?

DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel97, "Names with
DateCreated", "C:\Documents\Database\testfile.xls", True


 
Reply With Quote
 
 
 
 
Alex Dybenko
Guest
Posts: n/a
 
      3rd Nov 2008
Hi,
I think best way is to create a form to enter this parameter, so you get it,
append to filename and then pass it to your query

--
Best regards,
___________
Alex Dybenko (MVP)
http://accessblog.net
http://www.PointLtd.com


"LauraB" <(E-Mail Removed)> wrote in message
news:C12B360B-0728-4207-8032-(E-Mail Removed)...
> I have some code that runs a parameter query and then saves the results as
> an
> xls file (see below). What also I'd like to do is capture the entered
> parameter and append it to the filename. Can I programatically access the
> user entered parameter?
>
> DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel97, "Names with
> DateCreated", "C:\Documents\Database\testfile.xls", True
>
>

 
Reply With Quote
 
Pendragon
Guest
Posts: n/a
 
      3rd Nov 2008
dim strParInfo as string, strFileName as string
strParInfo = InputBox("Enter your parameter information here.")
strFileName = "C:\Documents\Database\testfile" & strParInfo & ".xls"

Then substitute strFileName in your code where you have explicitly listed
the path and file name.

The input box assumes the user is inputting alphanumeric information - if
you think the user might get creative (like adding slashes or other no-no
characters) then you should expand the text prompt of the input box to
instruct the user to type only letters and numbers.

HTH

"LauraB" wrote:

> I have some code that runs a parameter query and then saves the results as an
> xls file (see below). What also I'd like to do is capture the entered
> parameter and append it to the filename. Can I programatically access the
> user entered parameter?
>
> DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel97, "Names with
> DateCreated", "C:\Documents\Database\testfile.xls", True
>
>

 
Reply With Quote
 
LauraB
Guest
Posts: n/a
 
      3rd Nov 2008
Thank you Pendragon and Alex. I suspected that my nice one line of code
would have to turn ten. The input is a date, so yes I'll need to make sure I
strip out slashes and put it in a format that is understandable and filename
friendly.

Quick follow up question... once I have both the input parameter and the
string I want to append, can I still use the TransferSpreadsheet command?
How do I specify for the query variable how to take my input parameter?

"Pendragon" wrote:

> dim strParInfo as string, strFileName as string
> strParInfo = InputBox("Enter your parameter information here.")
> strFileName = "C:\Documents\Database\testfile" & strParInfo & ".xls"
>
> Then substitute strFileName in your code where you have explicitly listed
> the path and file name.
>
> The input box assumes the user is inputting alphanumeric information - if
> you think the user might get creative (like adding slashes or other no-no
> characters) then you should expand the text prompt of the input box to
> instruct the user to type only letters and numbers.
>
> HTH
>
> "LauraB" wrote:
>
> > I have some code that runs a parameter query and then saves the results as an
> > xls file (see below). What also I'd like to do is capture the entered
> > parameter and append it to the filename. Can I programatically access the
> > user entered parameter?
> >
> > DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel97, "Names with
> > DateCreated", "C:\Documents\Database\testfile.xls", True
> >
> >

 
Reply With Quote
 
Pendragon
Guest
Posts: n/a
 
      3rd Nov 2008
I believe you cannot execute a query from code wherein parameters are passed.
What you need to do, then, is take Alex's advice and create a form which
takes input from the user (open text boxes or selections from combo boxes).
You can still use an input box to get the date, but as long as you're
creating a form you might as well have a field on the form for this. Set the
field properties to date.

In code, those user inputs are set to variables. Those variables are
appended to an SQL string which is executed in code. This puts the data into
your table. You can then use TransferSpreadsheet on the table as you have
done.

First, make your query generic without any parameters and then copy the SQL
of the query. This is the string statement for your VBA variable MySQL.

Basic example - you will need to edit with your form, field, query and table
names.

'strPar1 and strPar2 are fields on your form - your parameters for the query
dim strPar1 as string, strPar2 as string, MySQL as string, strUserDate as
string

strPar1 = forms!MyForm.Field1
strPar2 = forms!MyForm.Field2
strUserDate = forms!MyForm.txtDateField

MySQL = "SELECT qryField1, qryField2 {etc etc} INTO TableNameToBeExported
FROM TableName"

Now apply your parameters to the SQL statement.

MySQL = MySQL & " WHERE (qryField4) = " & strPar1 & " AND (qryField5) = " &
strPar2 & ";"

NOTES - a) important that you have a space before the WHERE and after the
quotation mark; b) you will have to add appropriate characters if strPar1
and/or strPar2 are text (meaning adding single quotes).

Now execute the SQL to make your table. If you want to suppress messages,
add the DoCmd.SetWarnings False before and DoCmd.SetWarnings True after the
RunSQL statement.

DoCmd.RunSQL MySQL

To create your path and file name from the date field (user input):

strFileName = "c:\mydocuments\MyFileName" & "-" &
format(Month(strUserDate),"mm") & format(Day(strUserDate),"dd") &
format(Year(strUserDate),"yy") & ".xls"

This should create a path and file name like
"c:\mydocuments\MyFileName-110208.xls".

DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel97, "Names with
DateCreated", strFileName, True

If you run into errors or problems, post your revised code.

Good luck!


"LauraB" wrote:

> Thank you Pendragon and Alex. I suspected that my nice one line of code
> would have to turn ten. The input is a date, so yes I'll need to make sure I
> strip out slashes and put it in a format that is understandable and filename
> friendly.
>
> Quick follow up question... once I have both the input parameter and the
> string I want to append, can I still use the TransferSpreadsheet command?
> How do I specify for the query variable how to take my input parameter?
>
> "Pendragon" wrote:
>
> > dim strParInfo as string, strFileName as string
> > strParInfo = InputBox("Enter your parameter information here.")
> > strFileName = "C:\Documents\Database\testfile" & strParInfo & ".xls"
> >
> > Then substitute strFileName in your code where you have explicitly listed
> > the path and file name.
> >
> > The input box assumes the user is inputting alphanumeric information - if
> > you think the user might get creative (like adding slashes or other no-no
> > characters) then you should expand the text prompt of the input box to
> > instruct the user to type only letters and numbers.
> >
> > HTH
> >
> > "LauraB" wrote:
> >
> > > I have some code that runs a parameter query and then saves the results as an
> > > xls file (see below). What also I'd like to do is capture the entered
> > > parameter and append it to the filename. Can I programatically access the
> > > user entered parameter?
> > >
> > > DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel97, "Names with
> > > DateCreated", "C:\Documents\Database\testfile.xls", True
> > >
> > >

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Parameter Form - Combo Box - Parameter Query - Report Problems SJ Microsoft Access Forms 1 13th Jul 2008 04:02 AM
Pass a parameter to a parameter query from a data access page =?Utf-8?B?TWF0dA==?= Microsoft Access Queries 10 16th Aug 2006 02:52 PM
Pass parameter to a parameter query from a data access page =?Utf-8?B?TWF0dA==?= Microsoft Access 0 11th Aug 2006 06:18 PM
Appending a parameter query output recordset to a table =?Utf-8?B?QmxvY2tOaW5qYQ==?= Microsoft Access VBA Modules 0 17th Nov 2005 03:52 AM
Using a parameter for the filename in an SQL query =?Utf-8?B?QkJN?= Microsoft ADO .NET 3 10th May 2005 12:58 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:00 PM.