473,761 Members | 9,480 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How To Use Dynamic Queries As Report RecordSource?


All:

I'm using Access 2000 on a Windows XP platform.

My goal is to use a form to gather user criteria which I will then parse
into a useable SQL string. At this point I would like to open one or more
reports and use the query as the recordsource.

What would be a common approach to doing this? I have been using ADO
methods up until this point for other tasks.

Using docmd.openrepor t is easy, but it doesn't accommodate a change in a
report's recordsouce from what I can tell although it does allow for SQL
filtering.

Your advice is appreciated and I'm happy to provide further information upon
request.

Best,

Steve
Nov 13 '05 #1
9 14889
Steve,

Take a look at QueryDef in the Help file. Basically you have a query with a
name saved in the database. With QueryDef you dynamically create the SQL for
the query which can add or subtract fields from the saved query as well as
addm or delete criteria. Then you just use the name of the query as the
recordsource of your reports. You are actually doing the same thing as you
would do if you dynamically changed the record source of the reports.

--
PC Datasheet
Your Resource For Help With Access, Excel And Word Applications
re******@pcdata sheet.com
www.pcdatasheet.com
"mooseshoes " <mo********@gmx .net> wrote in message
news:Tb******** *********@newss vr21.news.prodi gy.com...

All:

I'm using Access 2000 on a Windows XP platform.

My goal is to use a form to gather user criteria which I will then parse
into a useable SQL string. At this point I would like to open one or more
reports and use the query as the recordsource.

What would be a common approach to doing this? I have been using ADO
methods up until this point for other tasks.

Using docmd.openrepor t is easy, but it doesn't accommodate a change in a
report's recordsouce from what I can tell although it does allow for SQL
filtering.

Your advice is appreciated and I'm happy to provide further information upon request.

Best,

Steve

Nov 13 '05 #2


<snip>
Take a look at QueryDef in the Help file. Basically you have a query with
a name saved in the database. With QueryDef you dynamically create the SQL
for the query which can add or subtract fields from the saved query as
well as addm or delete criteria. Then you just use the name of the query
as the recordsource of your reports. You are actually doing the same thing
as you would do if you dynamically changed the record source of the
reports.

Thanks. No "QueryDef" in my Help Index or Answer Wizard. I'm actually

using MS Access 2002 (10.2627.2625) which came with MS Office Pro. It
looks like it is dummed down to A2K for some reason which is fine because
I'm developing for an A2K box, but I was wondering about the odd
configuration. Nevertheless, I can't surface any querydef help content.
Can you suggest how I might open the door to these capabilities on my
Access version
Steve
Nov 13 '05 #3
Hey MooseShoes..

If the query isn't too complex, you can build a SQL statement using a global
variable. Define in a Module file.

Then in the Open Event in the report you can use:

Me.Recordsource = gstr_SQL << This is the SQL string that you build
dynamically

Now you have to be really careful the fields in gstr_SQL match the fields in
the report. Otherwise it will error.

Hope this helps..

Barry

Then in the report.open
"mooseshoes " <mo********@gmx .net> wrote in message
news:Tb******** *********@newss vr21.news.prodi gy.com...

All:

I'm using Access 2000 on a Windows XP platform.

My goal is to use a form to gather user criteria which I will then parse
into a useable SQL string. At this point I would like to open one or more
reports and use the query as the recordsource.

What would be a common approach to doing this? I have been using ADO
methods up until this point for other tasks.

Using docmd.openrepor t is easy, but it doesn't accommodate a change in a
report's recordsouce from what I can tell although it does allow for SQL
filtering.

Your advice is appreciated and I'm happy to provide further information upon request.

Best,

Steve

Nov 13 '05 #4
Did you look in VBA Help? Open a standard module in design view and try Help
there.
"mooseshoes " <mo********@gmx .net> wrote in message
news:un******** ***********@new ssvr29.news.pro digy.com...


<snip>
Take a look at QueryDef in the Help file. Basically you have a query with
a name saved in the database. With QueryDef you dynamically create the SQL for the query which can add or subtract fields from the saved query as
well as addm or delete criteria. Then you just use the name of the query
as the recordsource of your reports. You are actually doing the same thing as you would do if you dynamically changed the record source of the
reports.

