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

ListBox Problem

54
I have a similar problem and I have been following the advice but to no avail. I am a novice with VBA so help would be greatly appreciated. This is my first post so I apologise if it is in the wrong place or format.

In my query I have:
Expand|Select|Wrap|Line Numbers
  1. Field: Invigilator no
  2. Table: VerifiedPayments
  3. Criteria: [Forms]![SelectWeek]![List0]     
  4.  
and in the form I have:

Expand|Select|Wrap|Line Numbers
  1.      Dim stDocName As String
  2.      Dim varX As Variant
  3.      Dim strWhere As String
  4.  
  5.     Select Case Me!optOpenReport
  6.     Case 1
  7.     stDocName = "InvigilatorClaims"
  8.     DoCmd.OpenReport stDocName, acPreview
  9.  
  10.     Case 2
  11.     stDocName = "InvigilatorClaimsIndividual"
  12.     With Me.List0
  13.     For Each varX In .ItemsSelected
  14.          strWhere = strWhere & "," & .ItemData(varX)
  15.     Next varX
  16.     strWhere = "[Invigilator no] In(" & Mid(strWhere, 2) & ")"
  17.     Call DoCmd.OpenReport(ReportName:=stDocName, _
  18.                           View:=acPreview, _
  19.                           WhereCondition:=strWhere)
  20.     End With
  21.  
  22.     End Select
  23.  
  24.  
