473,658 Members | 2,623 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Changing report source query at runtime

8 New Member
I have a report that uses a parameter query where the parameter is supplied by a form in my db - frmSINGLE. I need to run this report based on a different form - frmMULTI. frmSINGLE may or may not be open - in any case, I don't want to use it.

I would like to avoid having two reports (and two queries) that have the exact same contents save one criteria on one field - this db is big enough as is. So my plan is to change the record source on the report when I run the report from frmMULTI, then switch it back sometime later.

Anyone have any thoughts?

~KJ
Sep 20 '07 #1
16 9176
KaleeyJ
8 New Member
I think that I may be forced to do the multiple rpeorts solution - seems the only way to programmaticall y change a report's source is to do so in the OnOpen Event of the report - I would much rather be able to do it from my form (so I can set it back as soon as I am finished, among other reasons).

If anyone comes up with a better solution (that doesn't involve changing the code in the report's on open event), I would appreciate it, but right now I am going with two separate reports.

~KJ
Sep 20 '07 #2
NeoPa
32,568 Recognized Expert Moderator MVP
Kaleey,

The OnOpen event is not such a bad approach actually as it doesn't save the changes to the object design. It only changes the settings in that INSTANCE of the object (in other words while it is running).
I have used this approach a number of times to very beneficial effect. It's much better design than multiple reports and takes less maintenance too, when the report needs updating.

Let us know how you get on :)
Sep 20 '07 #3
martin DH
114 New Member
Could one of you post an example of code for changing a report's record source on open?

Thank you.
martin
May 28 '08 #4
NeoPa
32,568 Recognized Expert Moderator MVP
[code]'On Open, this report determines the state of the form and adjusts various
'things based on that. If form not found then it defaults to showing :
' Main Stock Products only
' Costs
' Opal Column Prices
' Sorting must be handled by multiple reports.
Private Sub Report_Open(Can cel As Integer)
Dim intSort As Integer, intSelRange As Integer, intCols As Integer ', intPCs As Integer
Dim intSpare As Integer, intShift As Integer
Dim strSort As String, strWork As String, strType As String
Dim strPC1 As String, strPC2 As String
Dim blnCosts As Boolean, blnReplacement As Boolean, blnAll As Boolean
Dim blnVisible As Boolean