Thanks. No "QueryDef" in my Help Index or Answer Wizard. I'm actually

using MS Access 2002 (10.2627.2625) which came with MS Office Pro. It
looks like it is dummed down to A2K for some reason which is fine because
I'm developing for an A2K box, but I was wondering about the odd
configuration. Nevertheless, I can't surface any querydef help content.
Can you suggest how I might open the door to these capabilities on my
Access version
Steve

Nov 13 '05 #5
Hi Steve,

I like to this by:
1.) contructing the entire SQL string in code.
2.) use that SQL string as the recordsource for a datasheet subform to
"preview" the data that will be used for the report.
3.) Use the same SQL string as the recordsource for the report.

*************** ****** Code **************
Option Compare Database
Option Explicit
Dim MySQL As String
Dim whr As String

'------------------------------
Public Sub GetSQL()

'--------------- Step One ------------------
'-------- Build the SELECT portion ---------
MySQL = ""
MySQL = MySQL & "SELECT tblContacts.* "
MySQL = MySQL & "FROM tblContacts"

'--------------- Step Two ------------------
'- Determine data type of the compare value -
'-------- and format it accordingly. --------
Dim CV
CV = (Me.txtCompareV alue)

If IsNumeric(CV) Then
CV = CLng(CV)
ElseIf IsDate(CV) Then
CV = "#" & CDate(CV) & "#"
Else
CV = Chr(34) & CV & Chr(34)
End If
'--------------- Step Three ------------------
'-- Insert the selected comparison operator ---
'-- and ready the string for assembly into ----
'------------- the WHERE statement ------------
whr = ""
Select Case Me.optCriteriaT ype
Case 1 'Equal To
whr = whr & " = "
whr = whr & CV
Case 2 'Greater Than
whr = whr & " > "
whr = whr & CV
Case 3 'Less Than
whr = whr & " < "
whr = whr & CV
Case 4 'Like _____
'WHERE (((tblContacts. ContactName) Like "D" & '*'));
whr = whr & " Like "
whr = whr & CV
whr = whr & " & '*'"
Case 5 'Contains ____
'WHERE (((tblContacts. ContactName) Like '*' & "on" & '*'));
whr = whr & " Like "
whr = whr & "'*' & "
whr = whr & CV
whr = whr & " & '*'"
Case Else
whr = ""
End Select
'--------------- Step Four ------------------
'--- IF the user has entered any Criteria ----
'--- construct a WHERE clause and insert -----
'-------- it into the SQL statement ----------
If Len(whr) > 0 Then
MySQL = MySQL & " WHERE (((tblContacts. "
MySQL = MySQL & Me.lstFieldName s & ")"
MySQL = MySQL & whr & " ))"
End If
'--------------- Step Five ------------------
'----- "Close out" the SQL statement ---------
'-- There may be those that argue that this --
'-- step is unnessecary, but it's just good --
'------- programming practice, IMHO ----------
MySQL = MySQL & " ;"
'Debug.Print MySQL

'--------------- Step Six ---------------------
'- Use this SQL statement as the RecordSource -
'- for the subform (and a report if you like) -

Me.sbfContacts. Form.RecordSour ce = MySQL
End Sub

'------------------------------------------
Private Sub cmdOpenReport_C lick()
On Error GoTo Err_cmdOpenRepo rt_Click

Dim stDocName As String

stDocName = "rptContact s"
DoCmd.OpenRepor t stDocName, acPreview, MySQL

Exit_cmdOpenRep ort_Click:
Exit Sub

Err_cmdOpenRepo rt_Click:
MsgBox Err.Description
Resume Exit_cmdOpenRep ort_Click

End Sub
*************** *************** *************** *************

--
HTH,
Don
=============== ==============
E-Mail (if you must) My*****@Telus.n et

Disclaimer:
Professional PartsPerson
Amateur Database Programmer {:o)

I'm an Access97 user, so all posted code samples are also Access97- based
unless otherwise noted.

=============== =============== =============== =============== ==============
"mooseshoes " <mo********@gmx .net> wrote in message
news:Tb******** *********@newss vr21.news.prodi gy.com...

All:

I'm using Access 2000 on a Windows XP platform.

My goal is to use a form to gather user criteria which I will then parse
into a useable SQL string. At this point I would like to open one or more
reports and use the query as the recordsource.

