473,413 Members | 2,043 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,413 software developers and data experts.

Can't view Listbox selections

Hello all,

I am trying to use the selections from a multi-select listbox to modify selection criteria. When I try to reference either the listbox.column or listbox.itemdata values, the system jumps to the error area (see code). I am at my wits end with this one. Any ideas?

Form Code

If Me!lstBoard.ItemsSelected.Count > 0 Then
If Left(substrSQL, 1) <> "(" And substrSQL <> vbNullString Then
substrSQL = "(" & substrSQL & ")"
End If
BoardList = ListBoxContents(Me!lstBoard)
If numFound = 1 Then
substrSQL = substrSQL & "AND qryDispBoard.[Board] = " & BoardList
Else
substrSQL = substrSQL & "AND (qryDispBoard.[Board] = " & BoardList & ")"
End If
End If


Module Code

Public Function ListBoxContents(LB As ListBox) As String
'Returns extended SQL code if selected entries are found
'Null if none are selected
Dim intCurrentRow As Integer
ListBoxContents = vbNullString
numFound = 0
For intCurrentRow = 0 To LB.ColumnCount - 1
If LB.ItemData(intCurrentRow) Then
numFound = numFound + 1
If numFound > 1 Then
ListBoxContents = ListBoxContents & " OR " & LB.ItemData(intCurrentRow)
Else
ListBoxContents = LB.ItemData(intCurrentRow)
End If
End If
Next intCurrentRow
End Function


When I use the debugger and watch LB, there are no references to Column or ItemData. I tried using both LB.ItemData(intCurrentRow) as seen above as well as LB.Column(0, intCurrentRow).

Neither one works. Any ideas or suggestions would be greatly appreciated.

Thanks,

Rich
Apr 25 '07 #1
5 1833
MMcCarthy
14,534 Expert Mod 8TB
Hi Rich,

Try this ...

Expand|Select|Wrap|Line Numbers
  1. Dim valSel As Variant
  2.  
  3. If Me!lstBoard.ItemsSelected.Count > 0 Then
  4.    If Left(substrSQL, 1) <> "(" And substrSQL <> vbNullString Then
  5.       substrSQL = "(" & substrSQL & ")"
  6.    End If
  7.  
  8.    substrSQL = substrSQL & "AND qryDispBoard.[Board] IN ("
  9.    For Each valSel In Me!lstBoard.ItemsSelected
  10.       substrSQL = substrSQL & "'" & Me!lstBoard.ItemData(valSel) & "', "
  11.    Next
  12.  
  13.    substrSQL = Left(substrSQL, Len(substrSQL)-2) & ")"
  14. End If
  15.  
Apr 25 '07 #2
Thanks for the suggestion, but the SQL code works fine. It's the Access part that I am having a problem with. I can NOT see the data in either LB.Column or LB.ItemData - it doesn't exist. Any reference to it errors out. The system knows that items have been selected by looking at Items.Selected.Count. This returns the correct value, but when it comes to retrieveing the actual data, I get nothing but errors.

Thanks,

Rich

Hi Rich,

Try this ...

Expand|Select|Wrap|Line Numbers
  1. Dim valSel As Variant
  2.  
  3. If Me!lstBoard.ItemsSelected.Count > 0 Then
  4.    If Left(substrSQL, 1) <> "(" And substrSQL <> vbNullString Then
  5.       substrSQL = "(" & substrSQL & ")"
  6.    End If
  7.  
  8.    substrSQL = substrSQL & "AND qryDispBoard.[Board] IN ("
  9.    For Each valSel In Me!lstBoard.ItemsSelected
  10.       substrSQL = substrSQL & "'" & Me!lstBoard.ItemData(valSel) & "', "
  11.    Next
  12.  
  13.    substrSQL = Left(substrSQL, Len(substrSQL)-2) & ")"
  14. End If
  15.  
Apr 26 '07 #3
MMcCarthy
14,534 Expert Mod 8TB
Thanks for the suggestion, but the SQL code works fine. It's the Access part that I am having a problem with. I can NOT see the data in either LB.Column or LB.ItemData - it doesn't exist. Any reference to it errors out. The system knows that items have been selected by looking at Items.Selected.Count. This returns the correct value, but when it comes to retrieveing the actual data, I get nothing but errors.

Thanks,

Rich
Which is why I've given you a For ... Each loop which is how you retrieve the selections for a multi select listbox
Apr 26 '07 #4
Thank You!!!!!! Once I pulled my head out of my a$$ and paid attention to your fine advice, it worked like a charm!

Thanks again!!!


Which is why I've given you a For ... Each loop which is how you retrieve the selections for a multi select listbox
Apr 26 '07 #5
MMcCarthy
14,534 Expert Mod 8TB
Thank You!!!!!! Once I pulled my head out of my a$$ and paid attention to your fine advice, it worked like a charm!

Thanks again!!!
You're welcome.

I understand when the advice you get isn't what you expect it can throw you.

Mary
Apr 26 '07 #6

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

Similar topics

3
by: Andrew | last post by:
I'm having a major problem with a databound listbox in C#. In the constructor for the form I am trying to pre-select some of the items based in information in the database. When I step through the...
20
by: Neil | last post by:
I have an Access 2000 MDB file with a SQL 7 back end. I have a main table with 50,000 records; and I have a selections table with 50,000 records for each machine that uses the database (about...
0
by: Maqsood Ahmed | last post by:
Hello! I am using a ListBox control in a WinForm. ListBox control is populated through a DataSource. The problem starts when ListBox selects "index 0" automatically at form load. I have tried...
0
by: zdrakec | last post by:
Hello all: Listbox, multiple selectmode, populated at page load time only if page.ispostback = false. User selects several items, then clicks a button to move his selections. At postback time,...
0
by: | last post by:
Greets All, Question on data in datagrid /listbox and looping? I’m trying to decide the best way to write this code. 1 would like the user to make several selections from one listbox/combo box...
0
by: | last post by:
Greets All, Question on data in datagrid /listbox and looping? I’m trying to decide the best way to write this code. 1 would like the user to make several selections from one listbox/combo box...
4
by: keithsimpson3973 | last post by:
Does anyone know of a way to take the contents of a multiline textbox from a form, then when I open another form with a listbox on it, have the items matching the textbox items be higlighted in the...
2
by: ProgrammerChicago | last post by:
I'm not sure if this is the result of the postback behavior or my own code, but for some reason my onclick function is not detecting listbox selections (It's meant to delete files uploaded to the...
6
by: Mike | last post by:
Is there a way to get a ListView control to fire its SelectedIndexChanged event like the ListBox control? When the user selects multiple items at once, the ListView fires the event as each item...
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...
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
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
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,...
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...
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...

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.