On Error Resume Next
'If next line fails then intFrom stays 0 - otherwise it will be > 0
intFrom = Forms("frm" & conStub).fraFro m
On Error GoTo 0
If intFrom = 0 Then
'Form not found
intSelRange = 1
blnCosts = True
blnReplacement = True
blnAll = False
intSort = 1
strSort = "Prod Group then Code"
intFrom = 3
intCols = 2
'intPCs = 1
dblPC2 = 100 / 60 '40% GPM
Else
'Form found - use settings from form
With Forms("frm" & conStub)
intSelRange = IIf(.chkLawForm s, 2, 0) + IIf(.chkMainSto ck, 1, 0)
blnCosts = .chkShowCost
blnReplacement = .chkReplacement
blnAll = .chkAll
intSort = .fraSort
Select Case intSort
Case 1
strSort = .lblPGrpCode.Ca ption
Case 2
strSort = .lblPGrpDesc.Ca ption
Case 3
strSort = .lblProdCode.Ca ption
Case 4
strSort = .lblProdDesc.Ca ption
End Select
intCols = IIf(blnReplacem ent, 1, 0)
Select Case intFrom
Case 1
If Not blnReplacement Then intCols = 6
Case 2, 3
intCols = intCols + 1
strPC1 = Nz(.txtPC1, "")
dblPC1 = IIf(intFrom = 2, (100 + CDbl(strPC1)) / 100, _
100 / (100 - CDbl(strPC1)))
If blnReplacement Then
'First PC shows in second column
strPC2 = strPC1
dblPC2 = dblPC1
ElseIf (.txtPC2.Enable d And Not IsNull(.txtPC2) ) Then
'We may have a second PC column
intCols = 2
strPC2 = .txtPC2
dblPC2 = IIf(intFrom = 2, (100 + CDbl(strPC2)) / 100, _
100 / (100 - CDbl(strPC2)))
End If
End Select
End With
End If
'First step is to change the Recordsource if required...
If blnAll Then Me.RecordSource = "qry" & conStub & "All"
'Set up Sorting & Grouping
'Only show PGroupHdr if sorting primarily by PGroup and turn triggering
'to as few as possible (using Prefix) if not showing (otherwise Each)
'Assume both levels must be set with different fields regardless
PGroupHdr.Visib le = (intSort < 3)
'.GroupOn ==> 0 = Each; 1 = Prefix
GroupLevel(1).G roupOn = IIf(intSort < 3, 0, 1)
Select Case intSort
Case 1
GroupLevel(0).C ontrolSource = "PGroup"
GroupLevel(1).C ontrolSource = "Product"
Case 2
GroupLevel(0).C ontrolSource = "PGroup"
GroupLevel(1).C ontrolSource = "ProdDesc"
Case 3
GroupLevel(0).C ontrolSource = "Product"
GroupLevel(1).C ontrolSource = "PGroup"
Case 4
GroupLevel(0).C ontrolSource = "ProdDesc"
GroupLevel(1).C ontrolSource = "PGroup"
End Select
With txtTitle
Select Case intFrom
Case 1
strType = "Column" & IIf(blnReplacem ent, " 1 Only", "s")
Case 2
strType = "Markups"
Case 3
strType = "GPMs"
End Select
.ControlSource = MultiReplace(.T ag, "%S", strSort, "%T", strType)
End With
If intSelRange = 3 Then
FilterOn = False
Else
FilterOn = True
strWork = Split(Expressio n:=conFilters, Delimiter:="|") (intSelRange - 1)
Filter = Replace(strWork , "%L", conLawForms)
End If
lblCost.Visible = blnCosts
'Adjust widths and visibilities of various fields
'Some field attribute may need to be adjusted from their starting values
'Ignore some starting values in the design as they are just to fit visibly
'ProdDesc.Width = 7.354cm (4,170) conDescWidth
'Unit.Width = 1.24cm (703) conUnitWidth
'PriceFields.Wi dth = 1.199cm (680) conPriceWidth
'Expand width of Description in all scenarios except when both the Cost AND
'All Columns are shown.
Me.lblProdDesc. Width = conDescWidth + IIf(blnCosts And (intCols = 6), _
0, Me.lblCost.Widt h)
'Set captions, width and visibility for Column fields used
Me.lblPrice1.Ca ption = Replace(conColL bl, "%N", "1")
Me.txtPrice1.Co ntrolSource = "Price1"
Me.lblPrice1.Wi dth = conPriceWidth
Me.lblPrice2.Ca ption = Replace(conColL bl, "%N", "2")
Me.txtPrice2.Co ntrolSource = "Price2"
Me.lblPrice2.Wi dth = conPriceWidth
strWork = Replace(strType , "s", "") & vbCrLf
If intFrom > 1 Then
Me.txtPrice2.Co ntrolSource = "=Round([Cost]*" & dblPC2 & ",2)"
If blnReplacement Then
'First column same as default (Price1)
Me.lblPrice2.Ca ption = MultiReplace("M in Sell%L%N%", "%N", strPC1, _
"%L", vbCrLf)
Else
Me.lblPrice1.Ca ption = strWork & strPC1 & "%"
Me.txtPrice1.Co ntrolSource = "=Round([Cost]*" & dblPC1 & ",2)"
Me.lblPrice2.Ca ption = strWork & strPC2 & "%"
End If
End If
Me.lblPrice2.Vi sible = (intCols > 1)
'Special handling for new style report (chkReplacement = True)
Me.lblReplaceme nt.Visible = blnReplacement
'Handle last 4 column fields (Only leave required fields visible)
blnVisible = (intCols > 2)
Me.lblPrice3.Vi sible = blnVisible
Me.lblPrice4.Vi sible = blnVisible
Me.lblPrice5.Vi sible = blnVisible
Me.lblPrice6.Vi sible = blnVisible
Me.txtPrice3.Vi sible = blnVisible
Me.txtPrice4.Vi sible = blnVisible
Me.txtPrice5.Vi sible = blnVisible
Me.txtPrice6.Vi sible = blnVisible
'Build up positions and mirror widths & visibilities
intSpare = (Me.Width - BuildUp() - 10) / 4
If (intCols < 6) And (intSpare > 10) Then
If intSpare > conExtend Then
intShift = 2 * (intSpare - conExtend)
intSpare = conExtend
Else
intShift = 0
End If
Me.lblUnit.Widt h = conUnitWidth + intSpare
Me.lblCost.Widt h = conPriceWidth + intSpare
Me.lblPrice1.Wi dth = conPriceWidth + intSpare
Me.lblPrice2.Wi dth = conPriceWidth + intSpare
Call BuildUp(intShif t)
End If
'Set up Report ID & date for bottom left corner of report
strDate = DLookup(Expr:="
Expand|Select|Wrap|Line Numbers
  1. ", _
  2.                       Domain:="[tblReport]", _
  3.                       Criteria:="[ReportName]='" & Name & "'") & _
  4.                                 Format(Date, " - d mmmm yyyy")
  5. End Sub
