473,405 Members | 2,210 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

How do you build a query or report w/ list of parameters, separated by commas.

269 256MB
This is a two part question... I'm after one solution but I've thought of two possible ways to make this work.

I want to have a rptCustomer_Inquiry_Report that will list customer information for specific, user-defined customers. They will be identified by their Customer_Number. For example, I want to be prompted to Enter Customer Numbers and then enter "10628, 10654, 11369". When I make a query and set the criteria to the parameter [Enter Customer Numbers], if I enter more than one parameter I get zero results.

One idea is to get a query working. I would just need to know how to allow the user to enter the multiple values and have the database actually give me the desired results. What kind of query would this require?

Another idea is to use a parameter in the actual report. I'm less confident this is possible and/or straightforward. I haven't found too much info on specifying a parameter in a report, but I may find this useful in other situations.

Please advise or offer opinions. I am still very new at Access so don't assume I know too much. :-)
Thanks so much.
Oct 21 '09 #1

✓ answered by NeoPa

ADezii's code should work (as I mentioned earlier) but I would do it slightly differently (This works under the same basic assumptions though. Names etc.) :
Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdReport_Click()
  2.     Dim strSQL As String
  3.  
  4.     If IsNull(Me.txtCustomers) Then Exit Sub
  5.  
  6.     strSQL = Replace(Trim(Me.txtCustomers) , " ,", ",")
  7.     strSQL = Replace(strSQL, ", ", ",")
  8.     strSQL = Replace(strSQL, ",", "','")
  9.     strSQL = Replace("[CustomerID] IN('%S')", "%S", strSQL)
  10.  
  11.     Call DoCmd.OpenReport(ReportName:="rptCustomers", _
  12.                           View:=acViewPreview, _
  13.                           WhereCondition:=strSQL)
  14. End Sub
This also handles errors where the operator puts unrequired spaces around any of their commas.

30 2816
ADezii
8,834 Expert 8TB
Opening a Report that has a Data Source which is based on a Single or Variable number of Parameters can be a little tricky. I created a Code template that will do just that. The User will enter either a single or multiple Comma Separated Customer IDs into a Text Box named txtCustomers. The code will parse this String, then generate an SQL Statement to be used in the WHERE Clause of the OpenReport Method. Substitute your values as required and should you have any questions, feel free to ask.
Expand|Select|Wrap|Line Numbers
  1. Dim varCustomerIDs As Variant
  2. Dim intCounter As Integer
  3. Dim strSQL As String
  4.  
  5. If IsNull(Me![txtCustomers]) Then Exit Sub
  6.  
  7. varCustomerIDs = Split(Me![txtCustomers], ",")
  8.  
  9. For intCounter = LBound(varCustomerIDs) To UBound(varCustomerIDs)
  10.   strSQL = strSQL & "'" & Trim(varCustomerIDs(intCounter)) & "',"
  11. Next
  12.  
  13. strSQL = "[CustomerID] IN(" & Left$(strSQL, Len(strSQL) - 1) & ")"
  14.  
  15.  
  16. DoCmd.OpenReport "rptCustomers", acViewPreview, , strSQL
Oct 21 '09 #2
DanicaDear
269 256MB
I have attached my DB. I have had some pretty intelligent help from BYTES so anything you see pretty handy...probably was my idea paired up with someone else's hints and code.

My problem with answer's such as yours is that I can never figure out where to put the code. I'm getting better with things..my speed is definetely improving...but I'm still a little slow. (I'm only 3 weeks into Access.)

So, where would I put this code you refer to...and how do I call up the text box to enter the parameters? So far my parameter text boxes have been called up by MS Access where I have just entered in the parameter name and type. I think I've only entered code from forms and reports so I'm thinking this code has to attach to the properties box in the report. Specific instructions are appreciated.

If you want to look at my DB, the report I'm trying to get this parameter to run is named rptHOTSTICK_CUSTOMER_INQUIRY. The query the customer number and customer info is based from is named HOTSTICK_CUSTOMERS Query. Thanks so much for your help and time.
Attached Files
File Type: zip SHOPS102009.zip (146.9 KB, 147 views)
Oct 21 '09 #3
NeoPa
32,556 Expert Mod 16PB
Danica,

This code should work for you and it would be in the Click event procedure of the Command Button that you want to trigger running the report.

If you post the procedure where you currently run the report, I'm sure either of us can tell you how to incorporate this code so that it works for you.

