473,386 Members | 1,720 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.

Multiple select listbox, concatenate to single field in table

AccessIdiot
493 256MB
Apologies if this has been answered before - I searched but couldn't find anything.

I have a listbox that is populated with a query. I need to enable the user to select multiple items (simply set listbox Multi Select property to Simple yes?) and then I'd like to commit the choices to a single text field in a table, concatenated and separated by commas.

So in other words if the listbox has these values:

BRN
CM
XD
RG
RTD
WA
WE

I need the user to be able to choose (for example)

BRN
XD
WE

and in the table you'd see

BRN, XD, WE

Please keep in mind that I am not only an Access Idiot but also a VB Illiterate. :-) I can understand code but not write it myself and I don't always know where to put stuff so please be very detailed if you know how to help me!

Cheers,
Melissa
Nov 18 '08 #1
2 12034
AccessIdiot
493 256MB
OK, maybe I'm not as dumb as I thought. Turns out the answer was already out there and with a little tweaking I got it to work.

For anyone else struggling:

1) make sure your listbox has multi select set to simple (on the "other" tab in the control properties).

2) on the events tab in the On Click property add the following code:

Expand|Select|Wrap|Line Numbers
  1. Private Sub ListBoxName_Click()
  2.  
  3. Dim SelectedValues As String
  4. Dim frm As Form
  5. Dim varItem As Variant
  6. Dim lstItems As Control
  7. Set lstItems = Me!ListBoxName
  8.  
  9. For Each varItem In lstItems.ItemsSelected
  10.     If SelectedValues > "" Then
  11.         SelectedValues = SelectedValues & ", " & lstItems.ItemData(varItem)
  12.     Else
  13.         SelectedValues = lstItems.ItemData(varItem)
  14.     End If
  15. Next varItem
  16. Me!FieldInTableToPopulate = SelectedValues
  17.  
  18. End Sub
  19.  
Make sure you change the items in bold above to match your project.

*edit* bold doesn't seem to work in the code container so be sure to change:

ListBoxName and FieldInTableToPopulate
Nov 18 '08 #2
NeoPa
32,556 Expert Mod 16PB
Hi Melissa. A more compact version of the code might be :

Expand|Select|Wrap|Line Numbers
  1. Private Sub ListBoxName_Click()
  2.     Dim strSelected As String
  3.     Dim varItem As Variant
  4.  
  5.     With Me.ListBoxName
  6.         For Each varItem In .ItemsSelected
  7.             strSelected = strSelected & "," & .ItemData(varItem)
  8.         Next varItem
  9.         Me.FormControlToPopulate = Mid(strSelected, 2)
  10.     End With
  11. End Sub
See the technique used to avoid different versions of the code to add the first item. I use this concept frequently in my code.

NB. Also, A field is not the same thing as a control. A field is where data is held and may contain different values for each logical record in the record source. A control, on the other hand, is simply a window onto that data within a form or report (assuming the control is bound - otherwise it's not even that connected).
Sep 29 '11 #3

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

Similar topics

7
by: Rick Caborn | last post by:
Does anyone know of a way to execute sql code from a dynamically built text field? Before beginning, let me state that I know this db architecture is built solely for frustration and I hope to...
2
by: steve | last post by:
Hello, I am using ACCESS 2000 and am currently trying to make a database for a police department and they have several types of license classes. i.e A,B,C,D,E etc. What they have asked for is a...
2
by: Jen F. | last post by:
I have inherited a medical database in which there are multiple values stored in a single field (ie. "Current Conditions" field might contain 1-20 different conditions, separated by comma (ie....
3
by: Usenet | last post by:
Hi there, I am quite a novice to MS Access, and I would deeply appreciate help on the following: I have created a list box in a form. I wish for that list box to enter multiple values from table...
4
by: Dj87 | last post by:
Hi, I do not know whether was already posted a request about this problem, but my problem is this. I'd like to insert in a text field of a table of Ms Access some values from a list with the...
1
by: Ahmet Karaca | last post by:
Hi. myds.Reset(); mycommand.SelectCommand.CommandText= "Select att1 from Ing as Ingredient, Pro as Product "+ "where Pro.ad='apple' and Pro.id=Ing.id"; mycommand.Fill(myds, "Product"); // Here...
4
by: Dan | last post by:
Hi all, I am creating a search table where the keywords field is made up of several text fields and this is causing me some problems. I can concatentate the text ok but i can't seem to concatenate...
3
by: sejal17 | last post by:
hello Can any one tell me how to read multiple worksheets from a single excel file.I have stored that excel in xml file.so i want to read that xml that has multiple worksheet.And i want to store...
3
by: sejal17 | last post by:
hello Can any one tell me how to read multiple worksheets from a single excel file.I have stored that excel in xml file.so i want to read that xml that has multiple worksheet.And i want to store...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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
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...

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.