May 28 '08 #5
NeoPa
32,568 Recognized Expert Moderator MVP
It doesn't need to be quite that complicated obviously :D
Expand|Select|Wrap|Line Numbers
  1. Private Sub Report_Open(Cancel As Integer)
  2.     Dim intTest As Integer
  3.  
  4.     On Error Resume Next
  5.     'If next line fails then intTest stays 0 - otherwise it will be > 0
  6.     intTest = Forms("frmMyForm").fraFrom
  7.     On Error GoTo 0
  8.     'First step is to change the Recordsource if required...
  9.     If intTest = 0 Then
  10.         Me.RecordSource = "qryMyJobAll"
  11.     Else
  12.         Me.RecordSource = "qryMyJob"
  13.     End If
  14. End Sub
May 28 '08 #6
martin DH
114 New Member
Okay, here's my situation:

I would like to open a single report in one of two sort orders based on a selection on a form. I have two queries (one with one sort order and a second with another). I have the following code on the On_Open event of the report. The report has no control source named in its Properties. Am I going about this incorrectly, because this opens the report with the correct data but does not sort the data?

Expand|Select|Wrap|Line Numbers
  1. Private Sub Report_Open(Cancel As Integer)
  2. 'changes sort order on report by changing record source
  3. If Forms![frmAgedNotices]!fraSort = 1 Then
  4.   Me.RecordSource = "qryAgedNoticesGroupedAge"
  5. ElseIf Forms![frmAgedNotices]!fraSort = 2 Then
  6.   Me.RecordSource = "qryAgedNoticesGroupedAgency"
  7. End If
  8.  
  9. End Sub
Thanks!
May 28 '08 #7
Stewart Ross
2,545 Recognized Expert Moderator Specialist
Hi. If I may add a comment to this intriguing thread - the sort order for a report is defined in the report's design view and does not depend on the sort order of the recordsource query. Your sorting issues must result from (1) the sort order of the report not being defined correctly, or (2) the defined sort order having to be changed to fit the appropriate recordsource.

If it is cause (1) you should be able to resolve the sorting using the Sorting and Grouping facilities in design view. If it is (2), that you need to change the sort order for one of the recordsources, you will need a programmatic approach using the CreateGroupLeve l properties of the report object to create a new grouping and then set its associated sort properties. The help file will guide you how to do this.

-Stewart

Okay, here's my situation:

