472,992 Members | 3,326 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,992 software developers and data experts.

Report the current filtered records from a Form

---Report the current filtered records from a Form---

Hello All,

I've seen this topic discussed before, but the solution described then
doesn't work in my particular case.

My Config: Access 2002 front-end using SQL Server 2000 (MSDE actually)
via ADP/ADE Access Data Project.

I have a form (containing about 80 fields) on which I allow the user to
apply field/form filters -- this works fine.

The form has a <print reportbutton which should trigger a report based
on that filtered recordset -- this does not work.

I've tried the following VBA code in the button's click event handler,
which I've read should work in plain Access, but fails for me because I'm
using SQL Server at the back-end.

Dim strWhere As String
strWhere = ""
If Me.Dirty Then
Me.Dirty = False
End If
If Me.FilterOn Then
strWhere = Me.Filter
End If
DoCmd.OpenReport "myreport", acPreview, , strWhere

The above code passes the form's current filter as a parameter to
OpenReport, which fails with various SQL syntax errors because Me.Filter
contains a SQL "where" clause (without the word "where), but it is in
Access/Jet SQL format, not SQLServer's SQL format. It contains quotes
instead of apostrophes around strings, uses "=True" for YES/NO fields
instead of "=1" or "=-1" for SQLServer's BIT type fields, etc. This is
strange because Access knows that it's using a SQLServer backend (it's a
..ADP project).

Ideally I'd like to know how to pass the actual recordset to the report
(instead of the above attempt at having the report re-query the db), but
this doesn't seem possible?

A second-best solution I guess would be a way to retrieve the Me.Filter
value in SQLServer's format, or call a function which does that for me.

Perhaps there are completely different approaches/solutions?

ANY help would be VERY MUCH appreciated! Please don't assume any idea is
too obvious to suggest -- while I'm a 20-year coding veteran, I'm VERY
new to Access and VBA.

A happy bonus to a solution would be to be able to pass the current sort-
ordering of the form to the report as well.

Thank you very much,

melnhed
Oct 27 '06 #1
3 3394
On 27 Oct 2006 04:04:25 GMT, melnhed <x@y.zwrote:

It's still Access applying the filter, that's why it's in that format.
But it seems you already know what to replace, so why don't you use
the Replace function to do so.

One alternative is to iterate over the RecordsetClone, collect all
primary key values, save them to a "temporary" table (say tblTempPK),
and use that table to innerjoin with:
select * from Customers
inner join tblTempPK on Customers.CustomerID = tblTempPK.PrimaryKey
This would restrict all customers to the selected ones.
Great trick for when a really complex filter has been applied using
FilterByForm.

-Tom.

>---Report the current filtered records from a Form---

Hello All,

I've seen this topic discussed before, but the solution described then
doesn't work in my particular case.

My Config: Access 2002 front-end using SQL Server 2000 (MSDE actually)
via ADP/ADE Access Data Project.

I have a form (containing about 80 fields) on which I allow the user to
apply field/form filters -- this works fine.

The form has a <print reportbutton which should trigger a report based
on that filtered recordset -- this does not work.

I've tried the following VBA code in the button's click event handler,
which I've read should work in plain Access, but fails for me because I'm
using SQL Server at the back-end.

Dim strWhere As String
strWhere = ""
If Me.Dirty Then
Me.Dirty = False
End If
If Me.FilterOn Then
strWhere = Me.Filter
End If
DoCmd.OpenReport "myreport", acPreview, , strWhere

The above code passes the form's current filter as a parameter to
OpenReport, which fails with various SQL syntax errors because Me.Filter
contains a SQL "where" clause (without the word "where), but it is in
Access/Jet SQL format, not SQLServer's SQL format. It contains quotes
instead of apostrophes around strings, uses "=True" for YES/NO fields
instead of "=1" or "=-1" for SQLServer's BIT type fields, etc. This is
strange because Access knows that it's using a SQLServer backend (it's a
.ADP project).

Ideally I'd like to know how to pass the actual recordset to the report
(instead of the above attempt at having the report re-query the db), but
this doesn't seem possible?

A second-best solution I guess would be a way to retrieve the Me.Filter
value in SQLServer's format, or call a function which does that for me.

Perhaps there are completely different approaches/solutions?

ANY help would be VERY MUCH appreciated! Please don't assume any idea is
too obvious to suggest -- while I'm a 20-year coding veteran, I'm VERY
new to Access and VBA.

A happy bonus to a solution would be to be able to pass the current sort-
ordering of the form to the report as well.

