473,666 Members | 1,991 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Can't view Listbox selections

6 New Member
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.itemdat a 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.Ite msSelected.Coun t > 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(int CurrentRow) Then
numFound = numFound + 1
If numFound > 1 Then
ListBoxContents = ListBoxContents & " OR " & LB.ItemData(int CurrentRow)
Else
ListBoxContents = LB.ItemData(int CurrentRow)
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(int CurrentRow) 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 1850
MMcCarthy
14,534 Recognized Expert Moderator MVP
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
rjchristensen
6 New Member
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 Recognized Expert Moderator MVP
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
rjchristensen
6 New Member
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 Recognized Expert Moderator MVP
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
2819
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 debugger it all seems to be working properly until the Load function is called, and then all my selections but 1 are unselected. Anybody had any experience with this and know of a fix? Listbox is databound Listbox appears on a tabpage inside...
20
2676
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 25-50). This allows each user to have their own set of selections. The selections table has three fields: ID (int), Sel (bit), MachName (varchar). ID and MachName comprise the primary key. I have a view that combines the main table and the entries...
0
1266
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 ListBox.ClearSelection() and ListBox.SelectedIndex = -1 but they don't work. ClearSelection actually clears all selections but it doesn't seem to affect visual effects, same goes for SelectedIndex = -1. I have also tried Invalidate(true) and...
0
935
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, many of the selections (but not all) are "lost": that is, listbox.item(x).selected = false, even though it was in fact selected. Sometimes only the first 4 or 5 items remain selected; sometimes it skips over some selected items... Any ideas?
0
1533
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 that would Populate another Listbox/datagrid. Then it would cycle through the selections made in the populated listbox/datagrid and do some calculations. The calculations made would have to add the first two numbers in the populated...
0
1401
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 that would Populate another Listbox/datagrid. Then it would cycle through the selections made in the populated listbox/datagrid and do some calculations. The calculations made would have to add the first two numbers in the populated...
4
1715
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 listbox? Example: User opens form with textbox that has 2 items in it, say "BDU1" and "BDU2". He clicks on an Edit button and open a form that has a listbox that contains selections loaded from an Access table. I would like the "BDU1" and "BDU2"...
2
3026
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 server). The RemoveFiles function is being executed, bu I have a feeling the selections are being cleared beforehand. There is a Page_Load function which repopulates the listbox. Could this be getting in the way?
6
4715
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 gets selected. The ListBox waits to fire the event until all selections are complete. Thanks, Mike
0
8871
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
8551
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
8640
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...
1
6198
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5664
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
4198
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...
0
4369
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2011
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1776
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.