What would be a common approach to doing this? I have been using ADO
methods up until this point for other tasks.

Using docmd.openrepor t is easy, but it doesn't accommodate a change in a
report's recordsouce from what I can tell although it does allow for SQL
filtering.

Your advice is appreciated and I'm happy to provide further information upon request.

Best,

Steve

Nov 13 '05 #6
Hi Steve,

Install the optional DAO component and the related help files.
Microsoft really blew it by messing with the help files that were included
with Access97.

Allen Browne has summed the details on this subject in this post:
http://groups.google.ca/groups?q=%22....net.au&rnum=2

"mooseshoes " <mo********@gmx .net> wrote in message
news:un******** ***********@new ssvr29.news.pro digy.com...


<snip>
Take a look at QueryDef in the Help file. Basically you have a query with
a name saved in the database. With QueryDef you dynamically create the SQL for the query which can add or subtract fields from the saved query as
well as addm or delete criteria. Then you just use the name of the query
as the recordsource of your reports. You are actually doing the same thing as you would do if you dynamically changed the record source of the
reports.

Thanks. No "QueryDef" in my Help Index or Answer Wizard. I'm actually

using MS Access 2002 (10.2627.2625) which came with MS Office Pro. It
looks like it is dummed down to A2K for some reason which is fine because
I'm developing for an A2K box, but I was wondering about the odd
configuration. Nevertheless, I can't surface any querydef help content.
Can you suggest how I might open the door to these capabilities on my
Access version
Steve

Nov 13 '05 #7
<snip>
Install the optional DAO component and the related help files.
Microsoft really blew it by messing with the help files that were included
with Access97.

Allen Browne has summed the details on this subject in this post:
http://groups.google.ca/groups?q=%22....net.au&rnum=2
"mooseshoes " <mo********@gmx .net> wrote in message
news:un******** ***********@new ssvr29.news.pro digy.com...


VBA Help and DAO Library now installed. I'll proceed with QueryDef.

Helpful article from Allen Browne. Thank you.

Steve
Nov 13 '05 #8
Bri
Steve,

Leave the Recordsource for the report blank and then in the Open Event
of the Report build the SQL string and assign it to the Recordsource.
The string can be built using info from the form. Here's an aircode example:

Sub Report_Open()
Dim stSQL as String

stSQL = "SELECT MyData.* FROM MyData " & _
"WHERE MyField='" & Forms!MyForm!My Criteria & "'"
Me.RecordSource = stSQL

End Sub

Hope this helps.

Bri

mooseshoes wrote:
All:

I'm using Access 2000 on a Windows XP platform.

My goal is to use a form to gather user criteria which I will then parse
into a useable SQL string. At this point I would like to open one or more
reports and use the query as the recordsource.

What would be a common approach to doing this? I have been using ADO
methods up until this point for other tasks.

Using docmd.openrepor t is easy, but it doesn't accommodate a change in a
report's recordsouce from what I can tell although it does allow for SQL
filtering.

Your advice is appreciated and I'm happy to provide further information upon
request.

Best,

Steve

Nov 13 '05 #9
mooseshoes <mo********@gmx .net> wrote in
news:Tb******** *********@newss vr21.news.prodi gy.com:

All:

I'm using Access 2000 on a Windows XP platform.

My goal is to use a form to gather user criteria which I will then parse
into a useable SQL string. At this point I would like to open one or
more reports and use the query as the recordsource.

What would be a common approach to doing this? I have been using ADO
methods up until this point for other tasks.

Using docmd.openrepor t is easy, but it doesn't accommodate a change in a
report's recordsouce from what I can tell although it does allow for SQL
filtering.

Your advice is appreciated and I'm happy to provide further information
upon request.

Best,

Steve


This is very old and rather clumsy code. But the idea is, I believe, sound.
RptCharges has, as its recordset, QryCharges. The code changes the SQL for
QryCharges, checks to see if there are any records, (<- the clumsy part)
when that query is opened, and if it finds any, opens the report, which, of
course uses the query whose SQL has been modified, and then does some other
stuff. Be careful of new client inserted line breaks.

