473,469 Members | 1,595 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

list box and parameters

rcollins
234 New Member
I need my List box to carry over selected items to a report. Here is what I have
Expand|Select|Wrap|Line Numbers
  1. ([Forms]![CommentCriteria]![List2])
but this doesnt work. In fact with this, I get an empty report. Need help please
Jun 23 '09 #1

✓ answered by NeoPa

Sometimes, a bit of code can paint a thousand words. From this at least I can follow where you need a little more direction.

DoCmd.OpenReport has a parameter that you haven't used, called WhereCondition (not to be confused with FilterName). This is the fourth parameter. Parameters can also be passed by name if preferred :
Expand|Select|Wrap|Line Numbers
  1. Call DoCmd.OpenReport(ReportName:=stDocName, _
  2.                       View:=acPreview, _
  3.                       WhereCondition:=strWhere)
Before the call is made you need to translate the various items in your ListBox, using ItemsSelected (in a loop) to produce a string formatted like a SQL WHERE clause but without the "WHERE " part.
Expand|Select|Wrap|Line Numbers
  1. Dim strWhere As String
  2. Dim varX As Variant
  3.  
  4. With Me.List0
  5.     For Each varX In .ItemsSelected
  6.         strWhere = strWhere & "," & .ItemData(varX)
  7.     Next varX
  8.     strWhere = "[SurveyCode] In(" & Mid(strWhere,2) & ")"
  9.     Call DoCmd.OpenReport(ReportName:=stDocName, _
  10.                           View:=acPreview, _
  11.                           WhereCondition:=strWhere)
  12. End With
From your posted SQL, your problem is that ItemsSelected is not a string value so would be entirely unusable in SQL directly. My earlier code illustrates how a SQL string should be set up as it works on very similar lines. Also, as the SQL that the report is built on is encapsulated within the report object itself, any change to the SQL would have to be entered as the property .RecordSource of the report.

6 2788
NeoPa
32,556 Recognized Expert Moderator MVP
There isn't really much of a question here to work with. It would help a lot if you expressed more clearly what you're attempting and what you hope to achieve.

I will take a stab that you have a Multi-Select ListBox control on a form and you want only those records associated with the selected items to be included in some report or other.

With so little info to work with I can only suggest a solution in very general terms of course :
Use VBA to build up a filter (SQL WHERE clause without the word WHERE) of only the selected items. Use the .ItemsSelected property of the ListBox control to identify the required items.

When you have prepared the filter string pass it as the WhereCondition parameter to the OpenReport() function you use to open the report.
Jun 26 '09 #2
rcollins
234 New Member
ok, I think i am a little confused. I thought I could put something in the query when the report opened that would look at what I selected from the list box. This is how I do my combobox. I have List0 for the list of items (which is already set as multple select) and the frield I am using in the report OPComments is SurveyCode. So my understanding from you is I would put it before:
Expand|Select|Wrap|Line Numbers
  1.     stDocName = "OPComments"
  2.     DoCmd.OpenReport stDocName, acPreview
  3.  
Here is what I am trying and It doesnt work. Do I have some syntax wrong?
Expand|Select|Wrap|Line Numbers
  1. select OPComments.SurveyCode where OPComments.SurveyCode = List0.ItemsSelected
Jun 26 '09 #3
NeoPa
32,556 Recognized Expert Moderator MVP
@rcollins
Let me start by saying that ignoring my two comments about :
  1. Using the CODE tags.
  2. Posting in terms that make sense.
is not a great way to proceed.

As a long time member I'm trying not to slap an infraction onto your account, but you make this harder than it should be. When responding to a thread I prefer not to have to read up the whole thread again to work out what you're talking about.

What am I supposed to make of that (quoted)? Which query? Which report?

I will look at getting some answers for you, but please be more careful in future as this is frankly irritating (and totally unnecessary).
Jun 28 '09 #4
NeoPa
32,556 Recognized Expert Moderator MVP
Sometimes, a bit of code can paint a thousand words. From this at least I can follow where you need a little more direction.

