Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 281
  • Last Modified:

MS Access query displays form value, but not if you export to MS Excel

I have a form with a field called start date and end date
I have a query where I am pulling in data based on the input values provided by the form.  When I run the query it was my intent to display the input values found within the form.  

Thus in the query I have 2 fields
StartDate:Forms!FormfrmMain[start date]
EndDate:Forms!FormfrmMain[end date]

When I run the query in design view the start date and end date appear.  If I export the query to Excel the start date and end date do not appear.  What am I doing wrong ?
0
pbo1
Asked:
pbo1
  • 9
  • 8
  • 3
  • +1
1 Solution
 
tygrus2Commented:
You can put parts of the query SQL code in VBA code and have it delete&add the query each time.

strSQL = "SELECT Var1, Var2, Var3, #" & Forms!FormfrmMain[start date] "# as StartDate, #" & Forms!FormfrmMain[end date] "# as EndDate FROM table;"
db.QueryDefs.Delete "temp"
With db.CreateQueryDef("temp", strSql)
    .Execute
End With
0
 
pbo1Author Commented:
I don't understand the connection.  Are you indicating that I need to use VBA and delete the query in order for the values to display when the record-set is exported to Excel ?

Why do I see the values when I run the query in design, but not when I export the query to excel..thx :)
0
 
kavik379Commented:
I don't exactly understand your question. Where do the start date and end date appear when you run the query? Do you mean that it is used as parameters, but when you export to excel it just exports the whole query as if there were no parameters?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
pbo1Author Commented:
No the parameters work.  The query retrieves all data based on the parameters.  However I wanted to add 2 columns that display the parameters.  Thus if I ask for all orders between Jan 1 and Jan 30 then if the returned record.set records will display Jan 1 in the first column and Jan 30 in the second column.  The remaining values are based on a table.  When I run the query in design view Jan 1 shows in the first column and Jan 30 in the second column and all the table driven values based on the paramaters.  Thus when I run in design view everything is perfect.  

If I try to export to excel then the 1 st and 2 nd column are blank, but other columns have values.  Thus I am perplexed why the 2 parameter values do not display when doing export to excel.  Does this help ?
0
 
kavik379Commented:
Just to check minor things first, it looks like you are missing "!" after the form names.

Change these:

StartDate:Forms!FormfrmMain[start date]
EndDate:Forms!FormfrmMain[end date]

to

StartDate:Forms!FormfrmMain![start date]
EndDate:Forms!FormfrmMain![end date]
0
 
pbo1Author Commented:
that was a typo on my part within the post..the query has correct syntax..in terms of !
0
 
kavik379Commented:
Are you also running MS Excel 2003?
0
 
pbo1Author Commented:
yes
0
 
Nick67Commented:
If you run the query in the datasheet view, do you get the right results?

If the query is returning the right results, do a manual copy and paste into Excel.
What you see in Access should be what you get in Excel

If a manual copy and paste gives the right results, then post what code you are using to export the query to Excel
0
 
pbo1Author Commented:
interesting...if I do a copy and paste the value does not display in excel..however the value is in datasheet view..
0
 
Nick67Commented:
Then you've got something very weird on to go, because my Access2003/Excel2003 is WYSIWYG.

Now, in your db build a new, simple query just pulling dates and then the 'parameter' columns.
Does it copy and paste?

Try on another machine
Does it copy and paste?

Create a new mdb with just a little bit of data, and the same mechanics.
Does it copy and paste?

0
 
pbo1Author Commented:
Same thing happens on another computer.  I have attached the databsae ee.mdb
0
 
Nick67Commented:
Interesting.  It has to do with displaying dates.
Take your sample db and change the textboxes so they will accept text and fill in 'frig"
That displays in the query.
Formatted as date nothing shows
0
 
Nick67Commented:
Your sample wouldn't display dates for me in the query.
I am now outputting the working query to txt, and the parameters aren't going
ee.mdb
0
 
Nick67Commented:
they do paste into excel though
0
 
Nick67Commented:
This one exports properly to Excel as well as pasting properly.
The txt export doesn't carry the parameters though.

Try on your production one to remove the format property of "Short date" from the parameter textboxes and let me know  
ee.mdb
0
 
pbo1Author Commented:
I need to be able to use a date format...if I take the format off people will be inputting anything into the field.  There must be a reason and solution for this odd behavior ?
0
 
Nick67Commented:
I don't know offhand, and I haven't been able to Google a reason.  My own production db's don't exhibit this behavior, but if I create a new db I encounter the same thing.

There are other ways to do data validation.  As part of the button click event, modifiy the following code, and duplicate it for both controls.  It will allow the data to be only a date, gives you control over the message the end user sees, and gets away from the nastiness that is input masks
'in the _Click event for the button
dim ctrl as control
set ctrl = me.start-date
if ValidateTheInput(ctrl) = false then exit sub
set ctrl = me.end-date
if ValidateTheInput(ctrl) = false then exit sub



'in the form's module 

Private Function ValidateTheInput(ctrl as control) as boolean
Dim SubmissionDate As String


BadDate:
SubmissionDate =  Format(ctrl.value, "dd-MMM-yy"))

'bail if it is ""
If SubmissionDate = "" Then Exit Sub

'validate the user input as a date
If IsDate(SubmissionDate) = False Then
    MsgBox "Your input was not seen as a vaild date.  Try again."
    ctrl.value = ""
    ctrl.setfocus
    GoTo BadDate 'throw them back if it is no good and not ""
End If

end sub

Open in new window

0
 
pbo1Author Commented:
I think I may try an intermediate step where I make a table and then export to excel..I will run and see if the date value gets passed to the table...
0
 
Nick67Commented:
I've got it, I think.
Check out
http://www.accessmonster.com/Uwe/Forum.aspx/access-queries/27020/Date-Format-problem-in-parameter-query
Your sample db, when this is done to the query, quits returning wingdings.
I had tried this earlier, but I was use sd and ed in the parameters and not the [forms]![form1]![mycontrols] parameters
0
 
pbo1Author Commented:
Excellent follow-up and persistence to an efficient solution.  I was just about to give up..
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 9
  • 8
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now