This may seem dumb, and I should probably know the answer, but I shall ask anyway. Is there a way for vb to call a a report using different queries? I know you can set filter, but I want to use a completely different data set based on user’s selection.
More detail:
I have two tables with same field names. I have one report with same field names. I can use different reports, I suppose, but that doesn’t seem very efficient. In the following example is there a creative way to use a different query for the same report, instead of different reports?
If Me.chkYes = True Then
stDocName = “rptShippingRpt”
DoCmd.OpenReport stDocName, acViewPreview, , , acWindowNormal
Else
stDocName = “rptShippingRpt2”
DoCmd.OpenReport stDocName, acViewPreview, , , acWindowNormal
End If
![]() |
Patch reliability is unclear. Unless you have an immediate, pressing need to install a specific patch, don't do it. |
SIGN IN | Not a member? | REGISTER | PLUS MEMBERSHIP |
-
One Report different Query at Run time (2k, XP, 03)
Home » Forums » AskWoody support » Productivity software by function » MS Access and database help » One Report different Query at Run time (2k, XP, 03)
- This topic has 11 replies, 5 voices, and was last updated 17 years, 11 months ago.
AuthorTopicWSkwvh
AskWoody LoungerDecember 1, 2006 at 12:05 pm #437686Viewing 3 reply threadsAuthorReplies-
WSHansV
AskWoody LoungerDecember 1, 2006 at 12:10 pm #1041367You can set the Record Source of the report in the On Open event of the report.
Private Sub Report_Open(Cancel As Integer)
If Forms!frmSomething!chkYes = True Then
Me.RecordSource = “ThisTable”
Else
Me.RecordSource = “ThatTable”
End If
End SubChange frmSomething to the name of your form; if it contains spaces or punctuation, put square brackets [ ] around the form name.
Replace ThisTable and ThatTable with the appropriate table or query names. -
WSkriss669
AskWoody LoungerDecember 4, 2006 at 5:47 pm #1041690An even easier approach I’ve found is to set the datasource of the report to a named, saved query and then take your user’s selections and pass parameters to the sql property of that query (or even just write on the fly sql for it). That way, you don’t have to change the recordsource of the report at runtime, which sometimes can be problematic, and you only have one querydef object to maintain:
dim db as dao.database, qdef as querydef
set db=currentdb
set qdef = db.querydefs(“yourqueryname”)
qdef.sql = “your sql string here”
db.close
set qdef=nothing
set db=nothing -
WShawksway
AskWoody LoungerJuly 5, 2007 at 6:16 pm #1070804Hmmm … let’s make this a little broader ! Hans, if the query is ‘parameterized’ with a question msgbox, for example [What store do you want ?] in the query criterion, why do I find bad recordsets (i’m counting records to post a count on the form) that compute Recordset.count = 1 … I tried the clone and it’s a mirror of the recordset.
-
WSHansV
AskWoody Lounger -
WShawksway
AskWoody LoungerJuly 5, 2007 at 7:06 pm #1070838I realize that, Hans, but for sake of brevity I skipped over alot of code
I have an open form, I have a user select a query name in a CBO, I take that name (a string) and “on update” for the CBO I want to count potential records, as if, that ‘parameterized’ query were actually run … I can place the query name in the form’s RecordSource, and do a requery .. but I get ‘iffy’ results … sometimes the correct count, else a “1” (that’s non-sense)… it’s flippin’ me out. Regards
-
-
-
WSjohnhutchison
AskWoody LoungerDecember 4, 2006 at 9:05 pm #1041722Yet another option (that is not available in 2K) is to pass the name of the query to the report using OpenArgs:
So on the form:
stDocName = “rptShippingRpt”
If Me.chkYes = True Then
strQueryname = “query1″
Else
strqueryname =” query2″
End If
DoCmd.OpenReport stDocName, acViewPreview, , , acWindowNormal, strquerynameThen in the onopen event of the report
Private Sub Report_Open(Cancel As Integer)
If not isnull(me.OpenArgs) Then
Me.RecordSource = me.openArgsEnd If
End SubThis is useful if you have more than pathway for opening the report.
-
WSkwvh
AskWoody Lounger
WSHansV
AskWoody Lounger-
WShawksway
AskWoody LoungerJuly 5, 2007 at 7:25 pm #1070867I hope you are appreciated … I went too MS, in the meantime, to see if I could dig out the fault mechanism … sure enough …”Calling the Requery method of a form’s recordset (for example, Forms(0).Recordset.Requery) can cause the form to become unbound. To refresh the data in a form bound to a recordset, set the RecordSource property of the form to itself (Forms(0).RecordSource = Forms(0).RecordSource).” … That’s their way of saying it’s “unstable” ! LOL But the second sentence is interesting … we learn something every day. Thanks Hans
-
WShawksway
AskWoody LoungerJuly 5, 2007 at 9:13 pm #1070901I changed the queries to “select” queries to get away from any “parameter’ issues .. I am unable to het a correct recordset count … one time it’s 598, the next zero, maybe even a ‘1’ … it’s all over the place. I’ve made sure to close the recordset between requests and set the rescordset object to nothing … there’s no consistency in this ??? Regards Wayne
-
WSHansV
AskWoody Lounger
-
-
Viewing 3 reply threads -