PS. Terminology can be very important in Access. Fields aren't controls. Understanding the terminology can be very helpful to you in understanding what we are saying (and vice-versa of course). If there's ever anything you're not sure of simply ask and we'll be pleased to enlighten.
Oct 21 '09 #4
NeoPa
32,556 Expert Mod 16PB
ADezii's code should work (as I mentioned earlier) but I would do it slightly differently (This works under the same basic assumptions though. Names etc.) :
Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdReport_Click()
  2.     Dim strSQL As String
  3.  
  4.     If IsNull(Me.txtCustomers) Then Exit Sub
  5.  
  6.     strSQL = Replace(Trim(Me.txtCustomers) , " ,", ",")
  7.     strSQL = Replace(strSQL, ", ", ",")
  8.     strSQL = Replace(strSQL, ",", "','")
  9.     strSQL = Replace("[CustomerID] IN('%S')", "%S", strSQL)
  10.  
  11.     Call DoCmd.OpenReport(ReportName:="rptCustomers", _
  12.                           View:=acViewPreview, _
  13.                           WhereCondition:=strSQL)
  14. End Sub
This also handles errors where the operator puts unrequired spaces around any of their commas.
Oct 21 '09 #5
ADezii
8,834 Expert 8TB
Danica, I'm sending your Database back to you with the following changes:
  1. The Database opens with a Form being displayed.
  2. This Opening Form contains a Text Box with Sample Customer Numbers pre-entered. These are your Comma-Delimiter Parameters.
  3. There are also 2 Command Buttons on this Form. These Command Buttons contain both my code and NeoPa's in the Button's Click() Event Procedure. There is no Door #3! (LOL)!
  4. View the code in each Click() Event to see its implementation.
  5. Simple code was placed in the NoData() Event of the Report. Should no Records be returned for a specific Parameter or set of Parameters, a Message Box will be displayed indicating same and the Open() Event of the Report will be cancelled.
  6. Have fun with this, and should you have any further questions, either NeoPa or myself will be glad to assist.
  7. Hope you don't mind me speaking for you, NeoPa.
Attached Files
File Type: zip SHOPS102009.zip (188.8 KB, 96 views)
Oct 21 '09 #6
NeoPa
32,556 Expert Mod 16PB
Absolutely not my friend. You always do such a thorough job. You go more extra miles than anyone I know.

BTW Do me a favour would you. Tell me whether you see the link to Choose as best answer against every post bar the original in this thread?

I expect Danica does, as she's the OP. I know I do, as I can see them. Do you?
Oct 21 '09 #7
ADezii
8,834 Expert 8TB
Yes NeoPa, I do see the Link.
Oct 22 '09 #8
NeoPa
32,556 Expert Mod 16PB
Thanks. That's a liitle weird though. I'll have to chat with KUB about this sometime soon. Cheers.
Oct 22 '09 #9
DanicaDear
269 256MB
Believe it or not....I got this working! (I hope you're proud of me NeoPa!!) :-) ADezii, thank you for putting the extra time to show me exactly how that worked. I was able to figure it all out, but I did use NeoPa's code. She has helped me a LOT lately and so I just used her code to be consistent. LOL.

One question, when doing a report, I notice when the report is based off a couple different queries, if one customer doesn't have data in a particular query they will not show up in the report. For example, customer number 10654 is enetered into the form, it says no records available. This is because they have no orders yet in the table HOTSTICK_ORDERS. However, we can get customer info before they have an order...and we might still want to view the customer in this report. I saw this happen in another report when I didn't have an account number put in...it just left the customer off the report. How can I prevent this?
Oct 22 '09 #10
ADezii
8,834 Expert 8TB
@DanicaDear
You can accomplish this by changing the Join Properties of the Tables involved in the underlying Record Source for your Report. Now, all Customers will be included regardless of whether or not they have actually placed an Order. Download the New Attachment. and view the Join Properties of the Tables involved in the Query for the Report. If you are not sure as to how to do this, let us know.
Attached Files
File Type: zip SHOPS102209.zip (174.3 KB, 139 views)
Oct 22 '09 #11
DanicaDear
269 256MB
Well, flip!! I tried so hard to figure this out without having to come back and ask.... I am viewing my relationships and join properties between tables and other queries in query design view. However, I don't see any changes from the DB you uploaded and my original. I know *you* did it right because it's working in your report. Can you be more specific about what I should look at? I think this is worth my effort to learn. It will occur in many more reports than just this one.
Thanks, Danica PS. I will be gone until Monday so please stick around til then. LOL. ;-)
Oct 22 '09 #12
ADezii
8,834 Expert 8TB
@DanicaDear
  1. Single, Left-Click on the Join Line between Tables in your Query - the Line will now be thicker and Bold (it is selected).
  2. Right-Click on the Join Line to Open the Join Properties Dialog Box.
  3. The answer to your Question should now be obvious.
Oct 22 '09 #13
NeoPa
32,556 Expert Mod 16PB
@DanicaDear
I am proud of you Danica. You've come on leaps and bounds, and I like working on your questions generally as your attitude is so healthy.

