473,725 Members | 2,173 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using Listbox contents as query parameter

I have an extended multi-select list box of names in which the bound column
is a person ID field called ID (text, not numerical).

I want that list to act as a parameter in a query that is simply going to
produce a medical report for each person in the list box. Now, I know this
should be easy. My query for the medical report only has 4 fields, ID,
Firstname, Lastname, Medical, but I cant seem to use the listbox of names to
act as the ID parameter for the query.

I have read the item on Dev Ashish's site and am still in the dark (I really
believe I am quite dumb when I read some of the articles and techniques used
in Access L)

The Listbox is called lboExcID.

Can anyone please help me get this to work.

dixie
Nov 13 '05 #1
4 4738
> I want that list to act as a parameter in a query that is simply going to
produce a medical report for each person in the list box. Now, I know this
should be easy. My query for the medical report only has 4 fields, ID,
Firstname, Lastname, Medical, but I cant seem to use the listbox of names to
act as the ID parameter for the query.

Sounds like you're making this harder than it should be. Just use the
code to create the value list, for example, 'A' OR 'B' OR 'C', and
then just pass that string as the Where clause in the OpenReport
command.

Option Explicit

Private Sub Command2_Click( )
'************** ****** Code Start *************** *********
Dim frm As Form, ctl As Control
Dim varItem As Variant

'---this will be the filter string that I'll open the report with
Dim strFilter As String

Set frm = Me
Set ctl = frm!List0
strFilter = "[SerialNo]='"
'Assuming long [EmpID] is the bound field in lb
'enumerate selected items and
'concatenate to strSQL
For Each varItem In ctl.ItemsSelect ed
strFilter = strFilter & ctl.ItemData(va rItem) & "' OR
[SerialNo]='"
Next varItem

'Trim the end of strSQL
strFilter = Left$(strFilter , Len(strFilter) - 16)
'************** ****** Code end *************** *********

DoCmd.OpenRepor t "rptCompute rs", acViewPreview, , strFilter
Nov 13 '05 #2
Pieter, I tried to run this code and I get an Error 5, Invalid Procedure
call or argument at the line:

strFilter = Left$(strFilter , Len(strFilter) - 16)

The changes I have made to your original code are as follows:
I have substituted my Listbox name in the Set ctl line and also ID for
SerialNo

Mine now looks like this:

Private Sub MedicalReport_C lick()
Dim frm As Form, ctl As Control
Dim varItem As Variant

'---this will be the filter string that I'll open the report with
Dim strFilter As String

Set frm = Me
Set ctl = frm!lboBulkComm end
strFilter = "[ID]='"
'Assuming long [EmpID] is the bound field in lb
'enumerate selected items and
'concatenate to strSQL
For Each varItem In ctl.ItemsSelect ed
strFilter = strFilter & ctl.ItemData(va rItem) & "' OR [ID]='"
Next varItem

'Trim the end of strSQL
strFilter = Left$(strFilter , Len(strFilter) - 16)

DoCmd.OpenRepor t "rptMedicalInfo rmation", acViewPreview, , strFilter

End Sub

Also, could you explain what you are trimming from the strFilter and why the
number 16?

dixie
Private Sub Command2_Click( )
'************** ****** Code Start *************** *********
Dim frm As Form, ctl As Control
Dim varItem As Variant

'---this will be the filter string that I'll open the report with
Dim strFilter As String

Set frm = Me
Set ctl = frm!List0
strFilter = "[SerialNo]='"
'Assuming long [EmpID] is the bound field in lb
'enumerate selected items and
'concatenate to strSQL
For Each varItem In ctl.ItemsSelect ed
strFilter = strFilter & ctl.ItemData(va rItem) & "' OR
[SerialNo]='"
Next varItem

'Trim the end of strSQL
strFilter = Left$(strFilter , Len(strFilter) - 16)
'************** ****** Code end *************** *********

DoCmd.OpenRepor t "rptCompute rs", acViewPreview, , strFilter