**********
Const qryChargesName As String = "qryCharges "
Function fCreateAccountF ile(strAccountN umber As String)
Dim qdf As QueryDef
Dim rcs As Recordset
Dim strFullPath As String
Set qdf = DBEngine(0)(0). QueryDefs(qryCh argesName)
With qdf
.SQL = "SELECT * FROM tblCharges WHERE Sent = No And Account = " &
_
Chr(34) & strAccountNumbe r & Chr(34)
Set rcs = .OpenRecordset
With rcs
If .RecordCount <> 0 Then
strFullPath = Environ("temp") & _
"\" & "Chargeable Messages for " & strAccountNumbe r &
".rtf"
DoCmd.OutputTo _
acOutputReport, "rptCharges ", acFormatRTF, strFullPath,
False
End If
End With
End With
Set rcs = Nothing
Set qdf = Nothing
End Function

Sub test()
fCreateAccountF ile "905 555-5555"
End Sub
**********

--
Lyle
--
use iso date format: yyyy-mm-dd
http://www.w3.org/QA/Tips/iso-date
Nov 13 '05 #10

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

Similar topics

1
2056
by: ano1optimist | last post by:
Is it possible to create an ADO recordset as set it as the report recordsource? I was able to use this logic on forms but have been unable to use it on reports. HELP.
1
17672
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 Create a Dynamic Crosstab Report PRODUCT :Microsoft Access PROD/VER:1.00 1.10 OPER/SYS:WINDOWS
1
4096
by: mtech1 | last post by:
Access 2002 I am trying to create a dynamic crosstab report that parameters come from 3 different forms. I get runtime error 3070 - The Microsoft Jet database engine does not recognize 'Forms!frmDefaults!ProviderID' as a valid field name or expression, and debug takes me to line 60 below. Any Suggestions Would Be Truly Appreciated!
0
3683
by: Benny | last post by:
Hello Experts, Currently Im working on a web application using asp.net with C#. I need to create a report depends on user's selection of the database fields, i.e. the user may want to display the customer name and address (from the customers table) on the report. The question is how can I create a dynamic crystal report for this situation? The crystal report will only display the customer name and address for this example.
1
1134
by: Robert Strickland | last post by:
I need to create a dynamic PDF report based on values pulled from the database. I am using a XSL-FO rendering component that takes my FO and creates a PDF stream. Ii would like to send back the PDF to the client browser. I would like to use a HTTP Handler to chapture a PDF extension, build the pdf report, and send back a pdf stream without saving the report disk. Is this possible and if so could some one direct me to a site that provides some...
1
3456
by: Hank | last post by:
I'm using Access 2000 and want to set up the dataset for a subreport. I trying to use the Forms!SubForms scenario as a guide but I cannot get it. Here is my try: Reports!!.Report.Recordsource = SQL Statement Also Tried: Me!.RecordSource = SQL Statement My syntax is not right. Can anyone help? Thanks, Hank Reed
3
3472
by: WindowsAndDoors | last post by:
Hey, I'm trying to assign to a report recordsource a variable name but I keep getting a message that the database isn't recognizing the control name. The name of the report is BusinessUnitProductMeeting and this is how I'm referencing it in my VBA code in a Report_open procedure: Reports!BusinessUnitProductMeeting!Controls!Recordsource = rpt_recordsource What is wrong with the way I have that statement formed?
1
2230
by: khushbubhalla | last post by:
I am writing a procedure in which i am using dynamic queries. I am assigning the dynamic query to a variable and then executing the query . but as in when the query grows i can t keep it in a variable. so how shud i execute the query another problem is the result set is a set of records , how can i get the records My proc as of now Create or replace procedure datacompare (tab_name1 In varchar2,
1
1304
by: vilas556 | last post by:
I am developing Desktop application and i want to use Dynamic Crystal report then how I Assign Dynamic Path Of Database to crystal report.
14
7858
ollyb303
by: ollyb303 | last post by:
Hi, I am trying to create a dynamic crosstab report which will display number of calls handled (I work for a call centre) per day grouped by supervisor. I have one crosstab query (Query1) which has the following fields: SPID (supervisor ID), total:group by, as row heading Date, total:group by, as column heading Calls handled, total:sum, as value Date, total:where, criteria between and - this is taken from a form,
0
9531
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9345
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10115
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9957
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9905
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8780
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6609
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5229
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3881
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.