They're exactly the same.
This is the query in the Access SQL view (I copy/pasted from the VBA form to
the SQL view):
SELECT Assets.AssetNo, T.TransType, Assets.User1, Assets.User2,
Assets.[Serial#] FROM Assets LEFT JOIN (SELECT * FROM History WHERE
TranDate >= #1/1/2003# AND TranDate <= #2/1/2003# ) AS T ON Assets.AssetNo
= T.AssetNo WHERE Assets.Purch_Date >= #1/1/2003# And Assets.Purch_Date<=
#2/1/2003# AND T.AssetNo Is Null
_________________________________
This is the code in VBA with the *exact* sql query :
Dim strDocName As String, strSQL As String
Dim datBefore As Date, datAfter As Date
datBefore = Me.BeforeDate.Value
datAfter = Me.AfterDate.Value
strDocName = "AssetsNotInventoriedReport_2"
strSQL = "SELECT Assets.AssetNo, T.TransType, Assets.User1, Assets.User2,
Assets.[Serial#] FROM Assets LEFT JOIN (SELECT * FROM History WHERE
TranDate >= #1/1/2003# AND TranDate <= #2/1/2003# ) AS T ON Assets.AssetNo
= T.AssetNo WHERE Assets.Purch_Date >= #1/1/2003# And Assets.Purch_Date<=
#2/1/2003# AND T.AssetNo Is Null"
DoCmd.OpenReport "AssetsNotInventoriedReport_2", acViewPreview, , strSQL
In the SQL view, it'll bring me the correct recordset. When running through
VBA I'll get the error 3306: "You have written a subquery that can return
more than one field without using the EXISTS reserved word in the main
query's FROM clause. Revise the SELECT statement of the subquery to request
only one field."
Note: While typing the error I did notice that the error tells me that my
subquery should only request only *one* field. But that would mean that the
engine that runs my VBA query is different than the Jet engine that runs my
Access query.
Vaughn
Gary Miller said:
The syntax could be different. The query builder will put in
some more parenthesis, brackets and the like as well as
fuller references to the tables on each field.
Are the 'results' of the recordset different? Shouldn't be.
If there is, copy and post both sets of SQL.
Gary Miller
VM said:
That's what I needed.
I did notice that the result of my query, when running from VBA, is
different than the result of the exact query when running it from the Access
Sql view.
VM
Gotcha! Actually, you were very close at the beginning.
Bogdan gave you one method. Here is another that would be
good for opening it from a command button directly on your
form.
Dim strDocName As String, strSQL as String
Dim datBefore as Date, datAfter as Date
' Pick up the dates from your form. You will want some
' error handling in case they are empty
datBefore = Me.BeforeDate.Value
datAfter = Me.AfterDate.Value
strDocName = "rptYourReportName"
' Here is your SQL string
strSQL = "SELECT * from TableX where [BeforeDate] >= #" & _
datBefore & "# AND #" [AfterDate] <= #" & _
datAfter & "#"
' Now here is the VBA code to open the report. Note that the
' strSQL is the last item and is positioned in the 'WHERE'
section
' of the OpenReport command which is used to limit the
records of the report.
' The report should be setup to show all records from the
same base source initially
DoCmd.OpenReport stDocName, acPreview, , strSQL
Is this what you were looking for?
Gary Miller
Yes.
The report, when you double-click on it, asks for the
BeforeDate and
AfterDate (because the SQL query in the Access SQL view
has
[Forms]![UnivCriteriaFrm]![txtFrom] and
[Forms]![UnivCriteriaFrm]![txtTo]).
I'd like to be able to (literally) move this SQL statement
into VBA. The
only difference would be that, instead of using
[Forms]![UnivCriteriaFrm]![txtFrom], I'll be referring to
the VB textboxes
(me.BeforeDate.Value ).
Then, when I get the recordset from this query, I'd like
to be able to
display it in the report.
What I'm not sure of is how to take that recordset and
"move" it into the
report.
Vaughn
message
How would I incorporate this query into the VB Code?
It all depends what you want to do with it. Open a
recordset
maybe? You may need to give some more details of what
you
are trying to do.
Do be aware that you are still missing the critical '#'
signs needed for date values.
Gary Miller
Sisters, OR
Thanks for your help.
But if I choose to use the whole query, how would I do
it?
In my case, it'd be:
"Select * from TableX where BeforeDate >= " &
me.BeforeDate.Value & " and
AfterDate <= " & me.AfterDate.Value
How would I incorporate this query into the VB Code?
Thanks again.
VM
I'm new at VBA and I wanted to know how I'd be able
to
execute a report I
already created through a simple form with two
textboxes
and the exe
cution
button.
Basically, the report asks for a "Before" and
"After"
date, and I'd like
to
be able to create a form that:
1) Has two textboxes: BeforeDate and AfterDate
2) Has a button that, when pressed, runs a specific
SQL
statement based on
the Entered dates and sends the recordset to the
report.
The form design is pretty easy. The only hard thing
(for
me) is that the
form interact with the report.
Thanks,
VM