Nov 13 '05 #3
Pieter, ignore that last message. I didn't actually have any Items selected
in the list box when I ran the code, but now, when I select just the first
person, I get Error 3075 - Syntax error in string in query expression
'([ID]='0)'. If I select 2 people, that error then reads '([ID]='030154' OR
[ID]='0)'.
030154 is the ID of the first person in the items selected, but there is no
ID for the 2nd person, just the 0. As I select more people, it continues
like that, always missing the ID of the last person.
Note my ID field, although made up of numbers is actually a text field.
The error is occurring in the last line of the code which is the opening of
the report with the strFilter.

dixie

"dixie" <di****@dogmail .com> wrote in message
news:SL******** *********@nnrp1 .ozemail.com.au ...
Pieter, I tried to run this code and I get an Error 5, Invalid Procedure
call or argument at the line:

strFilter = Left$(strFilter , Len(strFilter) - 16)

The changes I have made to your original code are as follows:
I have substituted my Listbox name in the Set ctl line and also ID for
SerialNo

Mine now looks like this:

Private Sub MedicalReport_C lick()
Dim frm As Form, ctl As Control
Dim varItem As Variant

'---this will be the filter string that I'll open the report with
Dim strFilter As String

Set frm = Me
Set ctl = frm!lboBulkComm end
strFilter = "[ID]='"
'Assuming long [EmpID] is the bound field in lb
'enumerate selected items and
'concatenate to strSQL
For Each varItem In ctl.ItemsSelect ed
strFilter = strFilter & ctl.ItemData(va rItem) & "' OR [ID]='"
Next varItem

'Trim the end of strSQL
strFilter = Left$(strFilter , Len(strFilter) - 16)

DoCmd.OpenRepor t "rptMedicalInfo rmation", acViewPreview, , strFilter

End Sub

Also, could you explain what you are trimming from the strFilter and why the number 16?

dixie

Nov 13 '05 #4
I've been experimenting with the number at the end of the line
strFilter = Left$(strFilter , Len(strFilter) - 16)
and have found that if I change the number 16 to a 10, I can get through the
errors and get a report opening. Can you explain to me what this number is
actually doing and how you arrive at the correct value for it?

dixie

Nov 13 '05 #5

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

Similar topics

9
7026
by: Megan | last post by:
Hi- I'm creating a database of music bands with their cds and songs. I'm trying to program an SQL statement so that I can enter a string of text in a textbox, press the 'Enter' key, and have it return the associated records to a listbox. Once the listbox has the records, I want to select a record, which will open a form associated with the selected record in the listbox.
3
1522
by: Magno101 | last post by:
Hi all, First let me start out with what I want to happen with this and then maybe y'all can give me some better insight. I have a database in Microsoft Access which we need to upsize to SQL. We currently have a problem with it now. On one of our forms (frmScan, which is an unbound form with two unbound controls) there is a txtBox contorl (txtScan) which gets a number from a barcode put into it (usually a 9 character varchar). I have...
6
7561
by: Deano | last post by:
I needed to have a listbox populated by locations which are stored in tblLocations. However I wanted an "All locations" entry to be at the top of the listbox. This is not in the tblLocations. The resulting list will be used to provide a query parameter and the user can choose all locations (the default) or a single, specific location. In the open event of the form containing the listbox I wrote the following code to achieve this. Could...
3
1502
by: msnews.microsoft.com | last post by:
Hello All, I am trying to write Web Controls and in most of the samples I came across, I am seeing the following function where a HTML string is written to create HTML Controls. /// <summary> /// Render this control to the output parameter specified.
1
3300
by: JNariss | last post by:
Hello, I have created a form called frmS2P with the following: 1 listbox called List11 which holds the contents of a query created off my table called tblRequestActions. The fields which the listbox holds are Request_ID and MoveNumber. 1 text box called Date which is populated with the default value of =Date( ).
2
2469
by: ChasW | last post by:
I just read these 2 pages. These are most helpful, but leave me with a question. http://www.mvps.org/access/queries/qry0005.htm http://www.mvps.org/access/forms/frm0007.htm I have a Multi Select Listbox that will define query criteria for a query that is the record source for another form. Certain individual selections in this list box may act a label for a
3
2668
by: ML | last post by:
I have used Allen Brown's technique for filling a listbox on a form with the names of files in a certain disc folder. It works well. I am now giving the user the option to print the form contents in a report which has the same fields as the form. All fields derived from report query are OK A listbox with design mode rowsource setting, works fine BUT...
2
2029
by: DanWeaver | last post by:
I am assigning a profile variable based on the conents of a text box when a button is pressed- this is then used as a search term to populate a list box- all controls are in same ajax update panel- how can I update the profile variable before the listbox gets populated - ie assign the variable prior to the sql call (as the sql query should use the variable). I am trying to do this using the VS bind configuration wizard with a few...
20
5243
by: exipnakias | last post by:
Hello Guys. In a form I created a listbox which looks up the values of a table. I want: 1) ..to create a query where a parameter will be needed in order to be loaded. But I do not want to write the parameter, I want to select a row from the listbox and after clicking in a button, the query to automatically take the current value of listbox as the needed parameter and then to load the query. (The Inputbox for "Enter Parameter" will...
0
8888
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
9401
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
9176
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
9113
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
6011
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
4519
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
3221
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
2
2635
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2157
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.