473,473 Members | 2,255 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 3428
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...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.