Thank you very much,

melnhed
Oct 27 '06 #2
Thanks very much for the reply, Tom.

Apparently this Access/VBA newbie (that's me) quite underestimated the
complexity of this task. I had assumed that generating a report on a
current recordset would be a simple task in Access, perhaps taking at
most a few minutes to implement.

I'll look into both of your suggestions.

The "Replace" idea I had already implemented for the string and
boolean/bit fields, but I recognized that I'll probably run into
problems with strings containing apostrophes or quotation marks, and
then there are possibly other filter types to investigate like
less-than/greater-than (which Access seems to support when users are
field-filtering), and probably other constructs that I'm not even aware
of yet (again, I am fairly new at this!). Too bad Access doesn't seem
to make such a conversion function available to VBA code -- or maybe it
does and we just haven't found it.

I understand your alternative idea at a high level, but I have much
learning to do before I could implement such a technique. I won't
bother you by asking for more technical details on your idea, as I can
research these myself.

I would like to ask you these higher-level questions about your
"temporary table" idea though: would Access create this table natively
in Jet format on the local machine, or would it create it on the
SQLServer back-end? If on the back-end, would this be incredibly slow
(one sql insert per record)? Also, if on the back-end, could the
temp-table be private to the current user (I need to support
multiple-users).

Thanks again for taking the time to respond.

-melnhed

Tom van Stiphout <no*************@cox.netwrote in
news:du********************************@4ax.com:
On 27 Oct 2006 04:04:25 GMT, melnhed <x@y.zwrote:

It's still Access applying the filter, that's why it's in that format.
But it seems you already know what to replace, so why don't you use
the Replace function to do so.

One alternative is to iterate over the RecordsetClone, collect all
primary key values, save them to a "temporary" table (say tblTempPK),
and use that table to innerjoin with:
select * from Customers
inner join tblTempPK on Customers.CustomerID = tblTempPK.PrimaryKey
This would restrict all customers to the selected ones.
Great trick for when a really complex filter has been applied using
FilterByForm.

-Tom.

>>---Report the current filtered records from a Form---

Hello All,

I've seen this topic discussed before, but the solution described then
doesn't work in my particular case.

My Config: Access 2002 front-end using SQL Server 2000 (MSDE
actually) via ADP/ADE Access Data Project.

I have a form (containing about 80 fields) on which I allow the user
to apply field/form filters -- this works fine.

The form has a <print reportbutton which should trigger a report
based on that filtered recordset -- this does not work.

I've tried the following VBA code in the button's click event handler,
which I've read should work in plain Access, but fails for me because
I'm using SQL Server at the back-end.

Dim strWhere As String
strWhere = ""
If Me.Dirty Then
Me.Dirty = False
End If
If Me.FilterOn Then
strWhere = Me.Filter
End If
DoCmd.OpenReport "myreport", acPreview, , strWhere

The above code passes the form's current filter as a parameter to
OpenReport, which fails with various SQL syntax errors because
Me.Filter contains a SQL "where" clause (without the word "where), but
it is in Access/Jet SQL format, not SQLServer's SQL format. It
contains quotes instead of apostrophes around strings, uses "=True"
for YES/NO fields instead of "=1" or "=-1" for SQLServer's BIT type
fields, etc. This is strange because Access knows that it's using a
SQLServer backend (it's a .ADP project).

Ideally I'd like to know how to pass the actual recordset to the
report (instead of the above attempt at having the report re-query the
db), but this doesn't seem possible?

A second-best solution I guess would be a way to retrieve the
Me.Filter value in SQLServer's format, or call a function which does
that for me.

Perhaps there are completely different approaches/solutions?

ANY help would be VERY MUCH appreciated! Please don't assume any idea
is too obvious to suggest -- while I'm a 20-year coding veteran, I'm
VERY new to Access and VBA.

A happy bonus to a solution would be to be able to pass the current
sort- ordering of the form to the report as well.

Thank you very much,

melnhed
Oct 28 '06 #3
I've been playing with the RecordsetClone as you suggested,Tom, but am
stuck at a very early stage.

For me, the following TEST-ONLY code in my form's button click handler
displays the # of records in the original recordset, not the
user-filtered subset. Any thoughts on how to make this RecordSetClone
reflect the filtered records only? Also, the RS.Filter shows as simply
the digit "0".

Code:

If Me.FilterOn Then
Dim n As Integer
Dim RS As Recordset
Set RS = Me.RecordsetClone
RS.MoveLast
n = RS.RecordCount

MsgBox "# of records: " & CStr(n)
MsgBox RS.Filter
End If
Thank you again for any input!


Tom van Stiphout <no*************@cox.netwrote in
news:du********************************@4ax.com:
On 27 Oct 2006 04:04:25 GMT, melnhed <x@y.zwrote:

It's still Access applying the filter, that's why it's in that format.
But it seems you already know what to replace, so why don't you use
the Replace function to do so.

One alternative is to iterate over the RecordsetClone, collect all
primary key values, save them to a "temporary" table (say tblTempPK),
and use that table to innerjoin with:
select * from Customers
inner join tblTempPK on Customers.CustomerID = tblTempPK.PrimaryKey
This would restrict all customers to the selected ones.
Great trick for when a really complex filter has been applied using
FilterByForm.

-Tom.

>>---Report the current filtered records from a Form---

Hello All,

I've seen this topic discussed before, but the solution described then
doesn't work in my particular case.

My Config: Access 2002 front-end using SQL Server 2000 (MSDE
actually) via ADP/ADE Access Data Project.

I have a form (containing about 80 fields) on which I allow the user
to apply field/form filters -- this works fine.

The form has a <print reportbutton which should trigger a report
based on that filtered recordset -- this does not work.

I've tried the following VBA code in the button's click event handler,
which I've read should work in plain Access, but fails for me because
I'm using SQL Server at the back-end.

Dim strWhere As String
strWhere = ""
If Me.Dirty Then
Me.Dirty = False
End If
If Me.FilterOn Then
strWhere = Me.Filter
End If
DoCmd.OpenReport "myreport", acPreview, , strWhere

The above code passes the form's current filter as a parameter to
OpenReport, which fails with various SQL syntax errors because
Me.Filter contains a SQL "where" clause (without the word "where), but
it is in Access/Jet SQL format, not SQLServer's SQL format. It
contains quotes instead of apostrophes around strings, uses "=True"
for YES/NO fields instead of "=1" or "=-1" for SQLServer's BIT type
fields, etc. This is strange because Access knows that it's using a
SQLServer backend (it's a .ADP project).

Ideally I'd like to know how to pass the actual recordset to the
report (instead of the above attempt at having the report re-query the
db), but this doesn't seem possible?

A second-best solution I guess would be a way to retrieve the
Me.Filter value in SQLServer's format, or call a function which does
that for me.

Perhaps there are completely different approaches/solutions?

ANY help would be VERY MUCH appreciated! Please don't assume any idea
is too obvious to suggest -- while I'm a 20-year coding veteran, I'm
VERY new to Access and VBA.

A happy bonus to a solution would be to be able to pass the current
sort- ordering of the form to the report as well.

Thank you very much,

melnhed
Nov 1 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Viktor Lakics | last post by:
Dear All, I designed carefully a reasonably complicated report and a form. I use this form for data entry and also for searching by setting various filters on that form. What I would like to...
2
by: Koen | last post by:
Hi all, I've struggled with the following problem and welcome all a suggestions to solve this. I have a form that list records from a table with observations. The record source for the table...
1
by: Nathan Bloomfield | last post by:
Does anyone know if there is any documentation which relates to Access2k + ? or can anyone help adjust the code? I am having trouble converting the DAO references. TITLE :INF: How to...
4
by: Sami | last post by:
I hope someone will tell me how to do this without having to do any VB as I know nothing in that area. I am a rank beginner in using Access. I have created a database consisting of student...
15
by: Richard Hollenbeck | last post by:
I tried to ask this question before on the 14th of January but I never got a reply. I'm still struggling with the problem. I'll try to rephrase the question: I have a crosstab query with rows...
16
by: radio1 | last post by:
---Report the current filtered records from a Form (in an ADP project)--- Hello All, I've seen this topic discussed before, but the solution described then doesn't work in my particular case. ...
6
by: Dave | last post by:
On my form I have combo boxes. These combo boxes, after updating them, populate respective listboxes that are located below the combo boxes on the same form. I am trying to use a "generate...
4
by: Ironr4ge | last post by:
Hi everyone, I am trying to open the form "Languages" with a diffrent record source to the "Contacts" form where I conducted the search or filter... . I was wondering whether there was a vba...
3
by: franc sutherland | last post by:
Hello, I have a report which I filter using the me.filter command in the OnOpen event. Me.Filter = "OrderID=" & Forms!variable_form_name! Me.FilterOn = True I want to be able to open that...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.