I'm not entirely sure why I gave the impression of being a NeoMa - but possibly you didn't pick up on the reference in the name ;) I'll take it as a compliment though. I'm sure it was so intended :)
@DanicaDear
Check out SQL JOINs.
Oct 22 '09 #14
DanicaDear
269 256MB
It's a sad Monday morning here in Alabama. I STILL cannot figure out what you did on the join properties. At this point I'm going to be very specific. I opened qryCUSTOMER_INQUIRY. In design view, I clicked SHOW TABLES and chose HOTSTICK_ORDERS and HOTSTICK_ORDER_DETAILS. I looked at the join properties like you told me to. (I was in fact doing it the same way last week.) What I am unable to find is what you changed. You said it should be obvious to me so I'm thinking I am either in the wrong query or looking at the wrong table relationships. All of the ones I see in your modified DB have the same settings as mine...yet I know you did something magical because when you run YOUR report the desired results appear.

(Recall, start the report from frmPassCustomerNumbersToReport, customer 10654 is the magic parameter.)

This time, I have attached my real unmodified DB. I haven't been just taking the DBs you attach, I have been doing the exercises in my own DB (to learn how). Could you please help me find what I need to do to make this work? I know you have given, but I have not received. LOL. ;-)

(As a side question, if I hadn't named my reports to coincide with my queries, I would have trouble figuring out which query a report was based on. How could you determine it otherwise?)
Attached Files
File Type: zip SHOPS102209.zip (2.19 MB, 104 views)
Oct 26 '09 #15
ADezii
8,834 Expert 8TB
@NeoPa
Sorry NePa, but I also thought that you were a she! (LOL)!
Oct 26 '09 #16
ADezii
8,834 Expert 8TB
View the Attachment, Danica, and all should be clear.
Attached Files
File Type: zip Dump.zip (14.6 KB, 107 views)
Oct 26 '09 #17
NeoPa
32,556 Expert Mod 16PB
@DanicaDear
Although relationship definitions can help Access to determine the default joins between two related tables, the actual joins themselves are entirely under your control within the QueryDef definition. If none already exists, you can drag a field from one table and drop it on the related field in the other to join two fields. This can be done with up to ten field pairs.

When two tables are joined in a QueryDef they will have lines connecting each of the fields to be linked in one table, with each of the fields to be linked in the other. Each of these connecting lines should reflect the same type of join or Access will get shirty with you.

When you double-click on any of the lines you will see the Join Properties box. This shows the tables and the fields involved. The left indicates the table and field that you dragged from, and the right indicates those that you dropped on (if you did it that way).

Below that are three Join Type options. For Access to ignore any result sets where the same data doesn't exist in both input tables select a Type 1. If you want either of the tables to show their data regardless of whether there is matching data in the other, then select either Type 2 or Type 3 depending on which you require. See SQL JOINs for more help on this point.
@DanicaDear
Using Polish Notation (I think it's called) each object has a 3-letter code which identifies the type of object it is.
Expand|Select|Wrap|Line Numbers
  1. tblName ==> Table
  2. qryName ==> Query
  3. frmName ==> Form
  4. rptName ==> Report
  5. modName ==> Module
Oct 26 '09 #18
NeoPa
32,556 Expert Mod 16PB
@ADezii
Don't give me that!! We spoke on the phone once some while ago. I may not be Barry White, but I doubt you'd have mistaken me for a wo-man.
Oct 26 '09 #19
ADezii
8,834 Expert 8TB
@NeoPa
Gotcha, didn't I? (LOL)!
Oct 26 '09 #20
DanicaDear
269 256MB
@ADezii
It's because NeoPa is so nice and patient..... All this has cracked me up.

I hope you have not been offended that I thought you were a she. I will admit I have been reading your posts in a female voice with a British accent... LOL.

What does NeoPa mean, anyway? :-)
Oct 26 '09 #21
DanicaDear
269 256MB
@NeoPa

I think you may have misunderstood my question here....allow me to try again. I used qryCUSTOMER_INQUIRY to create rptCUSTOMER_INQUIRY. Forget about the Polish (or Hungarian?) notation....if I didn't name them both CUSTOMER_INQUIRY...then how would I know which query a report was based from? (Say in case I needed to add a control/field to the query to then add to the report. Is there another intelligent way to tell?)
Oct 26 '09 #22
ChipR
1,287 Expert 1GB
I believe you would have to check the data source defined in the report's properties or code.
Oct 26 '09 #23
ajalwaysus
266 Expert 100+
@DanicaDear
This is the greatest post I have ever read! Can't stop Laughing. LOL!!!

Sorry, absolutely no offense intended. =)
-AJ