Plus Membership
Donations from Plus members keep this site going. You can identify the people who support AskWoody by the Plus badge on their avatars.
AskWoody Plus members not only get access to all of the contents of this site -- including Susan Bradley's frequently updated Patch Watch listing -- they also receive weekly AskWoody Plus Newsletters (formerly Windows Secrets Newsletter) and AskWoody Plus Alerts, emails when there are important breaking developments.
Get Plus!
Welcome to our unique respite from the madness.
It's easy to post questions about Windows 11, Windows 10, Win8.1, Win7, Surface, Office, or browse through our Forums. Post anonymously or register for greater privileges. Keep it civil, please: Decorous Lounge rules strictly enforced. Questions? Contact Customer Support.
Search Newsletters
Search Forums
View the Forum
Search for Topics
Recent Topics
-
Excessive security alerts
by
WSSebastian42
4 hours, 57 minutes ago -
* CrystalDiskMark may shorten SSD/USB Memory life
by
Alex5723
5 hours, 45 minutes ago -
Ben’s excellent adventure with Linux
by
Ben Myers
3 minutes ago -
Seconds are back in Windows 10!
by
Susan Bradley
5 minutes ago -
WebBrowserPassView — Take inventory of your stored passwords
by
Deanna McElveen
2 hours, 48 minutes ago -
OS news from WWDC 2025
by
Will Fastie
6 hours, 59 minutes ago -
Need help with graphics…
by
WSBatBytes
5 hours, 40 minutes ago -
AMD : Out of Bounds (OOB) read vulnerability in TPM 2.0 CVE-2025-2884
by
Alex5723
21 hours, 13 minutes ago -
Totally remove or disable BitLocker
by
CWBillow
20 hours, 6 minutes ago -
Windows 10 gets 6 years of ESU?
by
n0ads
8 hours, 2 minutes ago -
Apple, Google stores still offer China-based VPNs, report says
by
Nibbled To Death By Ducks
1 day, 7 hours ago -
Search Forums only bring up my posts?
by
Deo
1 day, 8 hours ago -
Windows Spotlight broken on Enterprise and Pro for Workstations?
by
steeviebops
1 day, 19 hours ago -
Denmark wants to dump Microsoft for Linux + LibreOffice
by
Alex5723
1 day, 12 hours ago -
How to get Microsoft Defender to honor Group Policy Setting
by
Ralph
1 day, 20 hours ago -
Apple : Paragon’s iOS Mercenary Spyware Finds Journalists Target
by
Alex5723
2 days, 6 hours ago -
Music : The Rose Room – It’s Been A Long, Long Time album
by
Alex5723
2 days, 7 hours ago -
Disengage Bitlocker
by
CWBillow
1 day, 21 hours ago -
Mac Mini M2 Service Program for No Power Issue
by
Alex5723
2 days, 9 hours ago -
New Win 11 Pro Geekom Setup questions
by
Deo
1 day, 8 hours ago -
Windows 11 Insider Preview build 26200.5651 released to DEV
by
joep517
2 days, 16 hours ago -
Windows 11 Insider Preview build 26120.4441 (24H2) released to BETA
by
joep517
2 days, 16 hours ago -
iOS 26,, MacOS 26 : Create your own AI chatbot
by
Alex5723
2 days, 20 hours ago -
New PC transfer program recommendations?
by
DaveBoston
1 day, 1 hour ago -
Windows 11 Insider Preview Build 22631.5545 (23H2) released to Release Preview
by
joep517
3 days ago -
Windows 10 Build 19045.6029 (22H2) to Release Preview Channel
by
joep517
3 days ago -
Best tools for upgrading a Windows 10 to an 11
by
Susan Bradley
2 days, 13 hours ago -
The end of Windows 10 is approaching, consider Linux and LibreOffice
by
Alex5723
1 day, 17 hours ago -
Extended Windows Built-in Disk Cleanup Utility
by
bbearren
2 days, 2 hours ago -
Win 11 24H2 June 2025 Update breaks WIFI
by
dportenlanger
3 days, 19 hours ago
Recent blog posts
- Ben’s excellent adventure with Linux
- Seconds are back in Windows 10!
- WebBrowserPassView — Take inventory of your stored passwords
- OS news from WWDC 2025
- Best tools for upgrading a Windows 10 to an 11
- Master patch listing for June 10, 2025
- 24H2 may not be offered June updates
- June 2025 updates are out
Key Links
Want to Advertise in the free newsletter? How about a gift subscription in honor of a birthday? Send an email to sb@askwoody.com to ask how.
Mastodon profile for DefConPatch
Mastodon profile for AskWoody
Home • About • FAQ • Posts & Privacy • Forums • My Account
Register • Free Newsletter • Plus Membership • Gift Certificates • MS-DEFCON Alerts
Copyright ©2004-2025 by AskWoody Tech LLC. All Rights Reserved.