I would like to open a single report in one of two sort orders based on a selection on a form. I have two queries (one with one sort order and a second with another). I have the following code on the On_Open event of the report. The report has no control source named in its Properties. Am I going about this incorrectly, because this opens the report with the correct data but does not sort the data?
...
May 28 '08 #8
NeoPa
32,568 Recognized Expert Moderator MVP
... Am I going about this incorrectly, because this opens the report with the correct data but does not sort the data?
...
You've hit the nail on the head there Martin. As Stewart implies, there is no benefit in having two separate queries which are simply sorted differently. The sort order of the underlying queries is completely overridden by that defined within the report.

Stewart has started you off in the right direction if you're interested in changing the sort order on the fly within the code. Let us know if you're interested in proceeding with this but need some more help with the details.
May 29 '08 #9
martin DH
114 New Member
Thanks, Stewart and NeoPa. As I was typing my post I began to think that I would have to programmaticall y change the sort/group order, not the record source. Unfortunately, when I search for help on the CreateGroupLeve l Method, Microsoft takes me to a "content not found" page.

I really have no experience here - any guidance would be much appreciated. Thank you.
martin
May 29 '08 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

7
4007
by: Phin | last post by:
I need your HELP! I've seen all the posts on using Crystal Reports within vs.net (vb.net) and changing a SQL query at runtime. When I tried to pass in a dataset into the crystal report at runtime, the report still showed the results from the default query (from within the Crystal Report). Then I tried the XSD solution where you define a dataset (that mataches the database and the Crystal Report) and have the Crystal Report use this....
3
3705
by: CSDunn | last post by:
Hello, I have a situation with MS Access 2000 in which I need to display report data in spreadsheet orientation (much like a datasheet view for a form). If you think of the report in terms of what a spreadsheet might show, the column names will actually be dynamic, based on data from a SQL Server 2000 database. The row data will also come from the same database. So in this case, I will have a main report and a subreport. I've already tried...
9
3066
by: Colin McGuire | last post by:
Hi, I have an report in Microsoft Access and it displays everything in the table. One column called "DECISION" in the table has either 1,2, or 3 in it. On my report it displays 1, 2, or 3. I want to appear in the report is Yes, No, or Maybe. What do I need to do to change what appears in the report/what term do I need to search out in Google? Thank you Colin
4
7817
by: Bob Sanderson | last post by:
I am using a form to select a table and then opening a report using the selected table as the record source. This is a simplified description of what I'm doing. First, cboSelectBOM selects the table from a list of tables in the database. Then, command button cmdPrintXref produces the following code: Private Sub cmdPrintXref_Click() ReportSource = cboSelectBOM DoCmd.OpenReport "rptPartNoXref", acViewPreview
6
6166
by: thomas.jacobs | last post by:
I have reports formated and now need to use that format on another table name with the same field names as formated in the report. I belive it is in the properties but I can't seem to find it. thanks
7
2414
by: manning_news | last post by:
I've got a report that's not sorting correctly. I build a SQL statement and assign it to the recordsource in the Open event, sorting the data the way the user chooses. The user can choose up to 3 sorts using a form that opens before the report opens. When the report previews, it ignores the users' sort request and always sorts by the primary key even though ORDER BY is explicity set to another field other than primary key. If I copy...
3
6744
by: Thad | last post by:
I'm new to C# and I was trying to create a Crystal Report. I've designed a simple report and I've used several methods for attaching fields to the report at design time. I've used a DataSet that I've created by add new item, and I've used the add command and used an actual SQL statement, and I've used the OLEDB ADO and connected to my database (Access) tables directly. I have code that will create a query, use that query with an Adapter...
6
5393
by: John | last post by:
Hi Is it possible to change the record source query of a report at runtime via code so the report can work with different data source queries? Thanks Regards
3
18700
by: creative1 | last post by:
Here is how you create a complex data report that involves parent and child commands and you can update information at runtime. Its pretty straight forward to work with simple queries; however, working with complex reports is tricky Assumption: Reader of this article have basic knowledge of creating data reports. Creating a Parent-Child Command and create a DataReport Suppose we have a database called company with two tables ...
0
8427
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
8850
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...
1
8523
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
8626
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5649
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
4175
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...
0
4334
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1975
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1737
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.