473,386 Members | 1,798 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,386 software developers and data experts.

multi-select list box

MSD
I am running a report that uses a query as its record source and opens a
form collecting beginning and ending item numbers to feed to the query. This
works, but now I'm trying to use the result of a multi-select list box on
the form to feed multiple item numbers to the query.

In other words - if you pick item #100,105,110 from the list box, the query
should display information for those 3 items.

If I type 'IN (100,105,110)' in the criteria cell of the 'itemNo' field in
my query, it works - I get information for those 3 items. However, I would
like the list of items to be variable, coming from the items selected in the
multi-select list box on my form.

How do I loop through the items in the list box and feed them to the query?
What should I put in the criteria cell for my query?

Thanks very much,

Emma
Nov 12 '05 #1
4 2756
Take a look at http://www.mvps.org/access/forms/frm0007.htm at "The Access
Web"

--
Doug Steele, Microsoft Access MVP
http://I.Am/DougSteele
(No private e-mails, please)

"MSD" <ms*********@hotmail.com> wrote in message
news:jweUb.403401$ts4.58439@pd7tw3no...
I am running a report that uses a query as its record source and opens a
form collecting beginning and ending item numbers to feed to the query. This works, but now I'm trying to use the result of a multi-select list box on
the form to feed multiple item numbers to the query.

In other words - if you pick item #100,105,110 from the list box, the query should display information for those 3 items.

If I type 'IN (100,105,110)' in the criteria cell of the 'itemNo' field in
my query, it works - I get information for those 3 items. However, I would
like the list of items to be variable, coming from the items selected in the multi-select list box on my form.

How do I loop through the items in the list box and feed them to the query? What should I put in the criteria cell for my query?

Thanks very much,

Emma

Nov 12 '05 #2
Well, if you don't mind writing a little bit of code in your report you
could do something like this in the Report Open Event and Detail Event:

************************************************** ****
Option Compare Database
Option Explicit

Dim RS As Recordset

Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)
Text0 = RS(0)
RS.MoveNext
End Sub

Private Sub Report_Open(Cancel As Integer)
Dim frm As Form, ctl As Control,
Dim v As Variant, str1 As String, strSql As String
Set frm = Forms!Form1
Set ctl = frm!List16
For Each v In ctl.ItemsSelected
str1 = str1 & "'" & ctl.ItemData(v) & "',"
Next
str1 = Left(str1, Len(str1) - 1) 'get rid of last comma
Me.RecordSource = "Select fld1 From Table1 " _
& "Where fld1 In (" & str1 & ")"
strSql = "Select fld1 From Table1 " _
& "Where fld1 In (" & str1 & ")"
Set RS = CurrentDb.OpenRecordset(strSql)
End Sub
************************************************** ****

In this example, the report is not directly bound to a recordsource.
You set the recordsource in the Report_Open event. I am only using one
field from a table, but you could select multiple fields and you filter
this recordset from the selections on the listbox on your form (which is
list16 in my example). You can use the

DoCmd.OpenReport "Report1" statement

and when Report1 open it will automatically read the selections from the
listbox. Then say you have 5 fields in the detail section of your
report, they could be txt0, txt1, txt2, txt3, txt4 or maybe you named
them

txtID, txtRegion, txtName, txtAddress, txtPhone

In the Detail section you would do this:

Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)
txtID = RS(0) 'or RS!ID
txtRegion = RS(1) 'or RS!Region
txtName = RS(2) 'or RS!Name
txtAddress = RS(3) 'or RS!Address
txtPhone = RS(4) 'or RS!Phone
RS.MoveNext
End Sub

And, of course, your sql statement in the report_open event would select
the same fields for the RecordSource and for the Recordset var (RS).

Rich

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 12 '05 #3
Thanks so much for your help. I think I'm close to having it work now.

I modified your code a bit - I create a string that contains the values
from the list box that I need for the parameter query. It looks like
this, e.g. 201,203,205.

In my parameter query, in the criteria cell for itemNo, I have :
In ([Forms]![frmSelectItems]![txtSelect])
where txtSelect is the name of the hidden control containing the string
'201,203,205' and frmSelectItems is the form with the list box and
hidden text box.

If I type:
In (201,203,205)
directly into the criteria cell, it works, however it doesn't work the
way I am doing it - even though the value of
[Forms]![frmSelectItems]![txtSelect] is 201,203,205.