DoCmd.OpenReport has a parameter that you haven't used, called WhereCondition (not to be confused with FilterName). This is the fourth parameter. Parameters can also be passed by name if preferred :
Expand|Select|Wrap|Line Numbers
  1. Call DoCmd.OpenReport(ReportName:=stDocName, _
  2.                       View:=acPreview, _
  3.                       WhereCondition:=strWhere)
Before the call is made you need to translate the various items in your ListBox, using ItemsSelected (in a loop) to produce a string formatted like a SQL WHERE clause but without the "WHERE " part.
Expand|Select|Wrap|Line Numbers
  1. Dim strWhere As String
  2. Dim varX As Variant
  3.  
  4. With Me.List0
  5.     For Each varX In .ItemsSelected
  6.         strWhere = strWhere & "," & .ItemData(varX)
  7.     Next varX
  8.     strWhere = "[SurveyCode] In(" & Mid(strWhere,2) & ")"
  9.     Call DoCmd.OpenReport(ReportName:=stDocName, _
  10.                           View:=acPreview, _
  11.                           WhereCondition:=strWhere)
  12. End With
From your posted SQL, your problem is that ItemsSelected is not a string value so would be entirely unusable in SQL directly. My earlier code illustrates how a SQL string should be set up as it works on very similar lines. Also, as the SQL that the report is built on is encapsulated within the report object itself, any change to the SQL would have to be entered as the property .RecordSource of the report.
Jun 28 '09 #5
NeoPa
32,556 Recognized Expert Moderator MVP
As a last comment, that code assumes the [SurveyCode] field is numeric (I had to guess as this fundamental information was not available). If it turns out that this is a textual field instead, then simply replace line #6 in the posted code with :
Expand|Select|Wrap|Line Numbers
  1. strWhere = strWhere & ",'" & .ItemData(varX) & "'"
Notice the only difference here is the single-quotes around the values. This tells the SQL engine to treat the contents as a textual value rather than a numeric or date/time one.
Jun 28 '09 #6
NeoPa
32,556 Recognized Expert Moderator MVP
A new (related) question was posted in here which has been split off as ListBox Problem.
Sep 10 '09 #7

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

Similar topics

2
by: Jeff Thur | last post by:
I am running a SQL Stored Procedure that will give the user a count of how many records are in the database as per certain criteria. I'm using the Execute Scalar Method. I have no problem passing...
7
by: ritchie | last post by:
Hi, I am writing to ask for some info with a program I am trying out. I am teaching myself linked lists in C. I have a linked list (see code below), and I accept user input which is the...
4
by: JH | last post by:
Fairly new to c# so please forgive any simple oversights. I'm developing a console app that loads dll assemblies at run time. The app scans a "plugin" directory for these dlls. Each dll is loaded...
5
by: Vigneshwar Pilli via DotNetMonster.com | last post by:
string connectionString1 = "server=(local); user=sa;password=sa; database=sonic"; System.Data.SqlClient.SqlConnection dbConnection1 = new System.Data.SqlClient.SqlConnection(connectionString1);...
5
by: tshad | last post by:
I can't seem to find where to reset the parameter list. Dim objCmd as New SqlCommand(CommandText,objConn) with objCmd.Parameters .Add("@StateCode",SqlDbType.Char,2).value = ByState.SelectedValue...
2
by: R2d2Rabeau | last post by:
Hi, How can I pass values from a List as stored procedure parameters? name= "Smith", age = 21 name="Brown", age =46 name="White", age=28 ... // data added to the List
0
by: Atos | last post by:
SINGLE-LINKED LIST Let's start with the simplest kind of linked list : the single-linked list which only has one link per node. That node except from the data it contains, which might be...
0
AmberJain
by: AmberJain | last post by:
Windows Autorun FAQs: List of autostart locations Linked from the Original article- "Windows Autorun FAQs: Description". Que: Can you list all the autostart locations for windows? Ans: Here is...
3
by: kirschey | last post by:
I have read the other posts on this subject, but I am still having issues. I have a few combo, and one multiselect list box for States delimiting. I am trying to feed a query based on this info. ...
3
by: semomaniz | last post by:
I have a ajax reorderlist that displays the contents with out any problems but when i drag and reorder the list nothing happens, i get a postback and the same initial list is displayed, seems like my...
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
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
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.