Link to home
Start Free TrialLog in
Avatar of pbo1
pbo1Flag for United States of America

asked on

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 ?
Avatar of tygrus2
tygrus2
Flag of Australia image

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
Avatar of pbo1

ASKER

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 :)
Avatar of kavik379
kavik379

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?
Avatar of pbo1

ASKER

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 ?
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]
Avatar of pbo1

ASKER

that was a typo on my part within the post..the query has correct syntax..in terms of !
Are you also running MS Excel 2003?
Avatar of pbo1

ASKER

yes
Avatar of Nick67
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
Avatar of pbo1

ASKER

interesting...if I do a copy and paste the value does not display in excel..however the value is in datasheet view..
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?

Avatar of pbo1

ASKER

Same thing happens on another computer.  I have attached the databsae ee.mdb
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
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
they do paste into excel though
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
Avatar of pbo1

ASKER

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 ?
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

Avatar of pbo1

ASKER

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...
ASKER CERTIFIED SOLUTION
Avatar of Nick67
Nick67
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of pbo1

ASKER

Excellent follow-up and persistence to an efficient solution.  I was just about to give up..