Any ideas?

Thanks again,

Emma

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 12 '05 #4
Hi Emma,

This is the code I use to print from a multi-select box:

InSQL = CreateINClauseFromLB("lbListbox", "frmSelectItems",
ColWhereIDIsObtainedFrom, NoPosted)
SQLString = "[FieldName] IN (" & InSQL & ")"
DoCmd.OpenReport ReportName, , , Filter
Msgbox NoPosted & "Item(s) printed"


Public Function CreateINClauseFromLB(LB As String, FormName As String, Col
As Long, ByRef Count As Long) As String
'Create an comma seperated string from the selected items in a list box
'Returns the no of items in the list in Count

Dim ItemIndex As Variant
Dim Tempst As String
If TestMode = False Then On Error GoTo ErrorHand

Count = 0

If (Forms(FormName)(LB).ItemsSelected.Count < 1) Then
' If there are no items in the list, close form and exit procedure.
CreateINClauseFromLB = ""
Exit Function
End If

ItemIndex = 0

'Determine which listbox items are selected.
For Each ItemIndex In Forms(FormName)(LB).ItemsSelected
If Nz(Forms(FormName)(LB).Column(Col, ItemIndex), -1) > 0 Then
Tempst = Tempst & Format(Forms(FormName)(LB).Column(Col, ItemIndex))
& ","
Count = Count + 1
End If
Next ItemIndex

'Ensure that temp string has some characters first, then remove last comma
If Tempst <> "" Then Tempst = Left(Tempst, Len(Tempst) - 1)

CreateINClauseFromLB = Tempst
Exit Function
ErrorHand:
EHD , MODULE_NAME
End Function

"Emma Danielson" <ms*********@hotmail.com> wrote in message
news:40***********************@news.frii.net...
Thanks so much for your help. I think I'm close to having it work now.

I modified your code a bit - I create a string that contains the values
from the list box that I need for the parameter query. It looks like
this, e.g. 201,203,205.

In my parameter query, in the criteria cell for itemNo, I have :
In ([Forms]![frmSelectItems]![txtSelect])
where txtSelect is the name of the hidden control containing the string
'201,203,205' and frmSelectItems is the form with the list box and
hidden text box.

If I type:
In (201,203,205)
directly into the criteria cell, it works, however it doesn't work the
way I am doing it - even though the value of
[Forms]![frmSelectItems]![txtSelect] is 201,203,205.

Any ideas?

Thanks again,

Emma

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 12 '05 #5

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

Similar topics

37
by: ajikoe | last post by:
Hello, Is anyone has experiance in running python code to run multi thread parallel in multi processor. Is it possible ? Can python manage which cpu shoud do every thread? Sincerely Yours,...
4
by: Frank Jona | last post by:
Intellisense with C# and a multi-file assembly is not working. With VB.NET it is working. Is there a fix availible? We're using VisualStudio 2003 Regards Frank
12
by: * ProteanThread * | last post by:
but depends upon the clique: ...
0
by: frankenberry | last post by:
I have multi-page tiff files. I need to extract individual frames from the multi-page tiffs and save them as single-page tiffs. 95% of the time I receive multi-page tiffs containing 1 or more black...
6
by: cody | last post by:
What are multi file assemblies good for? What are the advantages of using multiple assemblies (A.DLL+B.DLL) vs. a single multi file assembly (A.DLL+A.NETMODULE)?
4
by: mimmo | last post by:
Hi! I should convert the accented letters of a string in the correspondent letters not accented. But when I compile with -Wall it give me: warning: multi-character character constant Do the...
5
by: bobwansink | last post by:
Hi, I'm relatively new to programming and I would like to create a C++ multi user program. It's for a project for school. This means I will have to write a paper about the theory too. Does anyone...
17
by: =?Utf-8?B?R2Vvcmdl?= | last post by:
Hello everyone, Wide character and multi-byte character are two popular encoding schemes on Windows. And wide character is using unicode encoding scheme. But each time I feel confused when...
0
by: Sabri.Pllana | last post by:
We apologize if you receive multiple copies of this call for papers. *********************************************************************** 2008 International Workshop on Multi-Core Computing...
2
by: Aussie Rules | last post by:
Hi, I have a site that Iwant to either display my text in english or french, based on the users prefernces ? I am new to webforms, but I know in winforms, this is pretty easy with a resource...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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
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...

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.