Case 1 is fine. The problem is with Case 2. I am trying to select multiple values from a list box to use for the parameter mentioned above in the query to produce a report. However I keep getting the report with errors (meaning that the invigilator has not been found). (The cases are there because radio buttons are used to produce either one report or the other - I have had it working before with just single values).
Sep 10 '09 #1
24 2290
ChipR
1,287 Expert 1GB
My first step would be to stop the code before line 17 (debug or MsgBox) and take a look at what is in strWhere just before you try to use it.
Sep 10 '09 #2
g diddy
54
@ChipR
sorry to sound like a noob but how do you do that? (I'm using Microsoft Visual Basic.)
Sep 10 '09 #3
ChipR
1,287 Expert 1GB
You can just MsgBox the contents of the variable and see what part is malformed.
Expand|Select|Wrap|Line Numbers
  1. 'After this:
  2. strWhere = ...
  3.  
  4. MsgBox strWhere
  5.  
  6. 'Before this:
  7. DoCmd.OpenReport ...
There is also an article on debugging you may want to look at.
Sep 10 '09 #4
g diddy
54
Thank you for the MsgBox function that helped me see what the code is actually doing!

When i selected 2 from the list it came up with:

Expand|Select|Wrap|Line Numbers
  1. [Invigilator no] In(1458,221)
  2.  
which means that it is not enclosing the values in ""
I think!!

Can you see, from this, what is wrong with my code?

Eternally grateful!
Sep 10 '09 #5
ChipR
1,287 Expert 1GB
I'm not sure if that's the proper syntax for IN, as I've only seen it used with a subquery SELECT. I'll try to check it out sometime today, but maybe do some experimenting and see if you can get anything to work.
Sep 10 '09 #6
NeoPa
32,556 Expert Mod 16PB
@g diddy
You're excused G_Diddy, but for reference please avoid posting your questions in existing threads in future. This (thread hijacking) is covered in our Help section - accessible from all pages.

For reference, this thread was split from (and may refer to) List Box and Parameters.

Welcome to Bytes!
Sep 10 '09 #7
ChipR
1,287 Expert 1GB
I couldn't get IN to work that way. Here's what you can do instead:
Expand|Select|Wrap|Line Numbers
  1.   Dim count As Integer
  2.   ...
  3.   count = 0
  4.   With Me.List0 
  5.     For Each varX In .ItemsSelected 
  6.       If count > 0 Then strWhere = strWhere & " OR "
  7.       strWhere = strWhere & "[Invigilator no] = " & .ItemData(varX) 
  8.       count = count + 1
  9.     Next varX 
  10.   End With 
  11.  
  12.   DoCmd.OpenReport ...
It would be more efficient to paste the OR on the end every time through the loop, then remove it at the end using Left() or something, but this will do as an example.
Sep 10 '09 #8
NeoPa
32,556 Expert Mod 16PB
@ChipR
It is indeed. I don't believe the automatic adding of quotes works within such a function though.

You (The OP) need to determine the type of the field you are comparing and ensure string or date literals are enclosed by the requisite characters (Quotes (') and Double-Quotes (") - Where and When to use them).
Sep 10 '09 #9
g diddy
54
I tried your way ChipR and received the following when I used the MSGBOX function:

Expand|Select|Wrap|Line Numbers
  1. [Invigilator no] = 1458 OR [Invigilator no] = 221 OR [Invigilator no] = 369
  2.  
I modified the code to:

Expand|Select|Wrap|Line Numbers
  1.     Dim stDocName As String
  2.     Dim varX As Variant
  3.     Dim strWhere As String
  4.     Dim count As Integer
  5.  
  6.     Select Case Me!optOpenReport
  7.     Case 1
  8.     stDocName = "InvigilatorClaims"
  9.     DoCmd.OpenReport stDocName, acPreview
  10.  
  11.     Case 2
  12.     stDocName = "InvigilatorClaimsIndividual"
  13.     count = 0
  14.     With Me.Combo46
  15.     For Each varX In .ItemsSelected
  16.       If count > 0 Then strWhere = strWhere & " OR "
  17.       strWhere = strWhere & "[Invigilator no] = " & "'" & .ItemData(varX) & "'"
  18.       count = count + 1
  19.     Next varX
  20.  
  21.  
  22.     MsgBox strWhere
  23.  
  24.     Call DoCmd.OpenReport(ReportName:=stDocName, _
  25.                           View:=acPreview, _
  26.                           WhereCondition:=strWhere)
  27.  
  28.  
  29.     End With
  30.  
  31. End Select
  32.  
and this gave me:

Expand|Select|Wrap|Line Numbers
  1. [Invigilator no] = '1458' OR [Invigilator no] = '221' OR [Invigilator no] = '369'
  2.  
However I now get an error saying the expression is typed incorrectly or is too complex to be evaluated.

I have also been playing round with my original code and managed to get strWhere to contain:

Expand|Select|Wrap|Line Numbers
  1. [Invigilator no] IN('1458','1492','221','369',')
  2.  
However the last one still has ,' after it which isn't right. I have tried so many combinations but cannot get it to work. I know that the ,' is appearing because of the loop. However without adding it in the loop it isn't added at all.

The code so far is:

Expand|Select|Wrap|Line Numbers
  1.     Dim stDocName As String
  2.     Dim varX As Variant
  3.     Dim strWhere As String
  4.  
  5.     Select Case Me!optOpenReport
  6.     Case 1
  7.     stDocName = "InvigilatorClaims"
  8.     DoCmd.OpenReport stDocName, acPreview
  9.  
  10.     Case 2
  11.     stDocName = "InvigilatorClaimsIndividual"
  12.     With Me.Combo46
  13.     For Each varX In .ItemsSelected
  14.          strWhere = strWhere & "'" & .ItemData(varX) & "'"
  15.          strWhere = strWhere & ","
  16.     Next varX
  17.     strWhere = "[Invigilator no] In('" & Mid(strWhere, 2) & "')"
  18.  
  19.     MsgBox strWhere
  20.  
  21.     Call DoCmd.OpenReport(ReportName:=stDocName, _
  22.                           View:=acPreview, _
  23.                           WhereCondition:=strWhere)
  24.     End With
  25.  
  26.  
  27. End Select
  28.  
Can you see where i'm going wrong at all? I am such a novice with VBA so I apologise for being a pain!!
Sep 11 '09 #10
ChipR
1,287 Expert 1GB
If my code didn't work, then you've got some other issue.
If you still want to do it that way though, use Mid() to remove the last ,'
Expand|Select|Wrap|Line Numbers
  1. Mid(strWhere, 1, Len(strWhere)-2)
Sep 11 '09 #11
g diddy
54
Where abouts in the code would that go mate?
Sep 11 '09 #12
ChipR
1,287 Expert 1GB
On line 17, just replace your Mid() with the one I wrote. But looking more closely, I think change it to
Mid(strWhere, 1, Len(strWhere)-1)

So is the [Invigilator no] a number field or a text field?
Sep 11 '09 #13
g diddy
54
@ChipR
its a number field mate with the criteria:

Expand|Select|Wrap|Line Numbers
  1. [Forms]![SelectWeek]![Combo46]
  2.  
(n.b. combo46 is just the name, the multi select box is actually a list box i just didn't get round to renaming it in the form)

Just added your code in and now get the message box saying strWhere contains:
Expand|Select|Wrap|Line Numbers
  1. [Invigilator no] IN('1458','1492','221','369')
  2.  
but then an error message saying that the expression is too complex etc as before. Without the ' ' around the numbers I get no error message but my report shows up with errors (i.e. invigilator not found). For a number field that is being passed the parameters like 1458, 1492 etc do they have to be enclosed in ' ' or not?
Sep 11 '09 #14
ChipR
1,287 Expert 1GB
Number fields do not need to be enclosed in quotes, so I would expect the first MsgBox output you listed in post #10 to work. We'll have to look into this error about the expression being too complex. What is the exact text of the error message, and does it have an error number?
I still can't get IN to work that way, but since it's the same error, I would guess that it's the same solution.

I just noticed you have an extra quote in your string.
Expand|Select|Wrap|Line Numbers
  1. strWhere = "[Invigilator no] In('" & Mid(strWhere, 2) & "')"
Since you are already putting quotes around each number, change this to:
Expand|Select|Wrap|Line Numbers
  1. strWhere = "[Invigilator no] In(" & Mid(strWhere, 1, Len(strWhere)-1) & ")"
Sep 11 '09 #15
g diddy
54
The error says:

Expand|Select|Wrap|Line Numbers
  1. This expression is typed incorrectly,or it is too complex to be evaluated. For example, a numeric expression may contain too many complicated elements. Try simplifying the expression by assigning parts of the expression to variables.
  2.  
This error doesn't show when the quotes are not around the numbers though. I'm going to leave them off from now on (thanks for letting me know that by the way...i would have been forever trying to correctly place these quotes when they are not needed!!!). The only problem now is that the report still shows errors so the multiselect box must not be passing the values to the parameter correctly. I'm really not sure why. It used to work with single values, I would select the invigilators name (e.g. Anderson, David) and it would pass their id (1458) to the query which then created the report with the results on. The only thing I have done differently is change it from a combo box to a multi select (simple) list box. Would that have changed anything at all? The row source is still the same too

(n.b. when 4 were selected strWhere contained:

Expand|Select|Wrap|Line Numbers
  1. [Invigilator no] IN(1458,1492,221,369)
  2.  
which looks like it is in the correct format...well to me anyway but i'm a novice!!)
Sep 11 '09 #16
g diddy
54
I've found this webpage which seems really relevant but I can't make head nor tail of it. You might find it useful though

http://www.fontstuff.com/access/acctut18.htm
Sep 11 '09 #17
ChipR
1,287 Expert 1GB
Thanks for the link. According to that, the strWhere you have looks fine. What errors do you get on the report? What do you mean by "pass their id to the query"?
Sep 11 '09 #18
g diddy
54
it just says #error which is because the invigilator no has not been recognised.
in the query it has for the field Invigilator no the criteria:

[Forms]![SelectWeek]![Combo46]

which takes the value from the multi select list box for use as a parameter

so when the user selects an invigilator from the multi select list box (named Combo46) it should pass their ID to the query which is then used to produce the report. When the list box only allowed the user to select single values it worked correctly it is only since trying to change it to multiple select that it failed. I hope that helps.
Sep 11 '09 #19
ChipR
1,287 Expert 1GB
You may not be able to reference the actual value selected in a multi-select list box that way. Notice in the code you have to reference the Combo46.ItemsSelected collection. What you could do is whenever an item is selected in the list box, use code to generate the string in another (hidden) text box, and then reference that text box in your query.
Sep 11 '09 #20
g diddy
54
that sounds like a plan! how would I go about doing this though? I know how to create text boxes but I'm not entirely sure on how to generate the string for use as a reference
(eternally grateful!!)
Sep 11 '09 #21
ChipR
1,287 Expert 1GB
Are you using that reference in the query to filter the query results, then using the strWhere to do the exact same thing? If that is the case, you just remove that condition in the query, let it return all the records, and use the where condition in the DoCmd.OpenReport to see only the ones you want.
Sep 11 '09 #22
g diddy
54
YES!! that was the problem all along!! Thank you so much for all your help mate. I really appreciate it thank you!!
Sep 11 '09 #23
ChipR
1,287 Expert 1GB
Glad to hear you got it working. I learned a little something too.
Sep 11 '09 #24
NeoPa
32,556 Expert Mod 16PB
Just to clarify for anyone reading who gets confused by all the to-ing & fro-ing :

The best way to apply filtering to a report is to design a query (or Record Source) which returns all records, then apply the filter to the report itself. This is most easily done in the call that opens the report itself using the WhereCondition parameter.

It's possible to apply criteria to the underlying query (as the OP did in this case) only to find that flexibility later is severely reduced.

BTW Sorry that I didn't help in time guys. By the time I had caught up with the problem (Post #14 gave the clue - but wasn't specific about what type of Criteria it was working with so I missed it. It was only at post #19 I realised the OP was trying to applying the criteria within the query) Chip had already supplied the solution.

As the query was already saying "WHERE [Invigilator] = [Value from form]" the use of IN() could never work (Equivalent to [Invigilator = IN(...)). Only one equality operator can be used at a time. Either "=" OR "IN()" but not both.
Sep 11 '09 #25

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

Similar topics

5
by: Brian | last post by:
Hi, All, I'm using MS.net 2003 and using a windows.forms.listbox control for my window application but I don't know why the Horizontal Scrollbar could NOT be shown even if I set...
5
by: Bill | last post by:
I have have two list boxes. One is a listing of all possible variables. We'll call this listbox A. The other is a listing of all the selected variables. We'll call this listbox B. If a person...
2
by: Simon P | last post by:
Hello group, I'm in desperate need of help. Here goes : I have the following tables : CONTACTS (ContactID, FirstName, LastName, Company, etc.), SHOWS (ShowID, ShowDescription) and SHOWDETAILS...
1
by: Josema | last post by:
Hi to all, I have a class (persons) that derives from collection base: and another class (person) with this properties: -ID -Name When i have complete filled the object Persons with all...
1
by: MrNobody | last post by:
Hi, I'm doing something where I add custom objects to a ListBox which have aToString() method overriden so it displays what I want. When adding instances of these custom objects to the ListBox I...
1
by: yamne | last post by:
I have a problem. When I click in edit datagrid button I show two listbox and two button. I use two button to move data between two listbox. My problem is that I can't call the listbox in the...
4
by: Ron | last post by:
I've got a listbox that holds a list of groups. Users can select a group, hit the remove button and the group should be removed from the listbox. The only problem is that no matter which group you...
8
by: nirdeshonline | last post by:
Hi, I have added a simple listbox in windows form under c# 2.0. It contains a collection of approx 10 strings as list items. Now when i resize the form whole listbox flickers. Please tell me...
9
by: zdrakec | last post by:
Hello all: Clearly, I'm not getting it! Here is the scenario: On a web page, I have two list boxen and a text box. The first listbox is populated at page load time (if it is not a postback)....
15
by: Doogie | last post by:
I have a .net app that a user currently enters a number in a text box, hits a button and a data call is executed. She wants the ability to enter in multiple numbers (up to 100). So to make...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.