P.S. I'm a "He" just as an FYI.
Oct 26 '09 #24
DanicaDear
269 256MB
ADezii,
I have looked at that at least 20 times. If it makes any difference, you explained this in a receivable way the very first time. Thank you for the attachment. I am attaching my own word document which may explain why I have been so confused. The DB that works correctly, the one you sent me, doesn't have a join line between HOTSTICK_CUSTOMERS Query and HOTSTICK_ORDERS Query, the same two queries shown connected in the WORD file you sent me. I was definetely expecting a radio button in the 2 or 3 position clicked (not the 1st position), but I could never find one. Then I thought, well if that's how it's supposed to be, I'll just do it like that in my own. But then that didn't explain how your report was working properly. Take a look at my attached file and please tell me if I'm making any sense.
I'm sorry for dragging out a simple issue here but I really do want to understand what I'm not doing correctly. Thanks!
Oct 26 '09 #25
DanicaDear
269 256MB
Oops...I don't think it attached last time.
Danica
Attached Files
File Type: zip Joins.zip (171.2 KB, 138 views)
Oct 26 '09 #26
NeoPa
32,556 Expert Mod 16PB
@DanicaDear
Not in the least. I felt sure you meant it as a compliment anyway and it brought a big grin to my face.

I certainly do have a British accent (as you would say), but probably down an octave or two from my female countryfolk ;)
@DanicaDear
NeoPa comes from my gaming days. When I joined a server that my son (who called himself N30 (Neo)) was playing on I used this to indicate I was his dad (Pa). That way I got a bit of slack until I'd warmed up a little. N30 was always very good at games (well - I used to beat him regularly until he started getting pubertised) so people put up with me if that meant they got to play with N30.
@ADezii
Darn it! I just put it down to a senior moment there. Forgetting who you were talking to and all that nonsense :D
Oct 26 '09 #27
NeoPa
32,556 Expert Mod 16PB
@ajalwaysus
s l a p !!!
Oct 26 '09 #28
ADezii
8,834 Expert 8TB
@DanicaDear
Simply stated, the Data Source for your Report cannot consist of three interrelated Tables and produce the results you are requesting. The Customers and Orders Tables must comprise a single Query with ALL Records from the Customer side (1). This Query can now be joined to the Details Table in another Query in order to produce the desired results. If you do not construct the Data Source for your Report this way and still try to include ALL Records from Customers, you will get an Ambiguous Outer Join Error. The Attachment indicates how it should be done.
Attached Files
File Type: zip SHOPS102609.zip (378.1 KB, 91 views)
Oct 26 '09 #29
DanicaDear
269 256MB
WOOOHOOOO!! I have found it now!!!!!!!!!!!!!!! Thanks so much for your patience and continued support. I will try to make this work for me now. :-)
Oct 26 '09 #30
DanicaDear
269 256MB
I am so embarrased. That was the easiest thing I've ever done so far. Sometimes just finding out where to look is 99% of the problem. I was looking in the queries themselves. Thanks to you and a hint from ChipR, I found the correct place in the property of the report itself. Thanks again. This problem is SOLVED! :-)
Oct 26 '09 #31

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

Similar topics

15
by: AK | last post by:
Once upon a time there was a table: CREATE TABLE VENDOR(VENDOR_ID INT, NAME VARCHAR(50), STATE CHAR(2))@ in a while the developers realized that a vendor may be present in xseveral states, so...
1
by: longtim | last post by:
I have been having endless difficulty creating reports/queries that set any relevent parameters from controls in forms. I am creating an application under access 2003 but will target access...
1
by: krish_4u_only | last post by:
Hi, I have designed a subreport - record source is on crosstab query. crosstab query is designed on union query. The sub report is working fine when it is opened individually. but if i place it on...
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...
2
by: Craig B. | last post by:
I am relativly new to access 2000 and am having some trouble with a report. I am not sure what I want to do is something I can do in access. I want to be able to choose from a combo box multiple...
9
by: Mark | last post by:
I've run a few simple tests looking at how query string encoding/decoding gets handled in asp.net, and it seems like the situation is even messier than it was in asp... Can't say I think much of the...
2
by: Zlatko Matić | last post by:
Hello. How to reference selected values from a multi-select list box, as a criteria in a query ? Is it possible at all? Regards, Zlatko
4
by: douglaswade | last post by:
I am new to Access, but I find this answer a little difficult to understand. I have an access query that list late work, I want to run the query and let say the results is ten records. I want one...
3
by: nvrivers | last post by:
I am trying to figure out how to do a query in Access 2000 that will find all records that contain the number 1. The problem is that the particular field I am searching has lists of numbers in it...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
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
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
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...

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.